GoodLuckChuck commited on
Commit
bd681de
·
verified ·
1 Parent(s): e40e6b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -45
app.py CHANGED
@@ -17,14 +17,13 @@ logging.basicConfig(
17
  logger = logging.getLogger(__name__) # Define logger here
18
 
19
  # Import necessary components from smolagents
20
- # We need CodeWriter for the workaround if smolagents doesn't initialize it properly.
21
  try:
22
  from smolagents import CodeAgent, Tool, OpenAIServerModel
23
- from smolagents.code_writer import CodeWriter # For the __init__ workaround
24
  SMOLAGENTS_IMPORTED_SUCCESSFULLY = True
25
- logger.info("Successfully imported CodeAgent, Tool, OpenAIServerModel, and CodeWriter from smolagents.")
26
  except ImportError as e:
27
- logger.error(f"CRITICAL: Failed to import base smolagents components or smolagents.code_writer. Error: {e}. Ensure smolagents is correctly installed via requirements.txt.", exc_info=True)
28
  SMOLAGENTS_IMPORTED_SUCCESSFULLY = False
29
  # Dummy classes for script parsing if smolagents is not installed
30
  class CodeAgent: pass
@@ -33,11 +32,6 @@ except ImportError as e:
33
  def __init__(self, *args, **kwargs):
34
  logging.warning("Using DUMMY OpenAIServerModel due to smolagents import failure.")
35
  pass
36
- class CodeWriter:
37
- def __init__(self, *args, **kwargs):
38
- logging.warning("Using DUMMY CodeWriter due to smolagents.code_writer import failure.")
39
- pass
40
-
41
 
42
  from datetime import datetime
43
 
@@ -250,48 +244,26 @@ class QuestionAnsweringAgent(CodeAgent):
250
  api_key=openai_api_key
251
  )
252
 
253
- # REFINED WORKAROUND for MultiStepAgent internal TypeError
 
 
 
254
  try:
255
- # Step 1: Initialize the parent of CodeAgent (MultiStepAgent)
256
- # Pass 'additional_authorized_imports' as MultiStepAgent's parent (Agent) handles it.
257
- super(CodeAgent, self).__init__(
258
  model=effective_model,
259
  tools=tools_list,
 
 
260
  max_steps=CONFIG.AGENT_MAX_STEPS,
261
- additional_authorized_imports=["PIL", "pandas"] # Pass this to the Agent base class via MultiStepAgent
262
  )
263
- logger.info("QuestionAnsweringAgent > MultiStepAgent super().__init__ called with minimal args + additional_authorized_imports (workaround active).")
264
-
265
- # Step 2: Manually set attributes that CodeAgent itself uses and that might not be handled
266
- # correctly by MultiStepAgent if passed via **kwargs in CodeAgent's own super() call.
267
- self.model = effective_model
268
- self.tools = tools_list
269
- self.system_prompt = CONFIG.AGENT_SYSTEM_PROMPT # CRITICAL for CodeWriter
270
- # self.additional_authorized_imports is set by Agent's __init__ via MultiStepAgent
271
- self.verbosity_level = CONFIG.AGENT_VERBOSITY_LEVEL
272
-
273
- # Step 3: Manually initialize CodeAgent's `code_writer`.
274
- # This is essential for the system_prompt and tool descriptions to be used by the LLM.
275
- if SMOLAGENTS_IMPORTED_SUCCESSFULLY:
276
- # from smolagents.code_writer import CodeWriter # This was moved to top imports
277
- self.code_writer = CodeWriter(
278
- model=self.model,
279
- tools=self.tools,
280
- system_prompt=self.system_prompt, # Ensure this is passed
281
- additional_authorized_imports=self.additional_authorized_imports # And this
282
- )
283
- logger.info("Manually initialized self.code_writer for CodeAgent with current system_prompt, tools, and authorized imports.")
284
- else:
285
- logger.error("SMOLAGENTS_IMPORTED_SUCCESSFULLY is False. Cannot initialize self.code_writer. Agent will likely fail.")
286
-
287
  except TypeError as e:
