hassenhamdi commited on
Commit
2f798b2
·
verified ·
1 Parent(s): 33eedd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -1,15 +1,13 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
  from smolagents import OpenAIServerModel
7
  from smolagents import CodeAgent, Tool, tool
8
  from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
9
- from smolagents import PythonInterpreterTool # Import the built-in Python Interpreter Tool
10
- from smolagents.agents.base import LogLevel # Import LogLevel for verbosity
11
- import time # Import time for sleep
12
- from requests.exceptions import HTTPError # Import HTTPError for specific exception handling
13
 
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -79,12 +77,13 @@ class GaiaAgent(CodeAgent):
79
 
80
  # Define the tools available to the agent
81
  agent_tools = [
82
- python_interpreter_tool, # Use the built-in Python interpreter tool
83
  gaia_file_tool_instance,
84
  duckduckgo_search_tool,
85
  visit_webpage_tool
86
  ]
87
- super().__init__(model=self.llm_model, tools=agent_tools, verbosity_level=LogLevel.DEBUG)
 
88
  print("GaiaAgent initialized successfully with Gemini Flash and built-in tools.")
89
 
90
  def __call__(self, question: str) -> str:
@@ -122,13 +121,12 @@ class GaiaAgent(CodeAgent):
122
  for attempt in range(max_retries):
123
  try:
124
  result = self.run(prompt)
125
- print(f"Agent raw output from self.run():\n{result}") # Log the raw output
126
  break # Break loop if successful
127
  except HTTPError as e:
128
  if e.response.status_code == 429:
129
  error_details = ""
130
  try:
131
- # Attempt to parse more specific error details from the response
132
  error_json = e.response.json()
133
  if 'error' in error_json and 'details' in error_json['error']:
134
  for detail in error_json['error']['details']:
@@ -137,7 +135,7 @@ class GaiaAgent(CodeAgent):
137
  quota_id = detail.get('quotaId', 'N/A')
138
  quota_value = detail.get('quotaValue', 'N/A')
139
  error_details = f"Quota Metric: {quota_metric}, Quota ID: {quota_id}, Value: {quota_value}. "
140
- break # Found relevant detail
141
  except Exception as parse_error:
142
  print(f"Could not parse detailed error from 429 response: {parse_error}")
143
  error_details = "Check Google Cloud Console for details. "
@@ -151,12 +149,10 @@ class GaiaAgent(CodeAgent):
151
  )
152
  print(error_message)
153
  time.sleep(retry_delay)
154
- retry_delay *= 2 # Exponential backoff
155
  else:
156
- # Re-raise other HTTP errors
157
  raise
158
  except Exception as e:
159
- # Log the full traceback for better debugging
160
  import traceback
161
  print(f"--- Error during agent execution on attempt {attempt + 1}/{max_retries}: {e}")
162
  traceback.print_exc()
@@ -165,7 +161,7 @@ class GaiaAgent(CodeAgent):
165
  time.sleep(retry_delay)
166
  retry_delay *= 2
167
  else:
168
- return "Agent encountered an error and could not provide an answer after multiple retries." # Final failure after retries
169
 
170
  if result is None:
171
  return "Agent failed after multiple retries due to an unknown error or persistent rate limits."
@@ -206,7 +202,7 @@ class GaiaAgent(CodeAgent):
206
  return cleaned_output.strip()
207
 
208
 
209
- # --- Gradio Application Logic (mostly unchanged from template) ---
210
 
211
  def run_and_submit_all(profile: gr.OAuthProfile | None):
212
  """
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
  from smolagents import OpenAIServerModel
6
  from smolagents import CodeAgent, Tool, tool
7
  from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
8
+ from smolagents import PythonInterpreterTool
9
+ import time
10
+ from requests.exceptions import HTTPError
 
11
 
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
77
 
78
  # Define the tools available to the agent
79
  agent_tools = [
80
+ python_interpreter_tool,
81
  gaia_file_tool_instance,
82
  duckduckgo_search_tool,
83
  visit_webpage_tool
84
  ]
85
+ # Set verbosity_level directly to 2 for DEBUG logs
86
+ super().__init__(model=self.llm_model, tools=agent_tools, verbosity_level=2)
87
  print("GaiaAgent initialized successfully with Gemini Flash and built-in tools.")
88
 
89
  def __call__(self, question: str) -> str:
 
121
  for attempt in range(max_retries):
122
  try:
123
  result = self.run(prompt)
124
+ print(f"Agent raw output from self.run():\n{result}")
125
  break # Break loop if successful
126
  except HTTPError as e:
127
  if e.response.status_code == 429:
128
  error_details = ""
129
  try:
 
130
  error_json = e.response.json()
131
  if 'error' in error_json and 'details' in error_json['error']:
132
  for detail in error_json['error']['details']:
 
135
  quota_id = detail.get('quotaId', 'N/A')
136
  quota_value = detail.get('quotaValue', 'N/A')
137
  error_details = f"Quota Metric: {quota_metric}, Quota ID: {quota_id}, Value: {quota_value}. "
138
+ break
139
  except Exception as parse_error:
140
  print(f"Could not parse detailed error from 429 response: {parse_error}")
141
  error_details = "Check Google Cloud Console for details. "
 
149
  )
150
  print(error_message)
151
  time.sleep(retry_delay)
152
+ retry_delay *= 2
153
  else:
 
154
  raise
155
  except Exception as e:
 
156
  import traceback
157
  print(f"--- Error during agent execution on attempt {attempt + 1}/{max_retries}: {e}")
158
  traceback.print_exc()
 
161
  time.sleep(retry_delay)
162
  retry_delay *= 2
163
  else:
164
+ return "Agent encountered an error and could not provide an answer after multiple retries."
165
 
166
  if result is None:
167
  return "Agent failed after multiple retries due to an unknown error or persistent rate limits."
 
202
  return cleaned_output.strip()
203
 
204
 
205
+ # --- Gradio Application Logic ---
206
 
207
  def run_and_submit_all(profile: gr.OAuthProfile | None):
208
  """