Skip to content

Commit 756e33b

Browse files
authored
Merge pull request #3 from Kaushalsurana/main
Add URL input support using --url argument
2 parents a4e9e8d + 28367f5 commit 756e33b

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

sonar-use-cases/fact_checker_cli/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ A command-line tool that identifies false or misleading claims in articles or st
1616
### 1. Install required dependencies
1717

1818
```bash
19-
pip install requests pydantic
19+
# Ensure you are using the same pip associated with the python3 you intend to run the script with
20+
pip install requests pydantic newspaper3k
2021
```
2122

2223
### 2. Make the script executable
@@ -76,6 +77,12 @@ This will analyze the claim, research it using Perplexity's Sonar API, and retur
7677
./fact_checker.py --file article.txt
7778
```
7879

80+
### Check an article from a URL
81+
82+
```bash
83+
./fact_checker.py --url https://www.example.com/news/article-to-check
84+
```
85+
7986
### Specify a different model
8087

8188
```bash
@@ -156,4 +163,4 @@ Claim 1: ❌ FALSE
156163
- The accuracy of fact-checking depends on the quality of information available through the Perplexity Sonar API.
157164
- Like all language models, the underlying AI may have limitations in certain specialized domains.
158165
- The structured outputs feature requires a Tier 3 or higher Perplexity API account.
159-
- The tool does not replace professional fact-checking services for highly sensitive or complex content.
166+
- The tool does not replace professional fact-checking services for highly sensitive or complex content.

sonar-use-cases/fact_checker_cli/fact_checker.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import requests
1717
from pydantic import BaseModel, Field
18+
from newspaper import Article, ArticleException
19+
from requests.exceptions import RequestException
1820

1921

2022
class Claim(BaseModel):
@@ -92,6 +94,8 @@ def _load_system_prompt(self, prompt_file: str) -> str:
9294
try:
9395
with open(prompt_file, 'r', encoding='utf-8') as f:
9496
return f.read().strip()
97+
except FileNotFoundError:
98+
print(f"Warning: Prompt file not found at {prompt_file}", file=sys.stderr)
9599
except Exception as e:
96100
print(f"Warning: Could not load system prompt from {prompt_file}: {e}", file=sys.stderr)
97101
print("Using default system prompt.", file=sys.stderr)
@@ -113,6 +117,8 @@ def check_claim(self, text: str, model: str = DEFAULT_MODEL, use_structured_outp
113117
Returns:
114118
The parsed response containing fact check results.
115119
"""
120+
if not text or not text.strip():
121+
return {"error": "Input text is empty. Cannot perform fact check."}
116122
user_prompt = f"Fact check the following text and identify any false or misleading claims:\n\n{text}"
117123

118124
headers = {
@@ -290,9 +296,10 @@ def main():
290296
input_group = parser.add_mutually_exclusive_group(required=True)
291297
input_group.add_argument("-t", "--text", type=str, help="Text to fact check")
292298
input_group.add_argument("-f", "--file", type=str, help="Path to file containing text to fact check")
299+
input_group.add_argument("-u", "--url", type=str, help="URL of the article to fact check")
293300

294301
parser.add_argument(
295-
"-m",
302+
"-m",
296303
"--model",
297304
type=str,
298305
default=FactChecker.DEFAULT_MODEL,
@@ -334,9 +341,35 @@ def main():
334341
except Exception as e:
335342
print(f"Error reading file: {e}", file=sys.stderr)
336343
return 1
337-
else:
344+
elif args.url:
345+
try:
346+
print(f"Fetching content from URL: {args.url}", file=sys.stderr)
347+
response = requests.get(args.url, timeout=15) # Add a timeout
348+
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
349+
350+
article = Article(url=args.url)
351+
article.download(input_html=response.text)
352+
article.parse()
353+
text = article.text
354+
if not text:
355+
print(f"Error: Could not extract text from URL: {args.url}", file=sys.stderr)
356+
return 1
357+
except RequestException as e:
358+
print(f"Error fetching URL: {e}", file=sys.stderr)
359+
return 1
360+
except ArticleException as e:
361+
print(f"Error parsing article content: {e}", file=sys.stderr)
362+
return 1
363+
except Exception as e: # Catch other potential errors during fetch/parse
364+
print(f"An unexpected error occurred while processing the URL: {e}", file=sys.stderr)
365+
return 1
366+
else: # This corresponds to args.text
338367
text = args.text
339-
368+
369+
if not text: # Ensure text is not empty before proceeding
370+
print("Error: No text found to fact check.", file=sys.stderr)
371+
return 1
372+
340373
print("Fact checking in progress...", file=sys.stderr)
341374
results = fact_checker.check_claim(
342375
text,

0 commit comments

Comments
 (0)