Update app.py
Browse files
app.py
CHANGED
|
@@ -17,9 +17,10 @@ logging.basicConfig(
|
|
| 17 |
logger = logging.getLogger(__name__) # Define logger here
|
| 18 |
|
| 19 |
# Import necessary components from smolagents
|
| 20 |
-
#
|
| 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:
|
|
@@ -244,26 +245,53 @@ class QuestionAnsweringAgent(CodeAgent):
|
|
| 244 |
api_key=openai_api_key
|
| 245 |
)
|
| 246 |
|
| 247 |
-
#
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 253 |
model=effective_model,
|
| 254 |
tools=tools_list,
|
| 255 |
-
|
| 256 |
-
additional_authorized_imports=["PIL", "pandas"],
|
| 257 |
-
max_steps=CONFIG.AGENT_MAX_STEPS,
|
| 258 |
-
verbosity_level=CONFIG.AGENT_VERBOSITY_LEVEL
|
| 259 |
)
|
| 260 |
-
logger.info(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
except TypeError as e:
|
| 262 |
-
logger.error(f"
|
| 263 |
raise e
|
| 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]:
|
|
@@ -363,6 +391,7 @@ class ChuckAgent:
|
|
| 363 |
logger.info("ChuckAgent shutdown complete.")
|
| 364 |
|
| 365 |
# --- Core Evaluation Logic ---
|
|
|
|
| 366 |
def get_space_id_agent_code_url() -> Tuple[Optional[str], str]:
|
| 367 |
space_id = os.getenv("SPACE_ID")
|
| 368 |
agent_code_url = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Agent code URL not found (SPACE_ID not set)"
|
|
@@ -542,7 +571,7 @@ with gr.Blocks() as demo:
|
|
| 542 |
* The agent uses `smolagents`, OpenAI (model: `{CONFIG.OPENAI_MODEL_ID}`), and Serper API for searches.
|
| 543 |
* Ensure `OPENAI_API_KEY` and `SERPER_API_KEY` are set as Space Secrets.
|
| 544 |
* `requirements.txt` should specify a compatible `gradio` version and `smolagents`.
|
| 545 |
-
*
|
| 546 |
"""
|
| 547 |
)
|
| 548 |
|
|
|
|
| 17 |
logger = logging.getLogger(__name__) # Define logger here
|
| 18 |
|
| 19 |
# Import necessary components from smolagents
|
| 20 |
+
# We are NOT importing CodeWriter explicitly here anymore due to ModuleNotFoundError
|
| 21 |
try:
|
| 22 |
from smolagents import CodeAgent, Tool, OpenAIServerModel
|
| 23 |
+
# We assume CodeAgent will internally handle its CodeWriter instantiation
|
| 24 |
SMOLAGENTS_IMPORTED_SUCCESSFULLY = True
|
| 25 |
logger.info("Successfully imported CodeAgent, Tool, OpenAIServerModel from smolagents.")
|
| 26 |
except ImportError as e:
|
|
|
|
| 245 |
api_key=openai_api_key
|
| 246 |
)
|
| 247 |
|
| 248 |
+
# REINSTATED WORKAROUND for MultiStepAgent TypeError
|
|
|
|
|
|
|
|
|
|
| 249 |
try:
|
| 250 |
+
# Step 1: Call the __init__ of CodeAgent's parent (MultiStepAgent) with minimal arguments.
|
| 251 |
+
# We use super(CodeAgent, self) to specifically call MultiStepAgent.__init__
|
| 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 MINIMAL args (workaround active).")
|
| 259 |
+
|
| 260 |
+
# Step 2: Manually set attributes that CodeAgent itself uses.
|
| 261 |
+
# CodeAgent's own __init__ would normally set these. We're setting them on the instance.
|
| 262 |
+
self.model = effective_model # Also used by CodeAgent
|
| 263 |
+
self.tools = tools_list # Also used by CodeAgent
|
| 264 |
+
self.system_prompt = CONFIG.AGENT_SYSTEM_PROMPT
|
| 265 |
+
self.additional_authorized_imports = ["PIL", "pandas"]
|
| 266 |
+
self.verbosity_level = CONFIG.AGENT_VERBOSITY_LEVEL
|
| 267 |
+
# self.max_steps is already handled by the MultiStepAgent's init.
|
| 268 |
+
|
| 269 |
+
# Step 3: Manually initialize CodeAgent's `code_writer`.
|
| 270 |
+
# The `CodeAgent` class typically initializes `self.code_writer` in its own `__init__`.
|
| 271 |
+
# Since we bypassed parts of CodeAgent's __init__ to avoid the TypeError with MultiStepAgent,
|
| 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 tools.")
|
| 283 |
+
else:
|
| 284 |
+
logger.error("SMOLAGENTS_IMPORTED_SUCCESSFULLY is False. Cannot initialize self.code_writer. Agent will likely fail.")
|
| 285 |
+
# This state should ideally not be reached if the top-level import check works.
|
| 286 |
+
|
| 287 |
except TypeError as e:
|
| 288 |
+
logger.error(f"WORKAROUND FAILED during __init__ (TypeError): {e}. This suggests an issue with the minimal args for MultiStepAgent or manual CodeWriter setup.", 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]:
|
|
|
|
| 391 |
logger.info("ChuckAgent shutdown complete.")
|
| 392 |
|
| 393 |
# --- Core Evaluation Logic ---
|
| 394 |
+
# (This section remains unchanged)
|
| 395 |
def get_space_id_agent_code_url() -> Tuple[Optional[str], str]:
|
| 396 |
space_id = os.getenv("SPACE_ID")
|
| 397 |
agent_code_url = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Agent code URL not found (SPACE_ID not set)"
|
|
|
|
| 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 due to potential library issues.
|
| 575 |
"""
|
| 576 |
)
|
| 577 |
|