Update app.py
Browse files
app.py
CHANGED
@@ -32,33 +32,36 @@ def load_page(url: str) -> str:
|
|
32 |
|
33 |
# Initialize Gemini client
|
34 |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
35 |
-
MODEL = "gemini-2.
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
"
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
]
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
SYSTEM_INSTRUCTION = """You are an AI assistant
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
Use the most appropriate tool for each query."""
|
62 |
|
63 |
def format_response(parts):
|
64 |
"""Format response parts with proper Markdown formatting"""
|
@@ -72,66 +75,36 @@ def format_response(parts):
|
|
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):
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
)
|
84 |
-
)
|
85 |
-
|
86 |
-
# Initial request
|
87 |
-
response = chat.send_message(user_input)
|
88 |
-
|
89 |
-
# Process all response parts
|
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 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
result = load_page(**fn.args)
|
101 |
-
chat.send_message(
|
102 |
-
types.Content(
|
103 |
-
parts=[
|
104 |
-
types.Part(
|
105 |
-
function_response=types.FunctionResponse(
|
106 |
-
name=fn.name,
|
107 |
-
id=fn.id,
|
108 |
-
response={"result": result}
|
109 |
-
)
|
110 |
-
)
|
111 |
-
]
|
112 |
-
)
|
113 |
-
)
|
114 |
-
# Get final response after tool execution
|
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 |
|
@@ -157,8 +130,7 @@ with gr.Blocks(
|
|
157 |
submit_btn.click(
|
158 |
fn=generate_response,
|
159 |
inputs=input_box,
|
160 |
-
outputs=output_box
|
161 |
-
queue=True
|
162 |
)
|
163 |
|
164 |
clear_btn.click(
|
|
|
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 |
+
TOOL_CONFIGS = {
|
38 |
+
"web": [
|
39 |
+
types.Tool(
|
40 |
+
function_declarations=[
|
41 |
+
types.FunctionDeclaration(
|
42 |
+
name="load_page",
|
43 |
+
description="Load webpage content as markdown",
|
44 |
+
parameters={
|
45 |
+
"type": "object",
|
46 |
+
"properties": {
|
47 |
+
"url": {"type": "string", "description": "Full URL to load"}
|
48 |
+
},
|
49 |
+
"required": ["url"]
|
50 |
+
}
|
51 |
+
)
|
52 |
+
]
|
53 |
+
),
|
54 |
+
types.Tool(google_search=types.GoogleSearch())
|
55 |
+
],
|
56 |
+
"code": [
|
57 |
+
types.Tool(code_execution=types.ToolCodeExecution())
|
58 |
+
]
|
59 |
+
}
|
60 |
|
61 |
+
SYSTEM_INSTRUCTION = """You are an AI assistant. Choose appropriate tools based on the query:
|
62 |
+
- Use web tools for real-time information
|
63 |
+
- Use code execution for calculations/analysis
|
64 |
+
- Never combine search with code execution"""
|
|
|
65 |
|
66 |
def format_response(parts):
|
67 |
"""Format response parts with proper Markdown formatting"""
|
|
|
75 |
formatted.append(f"**Result**:\n```\n{part.code_execution_result.output}\n```")
|
76 |
return "\n\n".join(formatted)
|
77 |
|
78 |
+
def select_tools(query: str) -> list:
|
79 |
+
"""Select appropriate tools based on query content"""
|
80 |
+
if any(keyword in query.lower() for keyword in ["plot", "calculate", "simulate", "algorithm"]):
|
81 |
+
return TOOL_CONFIGS["code"]
|
82 |
+
return TOOL_CONFIGS["web"]
|
83 |
+
|
84 |
def generate_response(user_input):
|
85 |
+
try:
|
86 |
+
if not user_input.strip():
|
87 |
+
raise ValueError("Please enter a valid query")
|
88 |
+
|
89 |
+
selected_tools = select_tools(user_input)
|
90 |
+
|
91 |
+
response = client.models.generate_content(
|
92 |
+
model=MODEL,
|
93 |
+
contents=[user_input],
|
94 |
+
config=types.GenerateContentConfig(
|
95 |
+
temperature=0.7,
|
96 |
+
tools=selected_tools,
|
97 |
+
system_instruction=SYSTEM_INSTRUCTION
|
98 |
+
)
|
99 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
return format_response(response.candidates[0].content.parts)
|
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("Web β’ Code β’ Data Analysis")
|
110 |
|
|
|
130 |
submit_btn.click(
|
131 |
fn=generate_response,
|
132 |
inputs=input_box,
|
133 |
+
outputs=output_box
|
|
|
134 |
)
|
135 |
|
136 |
clear_btn.click(
|