| import gradio as gr |
| from newspaper import Article |
| from modules.online_search import search_online |
| from modules.validation import calculate_truthfulness_score |
| from modules.knowledge_graph import search_kg |
| from modules.generate_explanation import generate_explanation |
| from dotenv import load_dotenv |
| import os |
| from concurrent.futures import ThreadPoolExecutor |
| from modules.record import DatabaseComponent |
|
|
| |
| load_dotenv() |
|
|
| |
| db = None |
| try: |
| db = DatabaseComponent() |
| except Exception as e: |
| print(f"[ERROR] Database connection failed: {str(e)}") |
|
|
| |
| executor = ThreadPoolExecutor(max_workers=3) |
|
|
| |
| KG_INDEX_PATH = "KG/news_category_index.faiss" |
| KG_DATASET_PATH = "KG/News_Category_Dataset_v3.json" |
| SEARCH_API_KEY = os.getenv("SEARCH_API_KEY") |
| SEARCH_BASE_URL = os.getenv("SEARCH_BASE_URL") |
| SEARCH_MODEL = os.getenv("SEARCH_MODEL") |
|
|
|
|
| |
| def evaluate_news(news_input): |
| if not news_input.strip(): |
| yield "**⚠️ Warning:** Input cannot be empty. Please enter news text or a URL." |
| return |
|
|
| yield "**Processing... Please wait.** ⏳" |
|
|
| |
| if news_input.startswith("http"): |
| try: |
| article = Article(news_input) |
| article.download() |
| article.parse() |
| news_text = article.title + ". " + article.text |
| except Exception as e: |
| yield f"**Error processing the URL:** {str(e)}" |
| return |
| else: |
| news_text = news_input |
|
|
| try: |
| |
| future_kg = executor.submit(search_kg, news_text, KG_INDEX_PATH, KG_DATASET_PATH) |
| future_online = executor.submit(search_online, news_text, SEARCH_API_KEY, SEARCH_BASE_URL, SEARCH_MODEL) |
|
|
| |
| kg_content = future_kg.result() |
| online_search_results = future_online.result() |
|
|
| |
| citations = online_search_results.get("citations", []) |
| first_citation = citations[0] if citations else "N/A" |
|
|
| |
| context = online_search_results['message_content'] + '\n' + kg_content + '\n' + 'Device set to use cpu' |
|
|
| |
| truth_score = calculate_truthfulness_score(info=news_text, context=context) |
| truth_percentage = truth_score * 100 |
|
|
| |
| if truth_score > 0.7: |
| status = f"**{truth_percentage:.0f}% chances to be true** - This news is likely true." |
| elif truth_score > 0.4: |
| status = f"**{truth_percentage:.0f}% chances to be true** - This news is uncertain. Please verify further." |
| else: |
| status = f"**{truth_percentage:.0f}% chances to be true** - This news is unlikely to be true. Proceed with caution." |
|
|
| |
| if db is not None: |
| db.save_news_verification(news_text[:100], truth_score, first_citation) |
|
|
| |
| result = f"**News:** \"{news_text[:300]}...\"\n\n" |
| result += f"**Truthfulness Score:** {truth_score:.2f} ({status})\n\n" |
|
|
| yield result |
|
|
| |
| future_explanation = executor.submit(generate_explanation, news_text, context, truth_score) |
| explanation = future_explanation.result() |
|
|
| if explanation: |
| result += f"**Explanation:** {explanation}\n\n" |
|
|
| |
| if citations: |
| result += "\n**Sources & References:**\n" |
| for i, source in enumerate(citations[:5]): |
| result += f"{i + 1}. [{source}]({source})\n" |
|
|
| yield result |
|
|
| except Exception as e: |
| yield f"**Error:** {str(e)}" |
|
|
|
|
| |
| def fetch_dashboard_data(): |
| if db is None: |
| return "**⚠️ Database unavailable. Recent verification records cannot be displayed.**" |
|
|
| total_news = db.get_total_news_count() |
| last_10_news = db.get_last_10_news() |
|
|
| |
| dashboard_info = f"**Total News Verified:** {total_news}\n\n" |
|
|
| if last_10_news: |
| table = "| # | News Title | Score (%) | Date Verified | Citation |\n" |
| table += "|---|------------|-----------|--------------|----------|\n" |
|
|
| for i, news in enumerate(last_10_news, start=1): |
| truth_percentage = news['score'] * 100 |
| citation = f"[Source]({news['citation']})" if news['citation'] != "N/A" else "N/A" |
| table += f"| {i} | {news['title'][:50]}... | {truth_percentage:.0f}% | {news['timestamp']} | {citation} |\n" |
|
|
| dashboard_info += table |
| else: |
| dashboard_info += "_No records found._" |
|
|
| return dashboard_info |
|
|
|
|
| |
| with gr.Blocks(css=""" |
| .gradio-container { font-family: 'Georgia', serif; font-size: 16px; } |
| h1, h2, h3 { font-family: 'Times New Roman', serif; } |
| table { width: 100%; border-collapse: collapse; } |
| th, td { padding: 10px; border: 1px solid #ddd; text-align: left; } |
| """) as demo: |
| with gr.Tabs() as tabs: |
| with gr.Tab("🔍 Verify News"): |
| gr.Markdown("# 📰 EchoTruth: News Verification") |
| gr.Markdown(""" |
| **How it Works:** |
| - Enter a news article **or** a URL. |
| - Click **Check Truthfulness**. |
| - Get a **truth score**, an explanation, and references. |
| """) |
|
|
| input_box = gr.Textbox(placeholder="Paste news text or URL...", label="News Input", lines=5) |
| submit_btn = gr.Button("Check Truthfulness") |
| output_box = gr.Markdown() |
| submit_btn.click(fn=evaluate_news, inputs=[input_box], outputs=[output_box]) |
|
|
| with gr.Tab("📊 Dashboard") as dashboard_tab: |
| gr.Markdown("# 📊 Verification Dashboard") |
| dashboard_output = gr.Markdown("_Click 'Refresh Data' to load latest records._") |
| refresh_btn = gr.Button("🔄 Refresh Data") |
| refresh_btn.click(fn=fetch_dashboard_data, inputs=[], outputs=[dashboard_output]) |
|
|
| gr.Markdown("### **About EchoTruth**") |
| gr.Markdown("EchoTruth uses AI to help users verify news authenticity in real-time.") |
|
|
| demo.launch() |