TRUTHLENS AI
BEYOND HEADLINES. TOWARDS TRUTH.
AI-powered multi-source news verification
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 "
{title}
{domain} | Model confidence: {item['confidence']}%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
{explanation}
{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("""BEYOND HEADLINES. TOWARDS TRUTH.
AI-powered multi-source news verification
An educational artificial intelligence project.
AI-assisted results are not guaranteed factual conclusions.