File size: 2,915 Bytes
683b7d8
 
 
 
 
 
e6a187a
683b7d8
e6a187a
683b7d8
e6a187a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
683b7d8
e6a187a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
985a108
 
e6a187a
985a108
e6a187a
985a108
2aefee2
 
 
 
e6a187a
2aefee2
 
e6a187a
2aefee2
e6a187a
 
 
985a108
683b7d8
985a108
683b7d8
e6a187a
 
 
 
 
 
 
 
 
 
 
 
2aefee2
e6a187a
 
7d673ae
e6a187a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
985a108
e6a187a
985a108
e6a187a
2aefee2
985a108
e6a187a
 
2aefee2
683b7d8
985a108
e6a187a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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()