Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import yaml
|
|
7 |
from tools.final_answer import FinalAnswerTool
|
8 |
|
9 |
from Gradio_UI import GradioUI
|
|
|
10 |
|
11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
12 |
|
@@ -246,19 +247,15 @@ backup_model = HfApiModel(
|
|
246 |
|
247 |
# Function to Build an Agent with Any Model
|
248 |
def build_agent(model_to_use):
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
return CodeAgent(
|
250 |
model=model_to_use,
|
251 |
-
tools=
|
252 |
-
final_answer,
|
253 |
-
get_horoscope,
|
254 |
-
get_date_panchang,
|
255 |
-
get_holidays,
|
256 |
-
get_panchang_field,
|
257 |
-
get_festivals_today,
|
258 |
-
get_current_time_in_timezone,
|
259 |
-
my_custom_tool,
|
260 |
-
image_generation_tool
|
261 |
-
],
|
262 |
max_steps=6,
|
263 |
verbosity_level=1,
|
264 |
grammar=None,
|
@@ -269,27 +266,41 @@ def build_agent(model_to_use):
|
|
269 |
)
|
270 |
|
271 |
# Instantiate Primary Agent
|
272 |
-
|
273 |
|
274 |
# Fallback-Handled Runner Function
|
275 |
def agent_runner(user_input):
|
|
|
|
|
|
|
|
|
276 |
try:
|
277 |
-
|
278 |
-
|
279 |
-
|
|
|
280 |
return result
|
281 |
except Exception as e:
|
282 |
-
print(f"Primary model failed: {e}")
|
283 |
-
print("Switching to backup model...")
|
284 |
-
|
285 |
-
backup_agent = build_agent(backup_model)
|
286 |
try:
|
|
|
287 |
result = backup_agent.run(user_input)
|
288 |
-
|
|
|
|
|
289 |
except Exception as e2:
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
|
295 |
-
|
|
|
7 |
from tools.final_answer import FinalAnswerTool
|
8 |
|
9 |
from Gradio_UI import GradioUI
|
10 |
+
import gradio as gr
|
11 |
|
12 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
13 |
|
|
|
247 |
|
248 |
# Function to Build an Agent with Any Model
|
249 |
def build_agent(model_to_use):
|
250 |
+
# Assuming the tools are defined elsewhere
|
251 |
+
tools = [
|
252 |
+
# final_answer, get_horoscope, get_date_panchang,
|
253 |
+
# get_holidays, get_panchang_field, get_festivals_today,
|
254 |
+
# get_current_time_in_timezone, my_custom_tool, image_generation_tool
|
255 |
+
]
|
256 |
return CodeAgent(
|
257 |
model=model_to_use,
|
258 |
+
tools=tools,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
max_steps=6,
|
260 |
verbosity_level=1,
|
261 |
grammar=None,
|
|
|
266 |
)
|
267 |
|
268 |
# Instantiate Primary Agent
|
269 |
+
primary_agent = build_agent(primary_model)
|
270 |
|
271 |
# Fallback-Handled Runner Function
|
272 |
def agent_runner(user_input):
|
273 |
+
"""
|
274 |
+
This function takes user input, tries the primary agent,
|
275 |
+
and switches to the backup agent on failure.
|
276 |
+
"""
|
277 |
try:
|
278 |
+
print("Attempting to run with the primary model...")
|
279 |
+
result = primary_agent.run(user_input)
|
280 |
+
if result is None or (isinstance(result, str) and result.strip() == ""):
|
281 |
+
raise ValueError("Primary model returned an empty or null response.")
|
282 |
return result
|
283 |
except Exception as e:
|
284 |
+
print(f"Primary model failed with error: {e}")
|
285 |
+
print("Switching to the backup model...")
|
|
|
|
|
286 |
try:
|
287 |
+
backup_agent = build_agent(backup_model)
|
288 |
result = backup_agent.run(user_input)
|
289 |
+
if result is None or (isinstance(result, str) and result.strip() == ""):
|
290 |
+
return "Backup model also returned an empty response."
|
291 |
+
return result
|
292 |
except Exception as e2:
|
293 |
+
print(f"Backup model also failed with error: {e2}")
|
294 |
+
return f"The backup model failed to generate a response: {e2}"
|
295 |
+
|
296 |
+
# Launch Gradio with the runner function
|
297 |
+
# We use the standard gr.Interface for this logic.
|
298 |
+
iface = gr.Interface(
|
299 |
+
fn=agent_runner,
|
300 |
+
inputs="text",
|
301 |
+
outputs="text",
|
302 |
+
title="Agent with Fallback Logic",
|
303 |
+
description="Enter your query. The system will use a primary model and switch to a backup if the primary fails."
|
304 |
+
)
|
305 |
|
306 |
+
iface.launch()
|