Update app.py
Browse files
app.py
CHANGED
|
@@ -17,14 +17,14 @@ logging.basicConfig(
|
|
| 17 |
logger = logging.getLogger(__name__) # Define logger here
|
| 18 |
|
| 19 |
# Import necessary components from smolagents
|
| 20 |
-
# We
|
| 21 |
try:
|
| 22 |
from smolagents import CodeAgent, Tool, OpenAIServerModel
|
| 23 |
-
|
| 24 |
SMOLAGENTS_IMPORTED_SUCCESSFULLY = True
|
| 25 |
-
logger.info("Successfully imported CodeAgent, Tool, OpenAIServerModel from smolagents.")
|
| 26 |
except ImportError as e:
|
| 27 |
-
logger.error(f"CRITICAL: Failed to import base smolagents components. 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,6 +33,11 @@ except ImportError as e:
|
|
| 33 |
def __init__(self, *args, **kwargs):
|
| 34 |
logging.warning("Using DUMMY OpenAIServerModel due to smolagents import failure.")
|
| 35 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
from datetime import datetime
|
| 38 |
|
|
@@ -245,47 +250,42 @@ class QuestionAnsweringAgent(CodeAgent):
|
|
| 245 |
api_key=openai_api_key
|
| 246 |
)
|
| 247 |
|
| 248 |
-
#
|
| 249 |
try:
|
| 250 |
-
# Step 1:
|
| 251 |
-
#
|
| 252 |
-
# This bypasses CodeAgent's own __init__ for this specific super() call.
|
| 253 |
super(CodeAgent, self).__init__(
|
| 254 |
model=effective_model,
|
| 255 |
tools=tools_list,
|
| 256 |
-
max_steps=CONFIG.AGENT_MAX_STEPS
|
|
|
|
| 257 |
)
|
| 258 |
-
logger.info("QuestionAnsweringAgent > MultiStepAgent super().__init__ called with
|
| 259 |
-
|
| 260 |
-
# Step 2: Manually set attributes that CodeAgent itself uses
|
| 261 |
-
#
|
| 262 |
-
self.model = effective_model
|
| 263 |
-
self.tools = tools_list
|
| 264 |
-
self.system_prompt = CONFIG.AGENT_SYSTEM_PROMPT
|
| 265 |
-
self.additional_authorized_imports
|
| 266 |
self.verbosity_level = CONFIG.AGENT_VERBOSITY_LEVEL
|
| 267 |
-
|
| 268 |
-
|
| 269 |
# Step 3: Manually initialize CodeAgent's `code_writer`.
|
| 270 |
-
#
|
| 271 |
-
|
| 272 |
-
# we need to ensure `code_writer` is set up correctly for CodeAgent to function.
|
| 273 |
-
# This assumes `CodeWriter` is importable and `smolagents` is installed.
|
| 274 |
-
if SMOLAGENTS_IMPORTED_SUCCESSFULLY: # Check if CodeWriter was imported
|
| 275 |
# from smolagents.code_writer import CodeWriter # This was moved to top imports
|
| 276 |
self.code_writer = CodeWriter(
|
| 277 |
model=self.model,
|
| 278 |
tools=self.tools,
|
| 279 |
-
system_prompt=self.system_prompt,
|
| 280 |
-
additional_authorized_imports=self.additional_authorized_imports
|
| 281 |
)
|
| 282 |
-
logger.info("Manually initialized self.code_writer for CodeAgent with current system_prompt and
|
| 283 |
else:
|
| 284 |
logger.error("SMOLAGENTS_IMPORTED_SUCCESSFULLY is False. Cannot initialize self.code_writer. Agent will likely fail.")
|
| 285 |
-
|
| 286 |
-
|
| 287 |
except TypeError as e:
|
| 288 |
-
logger.error(f"WORKAROUND FAILED during __init__ (TypeError): {e}.
|
| 289 |
raise e
|
| 290 |
except Exception as e_init:
|
| 291 |
logger.error(f"Unexpected error during QuestionAnsweringAgent __init__ (workaround): {e_init}", exc_info=True)
|
|
@@ -571,7 +571,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 |
+
# 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 |
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 |
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)
|
|
|
|
| 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 |
|