Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,16 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from google import genai
|
|
|
4 |
import requests
|
5 |
import markdownify
|
6 |
from urllib.robotparser import RobotFileParser
|
7 |
from urllib.parse import urlparse
|
8 |
|
|
|
|
|
|
|
|
|
9 |
# Configure browser tools
|
10 |
def can_crawl_url(url: str, user_agent: str = "*") -> bool:
|
11 |
"""Check robots.txt permissions for a URL"""
|
@@ -29,62 +34,86 @@ def load_page(url: str) -> str:
|
|
29 |
except Exception as e:
|
30 |
return f"Error loading page: {str(e)}"
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
def generate_response(user_input):
|
37 |
try:
|
38 |
if not user_input.strip():
|
39 |
return "Please enter a valid query"
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
contents=[user_input]
|
45 |
-
)
|
46 |
-
|
47 |
-
result = response.text
|
48 |
-
|
49 |
-
# If the response suggests needing web data, handle it
|
50 |
-
if "search" in result.lower() or "look up" in result.lower():
|
51 |
-
# Extract URL if mentioned
|
52 |
-
url = None
|
53 |
-
if "http" in result:
|
54 |
-
url = result.split("http")[1].split()[0]
|
55 |
-
url = "http" + url.split('"')[0].split("'")[0].split()[0]
|
56 |
-
|
57 |
-
if url:
|
58 |
-
page_content = load_page(url)
|
59 |
-
follow_up = f"Here's content from {url}:\n\n{page_content[:2000]}...\n\nPlease summarize this information."
|
60 |
-
response = client.models.generate_content(
|
61 |
-
model=MODEL,
|
62 |
-
contents=[follow_up]
|
63 |
-
)
|
64 |
-
result = response.text
|
65 |
-
else:
|
66 |
-
result += "\n\n(I can't perform live searches but you can ask me to visit specific URLs)"
|
67 |
-
|
68 |
-
# Handle code execution requests
|
69 |
-
if "code" in result.lower() or "calculate" in result.lower():
|
70 |
-
code_prompt = f"""Please provide only the Python code to solve this problem:
|
71 |
{user_input}
|
72 |
-
The code should
|
73 |
-
|
|
|
|
|
|
|
|
|
74 |
model=MODEL,
|
75 |
contents=[code_prompt]
|
76 |
)
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
except Exception as e:
|
82 |
return f"Error: {str(e)}"
|
83 |
|
84 |
# Create Gradio interface
|
85 |
with gr.Blocks(title="Gemini AI Assistant") as demo:
|
86 |
gr.Markdown("# 🚀 Gemini AI Assistant")
|
87 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
with gr.Row():
|
90 |
input_box = gr.Textbox(
|
@@ -93,28 +122,37 @@ with gr.Blocks(title="Gemini AI Assistant") as demo:
|
|
93 |
lines=3,
|
94 |
max_lines=10
|
95 |
)
|
96 |
-
output_box = gr.Markdown(
|
97 |
-
label="Response",
|
98 |
-
elem_classes="markdown-output"
|
99 |
-
)
|
100 |
|
101 |
with gr.Row():
|
102 |
submit_btn = gr.Button("Submit", variant="primary")
|
103 |
clear_btn = gr.Button("Clear")
|
104 |
|
|
|
|
|
|
|
|
|
|
|
105 |
def clear():
|
106 |
-
return ["", ""]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
submit_btn.click(
|
109 |
-
fn=
|
110 |
-
inputs=input_box,
|
111 |
outputs=output_box
|
112 |
)
|
113 |
|
114 |
clear_btn.click(
|
115 |
fn=clear,
|
116 |
inputs=[],
|
117 |
-
outputs=[input_box, output_box]
|
118 |
)
|
119 |
|
120 |
if __name__ == "__main__":
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from google import genai
|
4 |
+
from google.genai import types
|
5 |
import requests
|
6 |
import markdownify
|
7 |
from urllib.robotparser import RobotFileParser
|
8 |
from urllib.parse import urlparse
|
9 |
|
10 |
+
# Initialize Gemini client
|
11 |
+
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
12 |
+
MODEL = "gemini-2.5-pro-exp-03-25"
|
13 |
+
|
14 |
# Configure browser tools
|
15 |
def can_crawl_url(url: str, user_agent: str = "*") -> bool:
|
16 |
"""Check robots.txt permissions for a URL"""
|
|
|
34 |
except Exception as e:
|
35 |
return f"Error loading page: {str(e)}"
|
36 |
|
37 |
+
def execute_code(code: str) -> str:
|
38 |
+
"""Execute Python code safely"""
|
39 |
+
try:
|
40 |
+
# Create a dictionary to hold the execution context
|
41 |
+
local_vars = {}
|
42 |
+
# Execute the code in the isolated context
|
43 |
+
exec(code, {}, local_vars)
|
44 |
+
# Return any output variables
|
45 |
+
return str(local_vars.get('result', 'Code executed (no output captured)'))
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error executing code: {str(e)}"
|
48 |
|
49 |
+
def generate_response(user_input, mode):
|
50 |
try:
|
51 |
if not user_input.strip():
|
52 |
return "Please enter a valid query"
|
53 |
|
54 |
+
if mode == "code":
|
55 |
+
# Generate code for the problem
|
56 |
+
code_prompt = f"""Please provide complete Python code to solve this problem:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
{user_input}
|
58 |
+
The code should:
|
59 |
+
1. Be executable as-is
|
60 |
+
2. Store the final result in a variable called 'result'
|
61 |
+
3. Include any necessary imports"""
|
62 |
+
|
63 |
+
response = client.models.generate_content(
|
64 |
model=MODEL,
|
65 |
contents=[code_prompt]
|
66 |
)
|
67 |
+
|
68 |
+
if response.text:
|
69 |
+
code = response.text.strip()
|
70 |
+
# Execute the generated code
|
71 |
+
execution_result = execute_code(code)
|
72 |
+
return f"Generated Python code:\n```python\n{code}\n```\n\nExecution result:\n{execution_result}"
|
73 |
+
return "No code was generated for this request."
|
74 |
|
75 |
+
elif mode == "search":
|
76 |
+
# Simulate search functionality
|
77 |
+
search_prompt = f"""You are an AI assistant with web search capabilities.
|
78 |
+
For the query: "{user_input}"
|
79 |
+
1. Determine if this requires current/live information
|
80 |
+
2. If yes, suggest specific URLs to visit
|
81 |
+
3. If no, answer directly"""
|
82 |
+
|
83 |
+
response = client.models.generate_content(
|
84 |
+
model=MODEL,
|
85 |
+
contents=[search_prompt]
|
86 |
+
)
|
87 |
+
|
88 |
+
if "http" in response.text:
|
89 |
+
# Extract URL if mentioned
|
90 |
+
url = response.text.split("http")[1].split()[0]
|
91 |
+
url = "http" + url.split('"')[0].split("'")[0].split()[0]
|
92 |
+
page_content = load_page(url)
|
93 |
+
return f"Information from {url}:\n\n{page_content[:2000]}..."
|
94 |
+
return response.text
|
95 |
|
96 |
+
else: # default mode
|
97 |
+
response = client.models.generate_content(
|
98 |
+
model=MODEL,
|
99 |
+
contents=[user_input]
|
100 |
+
)
|
101 |
+
return response.text
|
102 |
+
|
103 |
except Exception as e:
|
104 |
return f"Error: {str(e)}"
|
105 |
|
106 |
# Create Gradio interface
|
107 |
with gr.Blocks(title="Gemini AI Assistant") as demo:
|
108 |
gr.Markdown("# 🚀 Gemini AI Assistant")
|
109 |
+
gr.Markdown("Code Execution • Information Search")
|
110 |
+
|
111 |
+
with gr.Row():
|
112 |
+
mode_radio = gr.Radio(
|
113 |
+
["Default", "Code Execution", "Search Mode"],
|
114 |
+
label="Mode",
|
115 |
+
value="Default"
|
116 |
+
)
|
117 |
|
118 |
with gr.Row():
|
119 |
input_box = gr.Textbox(
|
|
|
122 |
lines=3,
|
123 |
max_lines=10
|
124 |
)
|
|
|
|
|
|
|
|
|
125 |
|
126 |
with gr.Row():
|
127 |
submit_btn = gr.Button("Submit", variant="primary")
|
128 |
clear_btn = gr.Button("Clear")
|
129 |
|
130 |
+
output_box = gr.Markdown(
|
131 |
+
label="Response",
|
132 |
+
elem_classes="markdown-output"
|
133 |
+
)
|
134 |
+
|
135 |
def clear():
|
136 |
+
return ["", "", "Default"]
|
137 |
+
|
138 |
+
def process_input(user_input, mode):
|
139 |
+
mode_map = {
|
140 |
+
"Default": "default",
|
141 |
+
"Code Execution": "code",
|
142 |
+
"Search Mode": "search"
|
143 |
+
}
|
144 |
+
return generate_response(user_input, mode_map.get(mode, "default"))
|
145 |
|
146 |
submit_btn.click(
|
147 |
+
fn=process_input,
|
148 |
+
inputs=[input_box, mode_radio],
|
149 |
outputs=output_box
|
150 |
)
|
151 |
|
152 |
clear_btn.click(
|
153 |
fn=clear,
|
154 |
inputs=[],
|
155 |
+
outputs=[input_box, output_box, mode_radio]
|
156 |
)
|
157 |
|
158 |
if __name__ == "__main__":
|