TruthLens-AI / app.py
AIproject2008's picture
Update app.py
2e90048 verified
Raw History Blame Contribute Delete
17.9 kB
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 "<h3>No matching sources found.</h3>"
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"""
<div style="
background:#1F2937;
border:1px solid #374151;
border-left:5px solid {color};
border-radius:14px;
padding:18px;
margin-bottom:14px;
">
<div style="
display:flex;
justify-content:space-between;
flex-wrap:wrap;
gap:10px;
">
<b style="color:white;font-size:16px">
{publisher}
</b>
<b style="color:{color}">
{item['verdict'].upper()}
</b>
</div>
<p style="color:#D1D5DB">
{title}
</p>
<small style="color:#9CA3AF">
{domain} |
Model confidence: {item['confidence']}%
</small>
<br><br>
<a href="{link}"
target="_blank"
rel="noopener noreferrer"
style="color:#60A5FA">
Read original report β†’
</a>
</div>
"""
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]}",
"<h2 style='color:#F59E0B'>NON-VERIFIABLE</h2>"
"<p>No matching independent reports were found.</p>",
create_graph(percentages),
"<p>No sources found.</p>",
"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"""
<div style="
background:#111827;
border:1px solid #374151;
padding:24px;
border-radius:16px;
">
<p style="color:#9CA3AF">
FINAL VERDICT
</p>
<h1 style="
color:{color};
font-size:32px;
">
{verdict}
</h1>
<p style="color:#D1D5DB">
{explanation}
</p>
<hr style="border-color:#374151">
<div style="
display:flex;
justify-content:space-around;
flex-wrap:wrap;
gap:18px;
">
<div>
<h2 style="color:#22C55E">
{percentages['Supporting']}%
</h2>
<small>Supporting</small>
</div>
<div>
<h2 style="color:#EF4444">
{percentages['Contradicting']}%
</h2>
<small>Contradicting</small>
</div>
<div>
<h2 style="color:#F59E0B">
{percentages['Unclear']}%
</h2>
<small>Unclear</small>
</div>
</div>
</div>
"""
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"""
<h2 style="color:#F59E0B">
NON-VERIFIABLE
</h2>
<p>{error_message}</p>
""",
create_graph({
"Supporting": 0,
"Contradicting": 0,
"Unclear": 100
}),
"<p>Source information unavailable.</p>",
"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("""
<div id="main-header">
<div style="font-size:48px">
πŸ”Ž
</div>
<h1>
TRUTHLENS AI
</h1>
<p style="
color:#94A3B8;
font-size:17px;
">
BEYOND HEADLINES. TOWARDS TRUTH.
</p>
<p style="color:#64748B">
AI-powered multi-source news verification
</p>
</div>
""")
# 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("""
<div style="
text-align:center;
padding:25px;
color:#64748B;
">
<b>TRUTHLENS AI</b>
<p>
An educational artificial intelligence project.
</p>
<small>
AI-assisted results are not guaranteed factual conclusions.
</small>
</div>
""")
# 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()