harshnigamppt commited on
Commit
631fcb1
Β·
verified Β·
1 Parent(s): 8bbe87a

Upload 3 files

Browse files
Files changed (3) hide show
  1. .huggingface.yaml +2 -0
  2. app.py +106 -0
  3. requirements.txt +4 -0
.huggingface.yaml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ sdk: gradio
2
+ python_version: "3.10"
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import requests
3
+ from collections import Counter
4
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
5
+ import re
6
+ import gradio as gr
7
+
8
+ # βœ… Load Roberta fake news model
9
+ model_name = "hamzab/roberta-fake-news-classification"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
12
+ classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
13
+
14
+ # βœ… API Keys
15
+ GOOGLE_API_KEY = "AIzaSyCKnDlhWih34GdCuheNusnrEw_YE_q6GWQ"
16
+ NEWSAPI_KEY = "fcb304918fce4fb29b17b6c95dbc7518"
17
+
18
+ # βœ… Keyword extractor
19
+ def extract_keywords(text, max_words=5):
20
+ words = re.findall(r'\b\w+\b', text.lower())
21
+ stopwords = {"the", "and", "is", "in", "to", "of", "a", "on", "for", "with", "as", "by", "at", "an", "be", "are", "from", "this", "it", "that"}
22
+ filtered = [w for w in words if w not in stopwords and len(w) > 2]
23
+ return " ".join([w for w, _ in Counter(filtered).most_common(max_words)])
24
+
25
+ # βœ… Google Fact Check
26
+ def check_google_fact_check(query):
27
+ try:
28
+ response = requests.get("https://factchecktools.googleapis.com/v1alpha1/claims:search", params={"query": query, "key": GOOGLE_API_KEY}).json()
29
+ if "claims" in response and response["claims"]:
30
+ result = ""
31
+ for claim in response["claims"][:3]:
32
+ text = claim.get("text", "")
33
+ review = claim.get("claimReview", [{}])[0]
34
+ rating = review.get("textualRating", "Unrated")
35
+ source = review.get("publisher", {}).get("name", "Unknown")
36
+ url = review.get("url", "")
37
+ result += f"βœ… Claim: {text}\nπŸ“Š Verdict: {rating}\nπŸ“° Source: {source}\nπŸ”— {url}\n\n"
38
+ return result.strip()
39
+ return None
40
+ except Exception as e:
41
+ return f"❌ Google API error: {e}"
42
+
43
+ # βœ… NewsAPI fallback
44
+ def search_newsapi(query):
45
+ try:
46
+ response = requests.get("https://newsapi.org/v2/everything", params={
47
+ "q": query, "apiKey": NEWSAPI_KEY, "language": "en", "sortBy": "relevancy", "pageSize": 3
48
+ }).json()
49
+ if response.get("status") != "ok":
50
+ return f"❌ NewsAPI error: {response.get('message')}"
51
+ articles = response.get("articles", [])
52
+ if not articles:
53
+ return "ℹ️ No similar real news articles found."
54
+ output = "πŸ“° No official fact-check found.\n\nBut here are similar real news articles:\n\n"
55
+ for article in articles:
56
+ title = article.get("title", "No title")
57
+ source = article.get("source", {}).get("name", "Unknown")
58
+ url = article.get("url", "#")
59
+ output += f"β€’ πŸ“° Source: {source}\n πŸ“Œ Title: {title}\n πŸ”— {url}\n\n"
60
+ return output.strip()
61
+ except Exception as e:
62
+ return f"❌ NewsAPI error: {e}"
63
+
64
+ # βœ… Chatbot logic
65
+ def fake_news_chatbot(message, history):
66
+ try:
67
+ prediction = classifier(message)[0]
68
+ label = prediction['label']
69
+ confidence = round(prediction['score'], 2)
70
+
71
+ if confidence < 0.7:
72
+ verdict = "🟑 UNSURE"
73
+ elif label == "FAKE":
74
+ verdict = "πŸŸ₯ FAKE"
75
+ else:
76
+ verdict = "🟩 REAL"
77
+
78
+ ml_result = f"πŸ“Š ML Prediction: {verdict}\nπŸ”’ Confidence: {confidence}"
79
+
80
+ keywords = extract_keywords(message)
81
+ fact_result = check_google_fact_check(keywords)
82
+ if fact_result:
83
+ return f"{ml_result}\n\nπŸ” Keywords Used: {keywords}\n\n{fact_result}"
84
+
85
+ newsapi_result = search_newsapi(keywords)
86
+ if "πŸ“° Source:" in newsapi_result and verdict == "πŸŸ₯ FAKE":
87
+ ml_result += " ⚠️ ML says FAKE but similar real news found"
88
+ elif "No similar" in newsapi_result and verdict == "🟩 REAL":
89
+ ml_result += " ⚠️ ML says REAL but no matching news found"
90
+
91
+ return f"{ml_result}\n\nπŸ” Keywords Used: {keywords}\n\n{newsapi_result}"
92
+
93
+ except Exception as e:
94
+ return f"❌ Error: {str(e)}"
95
+
96
+ # βœ… Launch Chatbot
97
+ gr.ChatInterface(
98
+ fn=fake_news_chatbot,
99
+ title="🧠 Fake News Detection Chatbot",
100
+ description="Ask me if a news item is real or fake. I’ll use an ML model + Google Fact Check + NewsAPI!",
101
+ examples=[
102
+ "The Prime Minister announced a new moon mission",
103
+ "Aliens landed in New York yesterday",
104
+ "COVID vaccine turns you into a lizard"
105
+ ],
106
+ ).launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ transformers
2
+ gradio
3
+ torch
4
+ requests