Pariamdz commited on
Commit
c724cd9
·
verified ·
1 Parent(s): 7e8500d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -54
app.py CHANGED
@@ -1,11 +1,62 @@
1
  import os
2
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
3
  import datetime
4
  import requests
5
  import pytz
6
  import yaml
7
- from tools.final_answer import FinalAnswerTool
8
- from Gradio_UI import GradioUI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # -------------------------
11
  # Local custom tools
@@ -91,42 +142,49 @@ def simple_image_description(prompt: str) -> str:
91
  # -------------------------
92
  # Required final answer tool
93
  # -------------------------
94
- try:
95
- final_answer = FinalAnswerTool()
96
- print("✓ Final answer tool loaded")
97
- except Exception as e:
98
- print(f"✗ Error loading final answer tool: {e}")
99
- # Create a fallback
100
- @tool
101
- def final_answer(answer: str) -> str:
102
- """Provide the final answer to the user."""
103
- return answer
104
 
105
  # -------------------------
106
  # Model setup with error handling
107
  # -------------------------
108
- try:
109
- model = HfApiModel(
110
- max_tokens=2096,
111
- temperature=0.5,
112
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
113
- custom_role_conversions=None,
114
- )
115
- print("✓ Model loaded successfully")
116
- except Exception as e:
117
- print(f"✗ Error loading model: {e}")
118
- # Try a fallback model
119
  try:
120
- model = HfApiModel(
121
- max_tokens=1024,
122
  temperature=0.5,
123
- model_id='microsoft/DialoGPT-medium',
124
- custom_role_conversions=None,
125
  )
126
- print("✓ Fallback model loaded")
127
- except Exception as e2:
128
- print(f"✗ Fallback model also failed: {e2}")
129
- raise e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  # -------------------------
132
  # Pre-made tools from Hub / smolagents
@@ -196,7 +254,7 @@ except Exception as e:
196
  # Build tools list
197
  # -------------------------
