Ubik80's picture
fixed search_tool method
70d18fa
raw
history blame
1.85 kB
import gradio as gr
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
# Inizializza il modello e l'agente
search_tool = DuckDuckGoSearchTool()
model = HfApiModel(model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/")
agent = CodeAgent(model=model, tools=[search_tool])
# Lista delle fonti affidabili
trusted_sources = [
"bbc.com", "reuters.com", "apnews.com", "nytimes.com",
"cnn.com", "forbes.com", "theguardian.com", "npr.org"
]
# Funzione per classificare le fonti
def classify_sources(search_results):
categorized_results = []
for result in search_results:
source_domain = result['url'].split('/')[2] # Estrai il dominio
if source_domain in trusted_sources:
status = "βœ… Trusted Source"
else:
status = "⚠️ Unverified Source"
categorized_results.append(f"{status}: [{result['title']}]({result['url']}) ({source_domain})")
return "\n".join(categorized_results)
# Funzione principale per analizzare le notizie
def detect_fake_news(news_text):
# Cerca informazioni sulla notizia (CORRETTO!)
search_results = search_tool.invoke(news_text)
# Analizza le fonti
sources_classification = classify_sources(search_results)
# Interroga il modello LLM
response = agent.run(f"Check if this news is true or fake: {news_text}")
# Restituisci il risultato finale con analisi fonti
return f"{response}\n\nπŸ” **Source Analysis:**\n{sources_classification}"
# Configurazione di Gradio
interface = gr.Interface(
fn=detect_fake_news,
inputs="text",
outputs="text",
title="πŸ•΅οΈ Fake News Detective",
description="Paste a news article or statement and get a credibility analysis"
)
# Avvio dell'app
if __name__ == "__main__":
interface.launch()