288
- logger.error(f"WORKAROUND FAILED during __init__ (TypeError): {e}.", exc_info=True)
289
- raise e
290
- except Exception as e_init:
291
- logger.error(f"Unexpected error during QuestionAnsweringAgent __init__ (workaround): {e_init}", exc_info=True)
292
  raise e_init
293
-
294
- logger.info(f"QuestionAnsweringAgent initialized (with workaround) with model: {CONFIG.OPENAI_MODEL_ID}.")
295
 
296
 
297
  async def process_question(self, question_text: str) -> Tuple[str, str]:
@@ -571,7 +543,7 @@ with gr.Blocks() as demo:
571
  * The agent uses `smolagents`, OpenAI (model: `{CONFIG.OPENAI_MODEL_ID}`), and Serper API for searches.
572
  * Ensure `OPENAI_API_KEY` and `SERPER_API_KEY` are set as Space Secrets.
573
  * `requirements.txt` should specify a compatible `gradio` version and `smolagents`.
574
- * A workaround for `smolagents.CodeAgent` initialization is currently active.
575
  """
576
  )
577
 
 
17
  logger = logging.getLogger(__name__) # Define logger here
18
 
19
  # Import necessary components from smolagents
20
+ # Simplified import block. We are NOT trying to import CodeWriter directly here.
21
  try:
22
  from smolagents import CodeAgent, Tool, OpenAIServerModel
 
23
  SMOLAGENTS_IMPORTED_SUCCESSFULLY = True
24
+ logger.info("Successfully imported CodeAgent, Tool, OpenAIServerModel from smolagents.")
25
  except ImportError as e:
26
+ logger.error(f"CRITICAL: Failed to import base smolagents components. Error: {e}. Ensure smolagents is correctly installed via requirements.txt.", exc_info=True)
27
  SMOLAGENTS_IMPORTED_SUCCESSFULLY = False
28
  # Dummy classes for script parsing if smolagents is not installed
29
  class CodeAgent: pass
 
32
  def __init__(self, *args, **kwargs):
33
  logging.warning("Using DUMMY OpenAIServerModel due to smolagents import failure.")
34
  pass
 
 
 
 
 
35
 
36
  from datetime import datetime
37
 
 
244
  api_key=openai_api_key
245
  )
246
 
247
+ # REVERTED TO STANDARD CodeAgent INITIALIZATION
248
+ # This is the standard way to initialize CodeAgent.
249
+ # If this still causes a "MultiStepAgent... system_prompt" TypeError,
250
+ # it means the installed version of smolagents has an internal bug.
251
  try:
252
+ super().__init__(
 
 
253
  model=effective_model,
254
  tools=tools_list,
255
+ system_prompt=CONFIG.AGENT_SYSTEM_PROMPT,
256
+ additional_authorized_imports=["PIL", "pandas"],
257
  max_steps=CONFIG.AGENT_MAX_STEPS,
258
+ verbosity_level=CONFIG.AGENT_VERBOSITY_LEVEL
259
  )
260
+ logger.info(f"QuestionAnsweringAgent initialized successfully with model: {CONFIG.OPENAI_MODEL_ID}.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  except TypeError as e:
262
+ logger.error(f"TypeError during QuestionAnsweringAgent super().__init__: {e}. This is likely the 'MultiStepAgent' issue if the installed smolagents version is problematic.", exc_info=True)
263
+ raise e # Re-raise to see the error clearly if it happens
264
+ except Exception as e_init:
265
+ logger.error(f"Unexpected error during QuestionAnsweringAgent __init__: {e_init}", exc_info=True)
266
  raise e_init
 
 
267
 
268
 
269
  async def process_question(self, question_text: str) -> Tuple[str, str]:
 
543
  * The agent uses `smolagents`, OpenAI (model: `{CONFIG.OPENAI_MODEL_ID}`), and Serper API for searches.
544
  * Ensure `OPENAI_API_KEY` and `SERPER_API_KEY` are set as Space Secrets.
545
  * `requirements.txt` should specify a compatible `gradio` version and `smolagents`.
546
+ * If `smolagents` initialization errors occur, it might be due to internal library issues with the installed version.
547
  """
548
  )
549