|
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 dotenv import load_dotenv
|
|
import os
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
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):
|
|
|
|
yield "**Processing... Please wait while we analyze the information.** ⏳"
|
|
|
|
|
|
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:
|
|
|
|
kg_content = search_kg(query=news_text, index_path=KG_INDEX_PATH, dataset_path=KG_DATASET_PATH)
|
|
|
|
|
|
online_search_results = search_online(
|
|
query=news_text,
|
|
api_key=SEARCH_API_KEY,
|
|
base_url=SEARCH_BASE_URL,
|
|
model=SEARCH_MODEL
|
|
)
|
|
|
|
|
|
context = kg_content + '\n' + online_search_results['message_content']
|
|
|
|
|
|
truth_score = calculate_truthfulness_score(info=news_text, context=context)
|
|
|
|
|
|
if truth_score > 0.7:
|
|
status = "likely true"
|
|
recommendation = "You can reasonably trust this information, but further verification is always recommended for critical decisions."
|
|
elif truth_score > 0.4:
|
|
status = "uncertain"
|
|
recommendation = "This information might be partially true, but additional investigation is required before accepting it as fact."
|
|
else:
|
|
status = "unlikely to be true"
|
|
recommendation = "It is recommended to verify this information through multiple reliable sources before trusting it."
|
|
|
|
|
|
result = f"**News**: \"{news_text[:300]}...\"\n\n"
|
|
result += f"**Truthfulness Score**: {truth_score:.2f} (**{status.capitalize()}**)\n\n"
|
|
result += f"**Analysis**: {recommendation}\n\n"
|
|
yield result
|
|
|
|
except Exception as e:
|
|
yield f"**Error occurred while processing the input:** {str(e)}"
|
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
|
gr.Markdown("# 📰 EchoTruth: Amplifying Authenticity in Live Broadcasts")
|
|
gr.Markdown("### Enter a piece of news or a URL below to validate its authenticity.")
|
|
|
|
|
|
with gr.Row():
|
|
input_box = gr.Textbox(placeholder="Enter news text or URL here...", label="Input News or URL")
|
|
|
|
|
|
output_box = gr.Markdown()
|
|
|
|
|
|
submit_btn = gr.Button("Check Truthfulness")
|
|
|
|
|
|
submit_btn.click(
|
|
fn=evaluate_news,
|
|
inputs=[input_box],
|
|
outputs=[output_box]
|
|
)
|
|
|
|
|
|
demo.launch() |