Yasmeen7 commited on
Commit
508a8e9
·
verified ·
1 Parent(s): ea45a8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -18
app.py CHANGED
@@ -4,41 +4,53 @@ from google import genai
4
  from google.genai.types import GenerateContentConfig, GoogleSearch, Tool
5
 
6
  # Initialize GenAI Client
7
- API_KEY = os.getenv("GOOGLE_API_KEY") # Ensure to set this in Hugging Face Secrets
8
  client = genai.Client(api_key=API_KEY)
9
- MODEL_ID = "gemini-2.0-flash" # Replace with your desired model ID
10
 
11
- def google_search_query(question):
12
  try:
13
- # Define the Google Search Tool
14
  google_search_tool = Tool(google_search=GoogleSearch())
15
-
16
- # Generate the response
 
 
 
 
 
 
 
 
 
 
 
 
17
  response = client.models.generate_content(
18
  model=MODEL_ID,
19
- contents=question,
20
  config=GenerateContentConfig(tools=[google_search_tool]),
21
  )
22
 
23
- # Extract AI response and search results
24
- ai_response = response.text # AI response as plain text
25
- search_results = response.candidates[0].grounding_metadata.search_entry_point.rendered_content
 
26
 
27
- return ai_response, search_results
28
  except Exception as e:
29
  return f"Error: {str(e)}", ""
30
 
31
  # Gradio Interface
32
  app = gr.Interface(
33
- fn=google_search_query,
34
- inputs=gr.Textbox(lines=2, label="Ask a Question"),
35
  outputs=[
36
- gr.Textbox(label="AI Response"),
37
- gr.HTML(label="Search Results"),
38
  ],
39
- title="Google Search with Gemini AI",
40
- description="Ask a question, and the AI will use Google search tools to provide an answer along with contextual search results.",
41
  )
42
 
43
  if __name__ == "__main__":
44
- app.launch(share=True)
 
4
  from google.genai.types import GenerateContentConfig, GoogleSearch, Tool
5
 
6
  # Initialize GenAI Client
7
+ API_KEY = os.getenv("GOOGLE_API_KEY") # Hugging Face Secrets
8
  client = genai.Client(api_key=API_KEY)
9
+ MODEL_ID = "gemini-2.0-flash"
10
 
11
+ def fact_check_claim(claim):
12
  try:
13
+ # Google Search Tool
14
  google_search_tool = Tool(google_search=GoogleSearch())
15
+
16
+ prompt = f"""
17
+ You are a fact-checking assistant.
18
+ Analyze the following claim and decide whether it is:
19
+ - True
20
+ - False
21
+ - Misleading
22
+
23
+ Then give a short explanation based on reliable sources.
24
+
25
+ Claim:
26
+ {claim}
27
+ """
28
+
29
  response = client.models.generate_content(
30
  model=MODEL_ID,
31
+ contents=prompt,
32
  config=GenerateContentConfig(tools=[google_search_tool]),
33
  )
34
 
35
+ ai_analysis = response.text
36
+ sources_html = response.candidates[0].grounding_metadata.search_entry_point.rendered_content
37
+
38
+ return ai_analysis, sources_html
39
 
 
40
  except Exception as e:
41
  return f"Error: {str(e)}", ""
42
 
43
  # Gradio Interface
44
  app = gr.Interface(
45
+ fn=fact_check_claim,
46
+ inputs=gr.Textbox(lines=3, label="Enter a claim or news statement"),
47
  outputs=[
48
+ gr.Textbox(label="AI Fact Check Result"),
49
+ gr.HTML(label="Sources & Evidence"),
50
  ],
51
+ title="AI Fact Checker with Gemini",
52
+ description="Enter a claim or news statement. The AI will verify it using Google search and provide evidence-based analysis.",
53
  )
54
 
55
  if __name__ == "__main__":
56
+ app.launch(share=True)