import gradio as gr import spaces import torch import requests import feedparser import trafilatura import plotly.express as px import pandas as pd import html import re from urllib.parse import urlparse, quote_plus from transformers import ( AutoTokenizer, AutoModelForSequenceClassification ) # ===================================================== # 1. CONFIGURATION # ===================================================== APP_NAME = "TruthLens AI" MODEL_NAME = "cross-encoder/nli-deberta-v3-small" HEADERS = { "User-Agent": "TruthLensAI/1.0 Educational Project" } LABELS = [ "Contradicts", "Supports", "Unclear" ] # ===================================================== # 2. LOAD HUGGING FACE MODEL # ===================================================== # Load on CPU first. # GPU is used only inside the ZeroGPU function below. tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) model = AutoModelForSequenceClassification.from_pretrained( MODEL_NAME ) model.eval() # ===================================================== # 3. EXTRACT NEWS ARTICLE # ===================================================== def extract_article(url): parsed = urlparse(url) if parsed.scheme not in ["http", "https"]: raise ValueError("Enter a valid HTTP or HTTPS URL.") if not parsed.hostname: raise ValueError("Invalid website address.") if parsed.hostname.lower() in [ "localhost", "127.0.0.1", "::1" ]: raise ValueError("This website address is not allowed.") response = requests.get( url, headers=HEADERS, timeout=20, stream=True ) response.raise_for_status() if int(response.headers.get("Content-Length", 0)) > 4_000_000: raise ValueError("Article page is too large.") content = response.raw.read(4_000_000) response.close() text = trafilatura.extract(content) metadata = trafilatura.extract_metadata(content) if not text: raise ValueError( "Could not extract article. " "The website may block automatic access." ) title = ( metadata.title if metadata and metadata.title else text[:150] ) return title, text[:10000] # ===================================================== # 4. SEARCH GOOGLE NEWS RSS # ===================================================== def search_news(claim): rss_url = ( "https://news.google.com/rss/search?q=" + quote_plus(claim) + "&hl=en-IN&gl=IN&ceid=IN:en" ) response = requests.get( rss_url, headers=HEADERS, timeout=20 ) response.raise_for_status() feed = feedparser.parse(response.content) sources = [] seen = set() for entry in feed.entries[:30]: source_info = entry.get("source", {}) publisher = source_info.get( "title", "Unknown publisher" ) publisher_url = source_info.get("href", "") link = entry.get("link", "") domain = ( urlparse(publisher_url).netloc if publisher_url else urlparse(link).netloc ) domain = domain.lower().replace("www.", "") if not domain or domain in seen: continue if urlparse(link).scheme not in ["http", "https"]: continue seen.add(domain) summary = re.sub( r"<[^>]*>", " ", entry.get("summary", "") ) summary = html.unescape(summary) title = entry.get("title", "") evidence = title + ". " + summary sources.append({ "publisher": publisher, "domain": domain, "title": title, "link": link, "evidence": evidence[:1800] }) if len(sources) >= 8: break return sources # ===================================================== # 5. AI FACT CHECKING USING HUGGING FACE # ===================================================== @spaces.GPU(duration=120) def analyse_evidence(claim, sources): results = [] # Move the model to GPU only during this function. model.to("cuda") try: for source in sources: evidence = source["evidence"] # Premise = evidence # Hypothesis = news claim inputs = tokenizer( evidence, claim, return_tensors="pt", truncation=True, max_length=384 ) inputs = { key: value.to("cuda") for key, value in inputs.items() } with torch.inference_mode(): output = model(**inputs) probabilities = torch.softmax( output.logits, dim=-1 )[0] scores = probabilities.cpu().tolist() # Model label order: # 0 = contradiction # 1 = entailment # 2 = neutral contradiction = scores[0] support = scores[1] unclear = scores[2] if support >= 0.45 and support > contradiction: verdict = "Supports" confidence = support elif ( contradiction >= 0.45 and contradiction > support ): verdict = "Contradicts" confidence = contradiction else: verdict = "Unclear" confidence = unclear results.append({ **source, "verdict": verdict, "confidence": round(confidence * 100, 1) }) finally: # Return model to CPU after analysis. model.to("cpu") if torch.cuda.is_available(): torch.cuda.empty_cache() return results # ===================================================== # 6. CALCULATE FINAL VERDICT # ===================================================== def calculate_verdict(results): counts = { "Supporting": 0, "Contradicting": 0, "Unclear": 0 } for item in results: if item["verdict"] == "Supports": counts["Supporting"] += 1 elif item["verdict"] == "Contradicts": counts["Contradicting"] += 1 else: counts["Unclear"] += 1 total = len(results) if total == 0: return "NON-VERIFIABLE", { "Supporting": 0, "Contradicting": 0, "Unclear": 100 } percentages = { key: round(value / total * 100, 1) for key, value in counts.items() } if total < 3: verdict = "NON-VERIFIABLE" elif percentages["Supporting"] >= 60: verdict = "LIKELY TRUE" elif percentages["Contradicting"] >= 60: verdict = "LIKELY FALSE" else: verdict = "NON-VERIFIABLE" return verdict, percentages # ===================================================== # 7. CREATE PERCENTAGE GRAPH # ===================================================== def create_graph(percentages): df = pd.DataFrame({ "Category": list(percentages.keys()), "Percentage": list(percentages.values()) }) fig = px.bar( df, x="Category", y="Percentage", color="Category", text="Percentage", range_y=[0, 110], color_discrete_map={ "Supporting": "#22C55E", "Contradicting": "#EF4444", "Unclear": "#F59E0B" }, title="Evidence Distribution" ) fig.update_traces( texttemplate="%{text}%", textposition="outside" ) fig.update_layout( template="plotly_dark", paper_bgcolor="#111827", plot_bgcolor="#111827", font_color="white", showlegend=False, yaxis_title="Percentage of sources", xaxis_title="" ) return fig # ===================================================== # 8. DISPLAY SOURCE CARDS # ===================================================== def create_source_cards(results): if not results: return "

