Rahatara commited on
Commit
2aefee2
·
verified ·
1 Parent(s): 658354f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -8,40 +8,42 @@ API_KEY = os.getenv("GOOGLE_API_KEY") # Ensure to set this in Hugging Face Secr
8
  client = genai.Client(api_key=API_KEY)
9
  MODEL_ID = "gemini-2.0-flash-exp" # 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
- # Parse and format search results to separate links
28
- link_list = "\n".join([f"<a href='{entry.url}' target='_blank'>{entry.title}</a>" for entry in response.candidates[0].grounding_metadata.search_entry_point.entries])
29
- return ai_response, link_list
30
  except Exception as e:
31
  return f"Error: {str(e)}", "No results found."
32
 
33
- # Gradio Interface
34
- app = gr.Interface(
35
- fn=google_search_query,
36
- inputs=gr.Textbox(lines=2, label="Ask a Question"),
37
- outputs=[
38
- gr.Textbox(label="AI Response"),
39
- gr.HTML(label="Search Results"),
40
- ],
41
- title="Google Search with Gemini AI",
42
- description="Ask a question, and the AI will use Google search tools to provide an answer along with contextual search results.",
43
- theme="huggingface",
44
- )
 
 
 
45
 
46
- if __name__ == "__main__":
47
- app.launch(share=True)
 
8
  client = genai.Client(api_key=API_KEY)
9
  MODEL_ID = "gemini-2.0-flash-exp" # Replace with your desired model ID
10
 
11
+ def google_search_query(question, use_web_search):
12
  try:
13
+ if use_web_search:
14
+ google_search_tool = Tool(google_search=GoogleSearch())
15
+ response = client.models.generate_content(
16
+ model=MODEL_ID,
17
+ contents=question,
18
+ config=GenerateContentConfig(tools=[google_search_tool]),
19
+ )
20
+ ai_response = response.text # AI response as plain text
21
+ link_list = "\n".join([f"<a href='{entry.url}' target='_blank'>{entry.title}</a>" for entry in response.candidates[0].grounding_metadata.search_entry_point.entries])
22
+ return ai_response, link_list
23
+ else:
24
+ # Return response without web search
25
+ response = client.models.generate_content(
26
+ model=MODEL_ID,
27
+ contents=question,
28
+ )
29
+ return response.text, "Web search not used."
30
  except Exception as e:
31
  return f"Error: {str(e)}", "No results found."
32
 
33
+ with gr.Blocks() as app:
34
+ with gr.Row():
35
+ question_input = gr.Textbox(label="Ask a Question", lines=2)
36
+ web_search_checkbox = gr.Checkbox(label="Enable Web Search", value=False)
37
+ with gr.Row():
38
+ submit_button = gr.Button("Submit")
39
+ with gr.Row():
40
+ ai_response_output = gr.Textbox(label="AI Response")
41
+ search_results_output = gr.HTML(label="Search Results")
42
+
43
+ submit_button.click(
44
+ google_search_query,
45
+ inputs=[question_input, web_search_checkbox],
46
+ outputs=[ai_response_output, search_results_output]
47
+ )
48
 
49
+ app.launch(share=True)