Spaces:
No application file
No application file
Update app.py
Browse files
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 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
except Exception as e:
|
31 |
return f"Error: {str(e)}", "No results found."
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
gr.
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
-
|
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)
|
|