Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,47 +10,50 @@ 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 |
-
|
| 21 |
-
|
| 22 |
-
# Check if search results are available and extract them
|
| 23 |
-
if hasattr(response.candidates[0].grounding_metadata, 'search_entry_point'):
|
| 24 |
-
search_results = response.candidates[0].grounding_metadata.search_entry_point
|
| 25 |
-
links_html = "\n".join([f"<a href='{result.url}' target='_blank'>{result.title}</a>" for result in search_results.results])
|
| 26 |
-
else:
|
| 27 |
-
links_html = "No search results found."
|
| 28 |
-
|
| 29 |
-
return ai_response, links_html
|
| 30 |
else:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
contents=question,
|
| 35 |
-
)
|
| 36 |
-
return response.text, "Web search not used."
|
| 37 |
except Exception as e:
|
| 38 |
-
return f"Error: {str(e)}", "
|
| 39 |
|
|
|
|
| 40 |
with gr.Blocks() as app:
|
| 41 |
with gr.Row():
|
| 42 |
-
question_input = gr.Textbox(label="Ask a Question"
|
| 43 |
-
web_search_checkbox = gr.Checkbox(label="
|
| 44 |
with gr.Row():
|
| 45 |
submit_button = gr.Button("Submit")
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
submit_button.click(
|
| 51 |
-
|
| 52 |
-
inputs=[question_input, web_search_checkbox],
|
| 53 |
-
outputs=[
|
| 54 |
)
|
| 55 |
|
| 56 |
-
app.launch(
|
|
|
|
| 10 |
|
| 11 |
def google_search_query(question, use_web_search):
|
| 12 |
try:
|
| 13 |
+
# Generate the AI response without web search initially
|
| 14 |
+
response = client.models.generate_content(
|
| 15 |
+
model=MODEL_ID,
|
| 16 |
+
contents=question,
|
| 17 |
+
)
|
| 18 |
+
ai_response = response.text # AI response as plain text
|
| 19 |
+
|
| 20 |
if use_web_search:
|
| 21 |
+
# If user selects web search, redefine the tool and generate response with web search
|
| 22 |
google_search_tool = Tool(google_search=GoogleSearch())
|
| 23 |
response = client.models.generate_content(
|
| 24 |
model=MODEL_ID,
|
| 25 |
contents=question,
|
| 26 |
config=GenerateContentConfig(tools=[google_search_tool]),
|
| 27 |
)
|
| 28 |
+
search_results = response.candidates[0].grounding_metadata.search_entry_point.rendered_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
else:
|
| 30 |
+
search_results = "Web search not used."
|
| 31 |
+
|
| 32 |
+
return ai_response, search_results
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
+
return f"Error: {str(e)}", ""
|
| 35 |
|
| 36 |
+
# Gradio Interface using Chatbot and conditional web search
|
| 37 |
with gr.Blocks() as app:
|
| 38 |
with gr.Row():
|
| 39 |
+
question_input = gr.Textbox(lines=2, label="Ask a Question")
|
| 40 |
+
web_search_checkbox = gr.Checkbox(label="Enhance with Web Search", value=False)
|
| 41 |
with gr.Row():
|
| 42 |
submit_button = gr.Button("Submit")
|
| 43 |
+
chatbot = gr.Chatbot(label="Chatbot Responses")
|
| 44 |
+
|
| 45 |
+
def update_chatbot(question, use_web_search, chat_log):
|
| 46 |
+
ai_response, search_results = google_search_query(question, use_web_search)
|
| 47 |
+
chat_log.append(("You", question))
|
| 48 |
+
chat_log.append(("AI", ai_response))
|
| 49 |
+
if use_web_search:
|
| 50 |
+
chat_log.append(("AI + Web Search", search_results))
|
| 51 |
+
return chat_log
|
| 52 |
+
|
| 53 |
submit_button.click(
|
| 54 |
+
fn=update_chatbot,
|
| 55 |
+
inputs=[question_input, web_search_checkbox, chatbot],
|
| 56 |
+
outputs=[chatbot]
|
| 57 |
)
|
| 58 |
|
| 59 |
+
app.launch()
|