Firoj112 commited on
Commit
ba4099c
·
verified ·
1 Parent(s): ed55bdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -31
app.py CHANGED
@@ -13,6 +13,7 @@ from PIL import Image
13
  from datetime import datetime
14
  import tempfile
15
  import helium
 
16
  from smolagents import CodeAgent, LiteLLMModel
17
  from smolagents.agents import ActionStep
18
  from tools.search_item_ctrl_f import SearchItemCtrlFTool
@@ -115,40 +116,57 @@ for idx, tool in enumerate(tools):
115
  except Exception as e:
116
  logger.error(f"Failed to register tool {idx}: {str(e)}")
117
 
118
- # Initialize model
119
- model = LiteLLMModel("gemini/gemini-2.0-flash", api_key=default_gemini_api_key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  # Initialize agent
122
- agent = CodeAgent(
123
- model=model,
124
- tools=tools,
125
- max_steps=20,
126
- verbosity_level=2,
127
- prompt_templates=prompt_templates,
128
- step_callbacks=[save_screenshot],
129
- additional_authorized_imports=[
130
- "helium",
131
- "unicodedata",
132
- "stat",
133
- "datetime",
134
- "random",
135
- "pandas",
136
- "itertools",
137
- "math",
138
- "statistics",
139
- "queue",
140
- "time",
141
- "collections",
142
- "re",
143
- "cv2",
144
- "numpy"
145
- ]
146
- )
147
- agent.python_executor("from helium import *")
 
 
 
148
 
149
- # Launch Gradio UI
150
  try:
151
- GradioUI(agent).launch()
152
  except KeyboardInterrupt:
153
  driver.quit()
154
- logger.info("Chrome driver closed on exit.")
 
13
  from datetime import datetime
14
  import tempfile
15
  import helium
16
+ import gradio as gr
17
  from smolagents import CodeAgent, LiteLLMModel
18
  from smolagents.agents import ActionStep
19
  from tools.search_item_ctrl_f import SearchItemCtrlFTool
 
116
  except Exception as e:
117
  logger.error(f"Failed to register tool {idx}: {str(e)}")
118
 
119
+ # Initialize model with API key handling
120
+ def initialize_model(gemini_api_key=None):
121
+ # Log the API key being used (mask last 4 chars for security)
122
+ if gemini_api_key and gemini_api_key.strip():
123
+ logger.debug(f"Using user-provided API key: {gemini_api_key[:-4] + '****'}")
124
+ else:
125
+ logger.debug(f"Using default API key: {default_gemini_api_key[:-4] + '****' if default_gemini_api_key else 'None'}")
126
+
127
+ try:
128
+ api_key = gemini_api_key.strip() if gemini_api_key and gemini_api_key.strip() else default_gemini_api_key
129
+ if not api_key:
130
+ raise ValueError("No valid API key provided and GOOGLE_API_KEY not set in environment")
131
+ return LiteLLMModel("gemini/gemini-2.0-flash", api_key=api_key)
132
+ except Exception as e:
133
+ logger.error(f"Failed to initialize LiteLLMModel: {str(e)}")
134
+ raise gr.Error(f"API Key Error: {str(e)}", duration=5)
135
 
136
  # Initialize agent
137
+ def initialize_agent(gemini_api_key=None):
138
+ model = initialize_model(gemini_api_key)
139
+ agent = CodeAgent(
140
+ model=model,
141
+ tools=tools,
142
+ max_steps=20,
143
+ verbosity_level=2,
144
+ prompt_templates=prompt_templates,
145
+ step_callbacks=[save_screenshot],
146
+ additional_authorized_imports=[
147
+ "helium",
148
+ "unicodedata",
149
+ "stat",
150
+ "datetime",
151
+ "random",
152
+ "pandas",
153
+ "itertools",
154
+ "math",
155
+ "statistics",
156
+ "queue",
157
+ "time",
158
+ "collections",
159
+ "re",
160
+ "cv2",
161
+ "numpy"
162
+ ]
163
+ )
164
+ agent.python_executor("from helium import *")
165
+ return agent
166
 
167
+ # Launch Gradio UI with API key support
168
  try:
169
+ GradioUI(initialize_agent).launch()
170
  except KeyboardInterrupt:
171
  driver.quit()
172
+ logger.info("Chrome driver closed on exit.")