No matching sources found.

" cards = "" for item in results: if item["verdict"] == "Supports": color = "#22C55E" elif item["verdict"] == "Contradicts": color = "#EF4444" else: color = "#F59E0B" publisher = html.escape(item["publisher"]) title = html.escape(item["title"]) domain = html.escape(item["domain"]) link = html.escape(item["link"], quote=True) cards += f"""
{publisher} {item['verdict'].upper()}

{title}

{domain} | Model confidence: {item['confidence']}%

Read original report →
""" return cards # ===================================================== # 9. MAIN NEWS ANALYSIS # ===================================================== def run_analysis(url, custom_claim, progress=gr.Progress()): try: if not url or not url.strip(): raise ValueError( "Please enter a news article URL." ) progress(0.1, desc="Extracting news article...") title, article = extract_article(url) claim = ( custom_claim.strip() if custom_claim and custom_claim.strip() else title ) progress(0.3, desc="Searching news sources...") sources = search_news(claim) if not sources: percentages = { "Supporting": 0, "Contradicting": 0, "Unclear": 100 } return ( f"## 📰 {title}\n\n{article[:1500]}", "

NON-VERIFIABLE

" "

No matching independent reports were found.

", create_graph(percentages), "

No sources found.

", "Insufficient evidence to verify this news." ) progress( 0.5, desc="AI is comparing evidence from publishers..." ) results = analyse_evidence( claim, sources ) progress(0.85, desc="Generating final report...") verdict, percentages = calculate_verdict(results) graph = create_graph(percentages) source_cards = create_source_cards(results) if verdict == "LIKELY TRUE": color = "#22C55E" explanation = "Most collected sources support the claim." elif verdict == "LIKELY FALSE": color = "#EF4444" explanation = "Most collected sources contradict the claim." else: color = "#F59E0B" explanation = "Available evidence is insufficient or mixed." report = f"""

FINAL VERDICT

{verdict}

{explanation}


{percentages['Supporting']}%

Supporting

{percentages['Contradicting']}%

Contradicting

{percentages['Unclear']}%

Unclear
""" summary = f""" ### AI Analysis Report **Claim analysed:** {claim} **Sources collected:** {len(results)} **AI model:** DeBERTa Natural Language Inference **Evidence distribution:** - Supporting: {percentages['Supporting']}% - Contradicting: {percentages['Contradicting']}% - Unclear: {percentages['Unclear']}% ### Important limitations The percentages represent the distribution of source assessments, NOT the probability that the news is true. The system searches Google News RSS and does not cover the entire internet. Publishers may repeat the same original report, so their coverage is not necessarily independent. AI predictions can be incorrect. Always check the original reports before accepting a verdict. """ progress(1.0, desc="Verification complete!") return ( f"## 📰 {title}\n\n{article[:1800]}", report, graph, source_cards, summary ) except Exception as error: error_message = html.escape(str(error)) return ( "## Article Analysis", f"""

NON-VERIFIABLE

{error_message}

""", create_graph({ "Supporting": 0, "Contradicting": 0, "Unclear": 100 }), "

Source information unavailable.

", "Please check the URL and try again." ) # ===================================================== # 10. GRADIO DESIGN # ===================================================== CUSTOM_CSS = """ .gradio-container { max-width: 1150px !important; margin: auto !important; } #analyze-button { background: linear-gradient(90deg,#2563EB,#7C3AED); color: white; border: none; } #main-header { text-align: center; padding: 25px 10px; } #main-header h1 { font-size: 42px; background: linear-gradient(90deg,#60A5FA,#A78BFA); -webkit-background-clip: text; color: transparent; } """ with gr.Blocks( theme=gr.themes.Soft( primary_hue="blue", secondary_hue="purple", neutral_hue="slate" ), css=CUSTOM_CSS, title="TruthLens AI" ) as demo: # HEADER gr.HTML("""
🔎

TRUTHLENS AI

BEYOND HEADLINES. TOWARDS TRUTH.

AI-powered multi-source news verification

""") # INPUT AREA with gr.Group(): gr.Markdown("## 🔗 Verify a News Article") url_input = gr.Textbox( label="News Article URL", placeholder="Paste your news article link here..." ) claim_input = gr.Textbox( label="Specific claim (optional)", placeholder="Enter a specific statement to verify, or leave blank to use the headline.", lines=2 ) analyze_button = gr.Button( "🔍 ANALYSE NEWS", variant="primary", elem_id="analyze-button" ) gr.Markdown("---") # ARTICLE article_output = gr.Markdown( label="Extracted News Article" ) # DASHBOARD gr.Markdown("## 📊 Verification Dashboard") verdict_output = gr.HTML() graph_output = gr.Plot( label="Evidence Percentage Graph" ) # SOURCES gr.Markdown("## 🌐 News Sources") source_output = gr.HTML() # AI REPORT gr.Markdown("## 🧠 AI Analysis Report") summary_output = gr.Markdown() # FOOTER gr.HTML("""
TRUTHLENS AI

An educational artificial intelligence project.

AI-assisted results are not guaranteed factual conclusions.
""") # BUTTON ACTION analyze_button.click( fn=run_analysis, inputs=[ url_input, claim_input ], outputs=[ article_output, verdict_output, graph_output, source_output, summary_output ] ) demo.queue().launch()