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") # Set in Hugging Face Secrets | |
| client = genai.Client(api_key=API_KEY) | |
| MODEL_ID = "gemini-2.0-flash" | |
| # Custom Pink CSS | |
| pink_css = """ | |
| .gradio-container { | |
| background: linear-gradient(135deg, #ffe4ec, #fff0f6); | |
| } | |
| button { | |
| background-color: #ec4899 !important; | |
| color: white !important; | |
| border-radius: 14px !important; | |
| font-weight: bold; | |
| } | |
| button:hover { | |
| background-color: #db2777 !important; | |
| } | |
| textarea, input { | |
| border: 2px solid #f472b6 !important; | |
| border-radius: 10px !important; | |
| } | |
| """ | |
| def fact_check_claim(claim, use_web_search): | |
| try: | |
| prompt = f""" | |
| You are a professional fact-checking assistant. | |
| Classify the following claim as one of: | |
| - True | |
| - False | |
| - Misleading | |
| - Unverifiable | |
| Then provide a short explanation. | |
| Claim: | |
| {claim} | |
| """ | |
| # Base AI analysis | |
| response = client.models.generate_content( | |
| model=MODEL_ID, | |
| contents=prompt, | |
| ) | |
| verdict = response.text | |
| if use_web_search: | |
| google_search_tool = Tool(google_search=GoogleSearch()) | |
| response = client.models.generate_content( | |
| model=MODEL_ID, | |
| contents=prompt, | |
| config=GenerateContentConfig(tools=[google_search_tool]), | |
| ) | |
| evidence = response.candidates[0].grounding_metadata.search_entry_point.rendered_content | |
| else: | |
| evidence = "Web evidence was not requested." | |
| return verdict, evidence | |
| except Exception as e: | |
| return f"Error: {str(e)}", "" | |
| # Gradio UI | |
| with gr.Blocks( | |
| theme=gr.themes.Soft( | |
| primary_hue="pink", | |
| secondary_hue="rose", | |
| radius_size="lg", | |
| ), | |
| css=pink_css | |
| ) as app: | |
| gr.Markdown("# AI Fact Checker (Gemini 2.0)") | |
| with gr.Row(): | |
| chatbot = gr.Chatbot(label="Fact-Checking History", height=350) | |
| with gr.Row(): | |
| claim_input = gr.Textbox( | |
| lines=3, | |
| label="Enter a claim to verify", | |
| placeholder="Example: Vitamin C prevents colds." | |
| ) | |
| web_search_checkbox = gr.Checkbox( | |
| label="Verify with Web Evidence", | |
| value=True | |
| ) | |
| with gr.Row(): | |
| submit_button = gr.Button("Check Claim 🔍") | |
| def update_chatbot(claim, use_web_search, chat_log): | |
| verdict, evidence = fact_check_claim(claim, use_web_search) | |
| chat_log.append(("Claim", claim)) | |
| chat_log.append(("AI Verdict", verdict)) | |
| if use_web_search: | |
| chat_log.append(("Evidence", evidence)) | |
| return chat_log | |
| submit_button.click( | |
| fn=update_chatbot, | |
| inputs=[claim_input, web_search_checkbox, chatbot], | |
| outputs=[chatbot], | |
| ) | |
| app.launch() | |