Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from google import genai | |
| from google.genai.types import GenerateContentConfig, GoogleSearch, Tool | |
| # Initialize GenAI Client | |
| API_KEY = os.getenv("GOOGLE_API_KEY") # Hugging Face Secrets | |
| client = genai.Client(api_key=API_KEY) | |
| MODEL_ID = "gemini-2.0-flash" | |
| def fact_check_claim(claim): | |
| try: | |
| # Google Search Tool | |
| google_search_tool = Tool(google_search=GoogleSearch()) | |
| prompt = f""" | |
| You are a fact-checking assistant. | |
| Analyze the following claim and decide whether it is: | |
| - True | |
| - False | |
| - Misleading | |
| Then give a short explanation based on reliable sources. | |
| Claim: | |
| {claim} | |
| """ | |
| response = client.models.generate_content( | |
| model=MODEL_ID, | |
| contents=prompt, | |
| config=GenerateContentConfig(tools=[google_search_tool]), | |
| ) | |
| ai_analysis = response.text | |
| sources_html = response.candidates[0].grounding_metadata.search_entry_point.rendered_content | |
| return ai_analysis, sources_html | |
| except Exception as e: | |
| return f"Error: {str(e)}", "" | |
| # Gradio Interface | |
| app = gr.Interface( | |
| fn=fact_check_claim, | |
| inputs=gr.Textbox(lines=3, label="Enter a claim or news statement"), | |
| outputs=[ | |
| gr.Textbox(label="AI Fact Check Result"), | |
| gr.HTML(label="Sources & Evidence"), | |
| ], | |
| title="AI Fact Checker with Gemini", | |
| description="Enter a claim or news statement. The AI will verify it using Google search and provide evidence-based analysis.", | |
| ) | |
| if __name__ == "__main__": | |
| app.launch(share=True) | |