Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from newspaper import Article
|
|
| 3 |
from modules.online_search import search_online
|
| 4 |
from modules.validation import calculate_truthfulness_score
|
| 5 |
from modules.knowledge_graph import search_kg
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
import os
|
| 8 |
from concurrent.futures import ThreadPoolExecutor
|
|
@@ -18,7 +19,7 @@ SEARCH_BASE_URL = os.getenv("SEARCH_BASE_URL")
|
|
| 18 |
SEARCH_MODEL = os.getenv("SEARCH_MODEL")
|
| 19 |
|
| 20 |
# Initialize ThreadPoolExecutor
|
| 21 |
-
executor = ThreadPoolExecutor(max_workers=
|
| 22 |
|
| 23 |
# Function to process input and evaluate truthfulness
|
| 24 |
def evaluate_news(news_input):
|
|
@@ -49,12 +50,13 @@ def evaluate_news(news_input):
|
|
| 49 |
online_search_results = future_online.result()
|
| 50 |
|
| 51 |
# Combine context from KG and online search
|
| 52 |
-
context = online_search_results['message_content'] + '\n' + kg_content
|
|
|
|
| 53 |
|
| 54 |
# Calculate truth score
|
| 55 |
truth_score = calculate_truthfulness_score(info=news_text, context=context)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
if truth_score > 0.7:
|
| 59 |
status = "likely true"
|
| 60 |
recommendation = "You can reasonably trust this information, but further verification is always recommended for critical decisions."
|
|
@@ -65,11 +67,30 @@ def evaluate_news(news_input):
|
|
| 65 |
status = "unlikely to be true"
|
| 66 |
recommendation = "It is recommended to verify this information through multiple reliable sources before trusting it."
|
| 67 |
|
| 68 |
-
#
|
| 69 |
result = f"**News**: \"{news_text[:300]}...\"\n\n"
|
| 70 |
result += f"**Truthfulness Score**: {truth_score:.2f} (**{status.capitalize()}**)\n\n"
|
| 71 |
result += f"**Analysis**: {recommendation}\n\n"
|
| 72 |
-
yield result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
yield f"**Error occurred while processing the input:** {str(e)}"
|
|
@@ -77,25 +98,32 @@ def evaluate_news(news_input):
|
|
| 77 |
|
| 78 |
# Gradio Interface
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
-
gr.Markdown("# 📰 EchoTruth:
|
| 81 |
-
gr.Markdown("### Enter a piece of news or a URL below
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
with gr.Row():
|
| 85 |
-
input_box = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
# Output display
|
| 88 |
-
output_box = gr.Markdown()
|
| 89 |
-
|
| 90 |
-
# Submit button
|
| 91 |
submit_btn = gr.Button("Check Truthfulness")
|
| 92 |
|
| 93 |
-
|
|
|
|
| 94 |
submit_btn.click(
|
| 95 |
fn=evaluate_news,
|
| 96 |
inputs=[input_box],
|
| 97 |
outputs=[output_box]
|
| 98 |
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
| 3 |
from modules.online_search import search_online
|
| 4 |
from modules.validation import calculate_truthfulness_score
|
| 5 |
from modules.knowledge_graph import search_kg
|
| 6 |
+
from modules.generate_explanation import generate_explanation # Import the explanation generator
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
import os
|
| 9 |
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
| 19 |
SEARCH_MODEL = os.getenv("SEARCH_MODEL")
|
| 20 |
|
| 21 |
# Initialize ThreadPoolExecutor
|
| 22 |
+
executor = ThreadPoolExecutor(max_workers=3) # Increased workers to accommodate explanation task
|
| 23 |
|
| 24 |
# Function to process input and evaluate truthfulness
|
| 25 |
def evaluate_news(news_input):
|
|
|
|
| 50 |
online_search_results = future_online.result()
|
| 51 |
|
| 52 |
# Combine context from KG and online search
|
| 53 |
+
context = online_search_results['message_content'] + '\n' + kg_content + '\n' + 'Device set to use cpu'
|
| 54 |
+
# print(context) # Debug log
|
| 55 |
|
| 56 |
# Calculate truth score
|
| 57 |
truth_score = calculate_truthfulness_score(info=news_text, context=context)
|
| 58 |
|
| 59 |
+
# Determine truthfulness status and recommendation
|
| 60 |
if truth_score > 0.7:
|
| 61 |
status = "likely true"
|
| 62 |
recommendation = "You can reasonably trust this information, but further verification is always recommended for critical decisions."
|
|
|
|
| 67 |
status = "unlikely to be true"
|
| 68 |
recommendation = "It is recommended to verify this information through multiple reliable sources before trusting it."
|
| 69 |
|
| 70 |
+
# Display initial result with score
|
| 71 |
result = f"**News**: \"{news_text[:300]}...\"\n\n"
|
| 72 |
result += f"**Truthfulness Score**: {truth_score:.2f} (**{status.capitalize()}**)\n\n"
|
| 73 |
result += f"**Analysis**: {recommendation}\n\n"
|
| 74 |
+
yield result # Immediately display score and recommendation
|
| 75 |
+
|
| 76 |
+
# Generate explanation asynchronously
|
| 77 |
+
future_explanation = executor.submit(generate_explanation, news_text, context, truth_score)
|
| 78 |
+
|
| 79 |
+
# Add explanation and sources once available
|
| 80 |
+
explanation = future_explanation.result() # Wait for explanation result
|
| 81 |
+
if explanation:
|
| 82 |
+
result += f"**Explanation**: {explanation}\n\n" # Append explanation
|
| 83 |
+
|
| 84 |
+
# Add sources from the online search results (top 5 sources)
|
| 85 |
+
sources = online_search_results.get('sources', [])
|
| 86 |
+
if sources:
|
| 87 |
+
result += "\n**Sources**:\n"
|
| 88 |
+
# Ensure we only show up to 5 sources
|
| 89 |
+
for i, source in enumerate(sources[:5]):
|
| 90 |
+
result += f"{i + 1}. {source}\n"
|
| 91 |
+
result += "\n*Please make sure to do your own research for more confirmation and to cross-check the information.*"
|
| 92 |
+
|
| 93 |
+
yield result # Update UI with explanation and sources
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
yield f"**Error occurred while processing the input:** {str(e)}"
|
|
|
|
| 98 |
|
| 99 |
# Gradio Interface
|
| 100 |
with gr.Blocks() as demo:
|
| 101 |
+
gr.Markdown("# 📰 EchoTruth: Verify News Authenticity in Real-Time")
|
| 102 |
+
gr.Markdown("### Enter a piece of news or a URL below, and we'll help you evaluate its truthfulness using advanced AI.")
|
| 103 |
+
|
| 104 |
+
gr.Markdown("### How to Use:")
|
| 105 |
+
gr.Markdown("1. Paste a news article link or type in the text.")
|
| 106 |
+
gr.Markdown("2. Click **Check Truthfulness** to analyze the authenticity of the news.")
|
| 107 |
+
gr.Markdown("3. Get your truthfulness score along with explanations and sources.")
|
| 108 |
+
|
| 109 |
with gr.Row():
|
| 110 |
+
input_box = gr.Textbox(
|
| 111 |
+
placeholder="Enter news text or URL here... (e.g., https://example.com)",
|
| 112 |
+
label="Input News or URL",
|
| 113 |
+
lines=5
|
| 114 |
+
)
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
submit_btn = gr.Button("Check Truthfulness")
|
| 117 |
|
| 118 |
+
output_box = gr.Markdown()
|
| 119 |
+
|
| 120 |
submit_btn.click(
|
| 121 |
fn=evaluate_news,
|
| 122 |
inputs=[input_box],
|
| 123 |
outputs=[output_box]
|
| 124 |
)
|
| 125 |
|
| 126 |
+
gr.Markdown("### **About EchoTruth**")
|
| 127 |
+
gr.Markdown("EchoTruth uses AI to help users verify news authenticity in real-time. We recommend checking multiple sources before making decisions based on news.")
|
| 128 |
+
|
| 129 |
+
demo.launch()
|