Update app.py
Browse files
app.py
CHANGED
@@ -1,101 +1,101 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
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 |
-
|
9 |
-
# Load environment variables from .env file
|
10 |
-
load_dotenv()
|
11 |
-
|
12 |
-
|
13 |
-
# Constants
|
14 |
-
KG_INDEX_PATH="KG/news_category_index.faiss"
|
15 |
-
KG_DATASET_PATH="KG/News_Category_Dataset_v3.json"
|
16 |
-
SEARCH_API_KEY = os.getenv("SEARCH_API_KEY")
|
17 |
-
SEARCH_BASE_URL = os.getenv("SEARCH_BASE_URL")
|
18 |
-
SEARCH_MODEL = os.getenv("SEARCH_MODEL")
|
19 |
-
|
20 |
-
# Function to process input and evaluate truthfulness
|
21 |
-
def evaluate_news(news_input):
|
22 |
-
# Display loading message
|
23 |
-
yield "**Processing... Please wait while we analyze the information.** ⏳"
|
24 |
-
|
25 |
-
# Handle URL input
|
26 |
-
if news_input.startswith("http"):
|
27 |
-
try:
|
28 |
-
article = Article(news_input)
|
29 |
-
article.download()
|
30 |
-
article.parse()
|
31 |
-
news_text = article.title + ". " + article.text
|
32 |
-
except Exception as e:
|
33 |
-
yield f"**Error processing the URL:** {str(e)}"
|
34 |
-
return
|
35 |
-
else:
|
36 |
-
# Direct text input
|
37 |
-
news_text = news_input
|
38 |
-
|
39 |
-
try:
|
40 |
-
# Retrieve relevant info from FAISS knowledge graph
|
41 |
-
kg_content = search_kg(query=news_text, index_path=KG_INDEX_PATH, dataset_path=KG_DATASET_PATH)
|
42 |
-
|
43 |
-
# Search online for additional context
|
44 |
-
online_search_results = search_online(
|
45 |
-
query=news_text,
|
46 |
-
api_key=SEARCH_API_KEY,
|
47 |
-
base_url=SEARCH_BASE_URL,
|
48 |
-
model=SEARCH_MODEL
|
49 |
-
)
|
50 |
-
|
51 |
-
# Combine context from KG and online search
|
52 |
-
context =
|
53 |
-
|
54 |
-
# Calculate truth score
|
55 |
-
truth_score = calculate_truthfulness_score(info=news_text, context=context)
|
56 |
-
|
57 |
-
# Generate explanation based on score
|
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."
|
61 |
-
elif truth_score > 0.4:
|
62 |
-
status = "uncertain"
|
63 |
-
recommendation = "This information might be partially true, but additional investigation is required before accepting it as fact."
|
64 |
-
else:
|
65 |
-
status = "unlikely to be true"
|
66 |
-
recommendation = "It is recommended to verify this information through multiple reliable sources before trusting it."
|
67 |
-
|
68 |
-
# Final Output
|
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)}"
|
76 |
-
|
77 |
-
|
78 |
-
# Gradio Interface
|
79 |
-
with gr.Blocks() as demo:
|
80 |
-
gr.Markdown("# 📰 EchoTruth: Amplifying Authenticity in Live Broadcasts")
|
81 |
-
gr.Markdown("### Enter a piece of news or a URL below to validate its authenticity.")
|
82 |
-
|
83 |
-
# Input options
|
84 |
-
with gr.Row():
|
85 |
-
input_box = gr.Textbox(placeholder="Enter news text or URL here...", label="Input News or URL")
|
86 |
-
|
87 |
-
# Output display
|
88 |
-
output_box = gr.Markdown()
|
89 |
-
|
90 |
-
# Submit button
|
91 |
-
submit_btn = gr.Button("Check Truthfulness")
|
92 |
-
|
93 |
-
# Trigger function with spinner
|
94 |
-
submit_btn.click(
|
95 |
-
fn=evaluate_news,
|
96 |
-
inputs=[input_box],
|
97 |
-
outputs=[output_box]
|
98 |
-
)
|
99 |
-
|
100 |
-
# Launch Gradio App
|
101 |
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
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 |
+
|
9 |
+
# Load environment variables from .env file
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
|
13 |
+
# Constants
|
14 |
+
KG_INDEX_PATH="KG/news_category_index.faiss"
|
15 |
+
KG_DATASET_PATH="KG/News_Category_Dataset_v3.json"
|
16 |
+
SEARCH_API_KEY = os.getenv("SEARCH_API_KEY")
|
17 |
+
SEARCH_BASE_URL = os.getenv("SEARCH_BASE_URL")
|
18 |
+
SEARCH_MODEL = os.getenv("SEARCH_MODEL")
|
19 |
+
|
20 |
+
# Function to process input and evaluate truthfulness
|
21 |
+
def evaluate_news(news_input):
|
22 |
+
# Display loading message
|
23 |
+
yield "**Processing... Please wait while we analyze the information.** ⏳"
|
24 |
+
|
25 |
+
# Handle URL input
|
26 |
+
if news_input.startswith("http"):
|
27 |
+
try:
|
28 |
+
article = Article(news_input)
|
29 |
+
article.download()
|
30 |
+
article.parse()
|
31 |
+
news_text = article.title + ". " + article.text
|
32 |
+
except Exception as e:
|
33 |
+
yield f"**Error processing the URL:** {str(e)}"
|
34 |
+
return
|
35 |
+
else:
|
36 |
+
# Direct text input
|
37 |
+
news_text = news_input
|
38 |
+
|
39 |
+
try:
|
40 |
+
# Retrieve relevant info from FAISS knowledge graph
|
41 |
+
kg_content = search_kg(query=news_text, index_path=KG_INDEX_PATH, dataset_path=KG_DATASET_PATH)
|
42 |
+
|
43 |
+
# Search online for additional context
|
44 |
+
online_search_results = search_online(
|
45 |
+
query=news_text,
|
46 |
+
api_key=SEARCH_API_KEY,
|
47 |
+
base_url=SEARCH_BASE_URL,
|
48 |
+
model=SEARCH_MODEL
|
49 |
+
)
|
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 |
+
# Generate explanation based on score
|
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."
|
61 |
+
elif truth_score > 0.4:
|
62 |
+
status = "uncertain"
|
63 |
+
recommendation = "This information might be partially true, but additional investigation is required before accepting it as fact."
|
64 |
+
else:
|
65 |
+
status = "unlikely to be true"
|
66 |
+
recommendation = "It is recommended to verify this information through multiple reliable sources before trusting it."
|
67 |
+
|
68 |
+
# Final Output
|
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)}"
|
76 |
+
|
77 |
+
|
78 |
+
# Gradio Interface
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
gr.Markdown("# 📰 EchoTruth: Amplifying Authenticity in Live Broadcasts")
|
81 |
+
gr.Markdown("### Enter a piece of news or a URL below to validate its authenticity.")
|
82 |
+
|
83 |
+
# Input options
|
84 |
+
with gr.Row():
|
85 |
+
input_box = gr.Textbox(placeholder="Enter news text or URL here...", label="Input News or URL")
|
86 |
+
|
87 |
+
# Output display
|
88 |
+
output_box = gr.Markdown()
|
89 |
+
|
90 |
+
# Submit button
|
91 |
+
submit_btn = gr.Button("Check Truthfulness")
|
92 |
+
|
93 |
+
# Trigger function with spinner
|
94 |
+
submit_btn.click(
|
95 |
+
fn=evaluate_news,
|
96 |
+
inputs=[input_box],
|
97 |
+
outputs=[output_box]
|
98 |
+
)
|
99 |
+
|
100 |
+
# Launch Gradio App
|
101 |
demo.launch()
|