198
  tools_list = [
199
- final_answer,
200
  calculator,
201
  get_current_time_in_timezone,
202
  get_weather,
@@ -213,17 +271,22 @@ print(f"✓ Loaded {len(tools_list)} tools")
213
  # Agent with tools registered
214
  # -------------------------
215
  try:
216
- agent = CodeAgent(
217
- model=model,
218
- tools=tools_list,
219
- max_steps=10,
220
- verbosity_level=2,
221
- grammar=None,
222
- planning_interval=None,
223
- name=None,
224
- description=None,
225
- prompt_templates=prompt_templates
226
- )
 
 
 
 
 
227
  print("✓ Agent created successfully")
228
  except Exception as e:
229
  print(f"✗ Error creating agent: {e}")
@@ -232,25 +295,32 @@ except Exception as e:
232
  # -------------------------
233
  # Launch UI
234
  # -------------------------
235
- try:
236
- ui = GradioUI(agent)
237
- print("✓ UI created successfully")
238
- ui.launch()
239
- except Exception as e:
240
- print(f"✗ Error launching UI: {e}")
 
 
 
 
241
  # Fallback: create a simple Gradio interface
 
242
  import gradio as gr
243
 
244
  def chat_function(message):
245
  try:
246
- return agent.run(message)
 
247
  except Exception as e:
248
  return f"Error: {str(e)}"
249
 
250
  demo = gr.Interface(
251
  fn=chat_function,
252
- inputs=gr.Textbox(label="Your message"),
253
  outputs=gr.Textbox(label="Agent response"),
254
- title="AI Agent"
 
255
  )
256
- demo.launch()
 
1
  import os
 
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
+
7
+ # Try different import patterns for smolagents
8
+ try:
9
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, load_tool, tool
10
+ print("✓ Core smolagents imports successful")
11
+ except ImportError as e:
12
+ print(f"✗ Error importing from smolagents: {e}")
13
+ raise
14
+
15
+ # Try to import the model class with different names
16
+ model = None
17
+ try:
18
+ from smolagents import HfApiModel
19
+ model_class = HfApiModel
20
+ print("✓ HfApiModel imported")
21
+ except ImportError:
22
+ try:
23
+ from smolagents.models import HfApiModel
24
+ model_class = HfApiModel
25
+ print("✓ HfApiModel imported from models")
26
+ except ImportError:
27
+ try:
28
+ from smolagents import HuggingFaceModel
29
+ model_class = HuggingFaceModel
30
+ print("✓ HuggingFaceModel imported")
31
+ except ImportError:
32
+ try:
33
+ from smolagents.models import HuggingFaceModel
34
+ model_class = HuggingFaceModel
35
+ print("✓ HuggingFaceModel imported from models")
36
+ except ImportError:
37
+ print("✗ No suitable model class found, will use default")
38
+ model_class = None
39
+
40
+ # Try importing final answer tool
41
+ try:
42
+ from tools.final_answer import FinalAnswerTool
43
+ print("✓ FinalAnswerTool imported")
44
+ except ImportError as e:
45
+ print(f"! FinalAnswerTool not found: {e}")
46
+ # Create a simple fallback
47
+ @tool
48
+ def final_answer(answer: str) -> str:
49
+ """Provide the final answer to the user."""
50
+ return answer
51
+ FinalAnswerTool = None
52
+
53
+ # Try importing Gradio UI
54
+ try:
55
+ from Gradio_UI import GradioUI
56
+ print("✓ GradioUI imported")
57
+ except ImportError as e:
58
+ print(f"! GradioUI not found: {e}")
59
+ GradioUI = None
60
 
61
  # -------------------------
62
  # Local custom tools
 
142
  # -------------------------
143
  # Required final answer tool
144
  # -------------------------
145
+ if FinalAnswerTool:
146
+ try:
147
+ final_answer_tool = FinalAnswerTool()
148
+ print("✓ Final answer tool loaded")
149
+ except Exception as e:
150
+ print(f"✗ Error loading final answer tool: {e}")
151
+ final_answer_tool = final_answer
152
+ else:
153
+ final_answer_tool = final_answer
 
154
 
155
  # -------------------------
156
  # Model setup with error handling
157
  # -------------------------
158
+ if model_class:
 
 
 
 
 
 
 
 
 
 
159
  try:
160
+ model = model_class(
161
+ max_tokens=2096,
162
  temperature=0.5,
163
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
 
164
  )
165
+ print("✓ Model loaded successfully")
166
+ except Exception as e:
167
+ print(f"✗ Error loading model: {e}")
168
+ # Try a smaller/simpler model
169
+ try:
170
+ model = model_class(
171
+ max_tokens=1024,
172
+ temperature=0.5,
173
+ model_id='microsoft/DialoGPT-medium',
174
+ )
175
+ print("✓ Fallback model loaded")
176
+ except Exception as e2:
177
+ print(f"✗ Fallback model also failed: {e2}")
178
+ # Try without specific model
179
+ try:
180
+ model = model_class()
181
+ print("✓ Default model loaded")
182
+ except Exception as e3:
183
+ print(f"✗ Default model failed: {e3}")
184
+ model = None
185
+ else:
186
+ print("! No model class available - will try to create agent without explicit model")
187
+ model = None
188
 
189
  # -------------------------
190
  # Pre-made tools from Hub / smolagents
 
254
  # Build tools list
255
  # -------------------------
256
  tools_list = [
257
+ final_answer_tool,
258
  calculator,
259
  get_current_time_in_timezone,
260
  get_weather,
 
271
  # Agent with tools registered
272
  # -------------------------
273
  try:
274
+ if model:
275
+ agent = CodeAgent(
276
+ model=model,
277
+ tools=tools_list,
278
+ max_steps=10,
279
+ verbosity_level=2,
280
+ prompt_templates=prompt_templates
281
+ )
282
+ else:
283
+ # Try creating agent without explicit model
284
+ agent = CodeAgent(
285
+ tools=tools_list,
286
+ max_steps=10,
287
+ verbosity_level=2,
288
+ prompt_templates=prompt_templates
289
+ )
290
  print("✓ Agent created successfully")
291
  except Exception as e:
292
  print(f"✗ Error creating agent: {e}")
 
295
  # -------------------------
296
  # Launch UI
297
  # -------------------------
298
+ if GradioUI:
299
+ try:
300
+ ui = GradioUI(agent)
301
+ print("✓ UI created successfully")
302
+ ui.launch()
303
+ except Exception as e:
304
+ print(f"✗ Error launching custom UI: {e}")
305
+ GradioUI = None
306
+
307
+ if not GradioUI:
308
  # Fallback: create a simple Gradio interface
309
+ print("Using fallback Gradio interface")
310
  import gradio as gr
311
 
312
  def chat_function(message):
313
  try:
314
+ result = agent.run(message)
315
+ return str(result)
316
  except Exception as e:
317
  return f"Error: {str(e)}"
318
 
319
  demo = gr.Interface(
320
  fn=chat_function,
321
+ inputs=gr.Textbox(label="Your message", placeholder="Ask me anything..."),
322
  outputs=gr.Textbox(label="Agent response"),
323
+ title="AI Agent",
324
+ description="Chat with an AI agent that has access to various tools including weather, time, calculator, and web search."
325
  )
326
+ demo.launch(server_name="0.0.0.0", server_port=7860)