Spaces:
Running on Zero
Running on Zero
File size: 17,916 Bytes
2e90048 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 | 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() |