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 analizzare le notizie def detect_fake_news(news_text): # Esegui una ricerca sul web relativa al testo della notizia search_results = search_tool(news_text) # Classifica le fonti sources_classification = classify_sources(search_results) # Analizza la notizia utilizzando l'agente response = agent.run(f"Check if this news is true or fake: {news_text}") # Combina l'analisi dell'agente con la classificazione delle fonti final_response = f"{response}\n\nšŸ” Source Analysis:\n{sources_classification}" return final_response # Funzione per classificare le fonti def classify_sources(search_results): categorized_results = [] for result in search_results: source_domain = result['href'].split('/')[2] # Estrai il dominio dal link if source_domain in trusted_sources: status = "āœ… Trusted Source" else: status = "āš ļø Unverified Source" categorized_results.append(f"{status}: [{result['title']}]({result['href']}) ({source_domain})") return "\n".join(categorized_results) # 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()