Athspi commited on
Commit
e106c9a
Β·
verified Β·
1 Parent(s): 76cc36e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -45
app.py CHANGED
@@ -1,10 +1,9 @@
1
- import base64
2
  import os
3
  import gradio as gr
4
- import requests
5
- import markdownify
6
  from google import genai
7
  from google.genai import types
 
 
8
  from urllib.robotparser import RobotFileParser
9
  from urllib.parse import urlparse
10
 
@@ -31,9 +30,10 @@ def load_page(url: str) -> str:
31
  except Exception as e:
32
  return f"Error loading page: {str(e)}"
33
 
34
- # Configure Gemini client
35
  client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
36
  MODEL = "gemini-2.0-flash"
 
37
  TOOLS = [
38
  types.Tool(
39
  function_declarations=[
@@ -54,19 +54,14 @@ TOOLS = [
54
  types.Tool(code_execution=types.ToolCodeExecution())
55
  ]
56
 
57
- SYSTEM_INSTRUCTION = """You are an AI assistant with multiple capabilities:
58
- 1. Web browsing through search and direct page access
59
- 2. Code execution for calculations, simulations, and data analysis
60
- 3. File I/O operations for data processing
61
-
62
- Use this decision tree:
63
- - For factual questions: Use search
64
- - For time-sensitive data: Use browser tool
65
- - For math/data processing: Generate and execute code
66
- - Always explain your reasoning"""
67
 
68
- def format_code_response(parts):
69
- """Format code execution parts for Markdown display"""
70
  formatted = []
71
  for part in parts:
72
  if part.text:
@@ -74,9 +69,7 @@ def format_code_response(parts):
74
  if part.executable_code:
75
  formatted.append(f"```python\n{part.executable_code.code}\n```")
76
  if part.code_execution_result:
77
- formatted.append(f"**Result**:\n{part.code_execution_result.output}")
78
- if part.inline_data:
79
- formatted.append(f"![Generated Image](data:image/png;base64,{base64.b64encode(part.inline_data.data).decode()})")
80
  return "\n\n".join(formatted)
81
 
82
  def generate_response(user_input):
@@ -97,7 +90,7 @@ def generate_response(user_input):
97
  response_parts = []
98
  for part in response.candidates[0].content.parts:
99
  response_parts.append(part)
100
- full_response = format_code_response(response_parts)
101
  yield full_response
102
 
103
  # Handle function calls
@@ -122,24 +115,35 @@ def generate_response(user_input):
122
  final_response = chat.send_message("")
123
  for final_part in final_response.candidates[0].content.parts:
124
  response_parts.append(final_part)
125
- full_response = format_code_response(response_parts)
126
  yield full_response
127
 
128
- # Gradio Interface
129
- with gr.Blocks(title="Gemini 2.0 AI Assistant") as demo:
130
- gr.Markdown("# πŸš€ Gemini 2.0 AI Assistant")
131
- gr.Markdown("Web Access β€’ Code Execution β€’ Data Analysis")
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  with gr.Row():
134
  input_box = gr.Textbox(
135
  label="Your Query",
136
- placeholder="Ask anything or request code execution...",
137
  lines=3,
138
- max_lines=10,
139
- autofocus=True
140
  )
141
  output_box = gr.Markdown(
142
- label="Assistant Response",
143
  elem_classes="markdown-output"
144
  )
145
 
@@ -164,19 +168,4 @@ with gr.Blocks(title="Gemini 2.0 AI Assistant") as demo:
164
  )
165
 
166
  if __name__ == "__main__":
167
- demo.launch(
168
- server_name="0.0.0.0",
169
- server_port=7860,
170
- css="""
171
- .markdown-output {
172
- padding: 20px;
173
- border-radius: 5px;
174
- background: #f9f9f9;
175
- }
176
- .markdown-output code {
177
- background: #f3f3f3;
178
- padding: 2px 5px;
179
- border-radius: 3px;
180
- }
181
- """
182
- )
 
 
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
 
 
30
  except Exception as e:
31
  return f"Error loading page: {str(e)}"
32
 
33
+ # Initialize Gemini client
34
  client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
35
  MODEL = "gemini-2.0-flash"
36
+
37
  TOOLS = [
38
  types.Tool(
39
  function_declarations=[
 
54
  types.Tool(code_execution=types.ToolCodeExecution())
55
  ]
56
 
57
+ SYSTEM_INSTRUCTION = """You are an AI assistant with:
58
+ 1. Web browsing capabilities
59
+ 2. Code execution for calculations
60
+ 3. Data analysis skills
61
+ Use the most appropriate tool for each query."""
 
 
 
 
 
62
 
63
+ def format_response(parts):
64
+ """Format response parts with proper Markdown formatting"""
65
  formatted = []
66
  for part in parts:
67
  if part.text:
 
69
  if part.executable_code:
70
  formatted.append(f"```python\n{part.executable_code.code}\n```")
71
  if part.code_execution_result:
72
+ formatted.append(f"**Result**:\n```\n{part.code_execution_result.output}\n```")
 
 
73
  return "\n\n".join(formatted)
74
 
75
  def generate_response(user_input):
 
90
  response_parts = []
91
  for part in response.candidates[0].content.parts:
92
  response_parts.append(part)
93
+ full_response = format_response(response_parts)
94
  yield full_response
95
 
96
  # Handle function calls
 
115
  final_response = chat.send_message("")
116
  for final_part in final_response.candidates[0].content.parts:
117
  response_parts.append(final_part)
118
+ full_response = format_response(response_parts)
119
  yield full_response
120
 
121
+ # Create Gradio interface
122
+ with gr.Blocks(
123
+ title="Gemini AI Assistant",
124
+ css=""".markdown-output {
125
+ padding: 20px;
126
+ border-radius: 5px;
127
+ background: #f9f9f9;
128
+ }
129
+ .markdown-output code {
130
+ background: #f3f3f3;
131
+ padding: 2px 5px;
132
+ border-radius: 3px;
133
+ }"""
134
+ ) as demo:
135
+ gr.Markdown("# πŸš€ Gemini AI Assistant")
136
+ gr.Markdown("Web β€’ Code β€’ Data Analysis")
137
 
138
  with gr.Row():
139
  input_box = gr.Textbox(
140
  label="Your Query",
141
+ placeholder="Ask anything...",
142
  lines=3,
143
+ max_lines=10
 
144
  )
145
  output_box = gr.Markdown(
146
+ label="Response",
147
  elem_classes="markdown-output"
148
  )
149
 
 
168
  )
169
 
170
  if __name__ == "__main__":
171
+ demo.launch(server_name="0.0.0.0", server_port=7860)