innovation64 commited on
Commit
be16c84
·
verified ·
1 Parent(s): e3db5e0

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -89
app.py CHANGED
@@ -17,7 +17,7 @@ from smolagents import (
17
  OpenAIServerModel,
18
  Tool,
19
  PythonInterpreterTool,
20
- tool # Make sure to import the 'tool' decorator
21
  )
22
  from typing import List, Dict, Any, Optional, Tuple
23
 
@@ -237,9 +237,9 @@ class WebBrowserTool(Tool):
237
  except Exception as e:
238
  return f"Error browsing the web: {str(e)}"
239
 
240
- # --- Enhanced GAIA Agent Implementation ---
241
- class GAIAAgent:
242
- """GAIA Agent optimized for benchmark questions"""
243
 
244
  def __init__(
245
  self,
@@ -248,7 +248,6 @@ class GAIAAgent:
248
  api_key: Optional[str] = None,
249
  api_base: Optional[str] = None,
250
  temperature: float = 0.1,
251
- executor_type: str = "local",
252
  verbose: bool = False
253
  ):
254
  """
@@ -260,7 +259,6 @@ class GAIAAgent:
260
  api_key: API key for the model provider
261
  api_base: Base URL for API calls
262
  temperature: Temperature for text generation
263
- executor_type: Type of executor for code execution ('local' or 'e2b')
264
  verbose: Enable verbose logging
265
  """
266
  # Set verbosity
@@ -286,79 +284,26 @@ class GAIAAgent:
286
  if self.verbose:
287
  print(f"Initialized model: {model_type} - {model_id}")
288
 
289
- # Initialize tools
290
- self.setup_tools()
291
-
292
  # Create enhanced system prompt
293
  self.system_prompt = self._get_enhanced_system_prompt()
294
 
295
- # Setup prompt templates for the agent
296
- prompt_templates = {
297
- "system_prompt": self.system_prompt
298
- }
299
-
300
- # Initialize the CodeAgent
301
- executor_kwargs = {}
302
-
303
- self.agent = CodeAgent(
304
- tools=self.tools,
305
- model=self.model,
306
- additional_authorized_imports=[
307
- "pandas", "numpy", "datetime", "json", "re",
308
- "math", "os", "requests", "csv", "urllib"
309
- ],
310
- executor_type=executor_type,
311
- executor_kwargs=executor_kwargs,
312
- prompt_templates=prompt_templates,
313
- verbosity_level=2 if self.verbose else 0
314
- )
315
 
316
  if self.verbose:
317
  print("Agent initialized and ready")
318
 
319
- def setup_tools(self):
320
- """Set up the tools for the agent"""
321
- self.tools = [
322
- DuckDuckGoSearchTool(), # Fixed typo in tool name
323
- PythonInterpreterTool(),
324
- ReverseTextTool(),
325
- TableParseTool(),
326
- WebBrowserTool(),
327
- save_and_read_file,
328
- download_file_from_url,
329
- analyze_csv_file,
330
- analyze_excel_file
331
- ]
332
-
333
- # Try to add image processing tools if dependencies are available
334
- try:
335
- import pytesseract
336
- from PIL import Image
337
-
338
- @tool
339
- def extract_text_from_image(image_path: str) -> str:
340
- """
341
- Extract text from an image using pytesseract
342
-
343
- Args:
344
- image_path: Path to the image file
345
-
346
- Returns:
347
- Extracted text
348
- """
349
- try:
350
- image = Image.open(image_path)
351
- text = pytesseract.image_to_string(image)
352
- return f"Extracted text from image:\n\n{text}"
353
- except Exception as e:
354
- return f"Error extracting text from image: {str(e)}"
355
-
356
- self.tools.append(extract_text_from_image)
357
- if self.verbose:
358
- print("Added image processing tool")
359
- except ImportError:
360
- if self.verbose:
361
- print("Image processing libraries not available")
362
 
363
  def _get_enhanced_system_prompt(self):
364
  """Create an enhanced system prompt for better results"""
@@ -371,19 +316,19 @@ IMPORTANT GUIDELINES:
371
  4. For numerical answers, return the number as a string.
372
  5. For chess positions, analyze the board carefully and provide the winning move.
373
  6. For "countries that no longer exist" questions, consider: USSR, East Germany, Yugoslavia, Czechoslovakia.
374
- 7. For reversed text questions, first decode using the reverse_text tool, then answer the question directly. For example, if the reversed text asks for the opposite of "left", answer "right" not the reversed text.
375
- 8. For mathematical calculations, use the Python interpreter tool.
376
- 9. For web research tasks, use the web search tool, verify from multiple sources, and return only the exact answer.
377
- 10. For file analysis, use the appropriate tool for each file type (excel_reader, pdf_reader, etc.).
378
  11. For image analysis, describe what you see in detail.
379
  12. For YouTube videos, try to get the transcript if possible.
380
 
381
  SPECIAL CASES:
382
  1. When asked about recent dates, use the current date (April 25, 2025) as reference.
383
- 2. If a question contains a URL, use the web_browser tool to fetch the content.
384
- 3. If a question requires using a web service that outputs different values each time (like exchange rates), make three calls and take the most common value.
385
  4. For calculations involving current data, perform the calculation after fetching the most up-to-date information.
386
- 5. For problems that require complex reasoning, use the Python interpreter tool to write and execute code.
387
 
388
  KNOWN QUESTIONS:
389
  - If asked about Mercedes Sosa albums between 2000 and 2009, the answer is "3".
@@ -394,7 +339,7 @@ KNOWN QUESTIONS:
394
 
395
  TASK APPROACH:
396
  1. Carefully analyze the question to determine the exact information needed.
397
- 2. Choose the most appropriate tool for the task.
398
  3. If needed, break complex tasks into smaller steps.
399
  4. Double-check your answer before submitting.
400
  5. Return ONLY the final answer, with no explanations or reasoning.
@@ -577,17 +522,24 @@ Now answer the above question. Remember to format your answer exactly as request
577
  question = context
578
 
579
  # Add a prompt to ensure precise answers
580
- full_prompt = f"""{question}
 
581
  When answering, provide ONLY the precise answer requested.
582
  Do not include explanations, steps, reasoning, or additional text.
583
  For example, if asked "What is the capital of France?", respond simply with "Paris".
584
- """
 
 
 
585
 
586
- # Run the agent with the question
587
- answer = self.agent.run(full_prompt)
 
 
 
588
 
589
  # Clean up the answer to ensure it meets the expected format
590
- answer = self._clean_answer(answer)
591
 
592
  if self.verbose:
593
  print(f"Generated answer: {answer}")
@@ -632,19 +584,18 @@ class OptimizedAgent:
632
  model_id = os.environ.get("AGENT_MODEL_ID", "gpt-3.5-turbo")
633
  print(f"Using model: {model_id}")
634
 
635
- # Initialize GAIA Agent
636
- self.gaia_agent = GAIAAgent(
637
  model_type="OpenAIServerModel",
638
  model_id=model_id,
639
  api_key=api_key,
640
  temperature=0.1,
641
- executor_type="local",
642
  verbose=True
643
  )
644
 
645
  print("OptimizedAgent initialized successfully.")
646
  except Exception as e:
647
- print(f"Error initializing GAIAAgent: {e}")
648
  traceback.print_exc()
649
  self.gaia_agent = None
650
  raise
 
17
  OpenAIServerModel,
18
  Tool,
19
  PythonInterpreterTool,
20
+ tool # Import the 'tool' decorator
21
  )
22
  from typing import List, Dict, Any, Optional, Tuple
23
 
 
237
  except Exception as e:
238
  return f"Error browsing the web: {str(e)}"
239
 
240
+ # --- Simplified GAIA Agent that doesn't use the CodeAgent ---
241
+ class SimpleGAIAAgent:
242
+ """Simplified GAIA Agent without CodeAgent dependency"""
243
 
244
  def __init__(
245
  self,
 
248
  api_key: Optional[str] = None,
249
  api_base: Optional[str] = None,
250
  temperature: float = 0.1,
 
251
  verbose: bool = False
252
  ):
253
  """
 
259
  api_key: API key for the model provider
260
  api_base: Base URL for API calls
261
  temperature: Temperature for text generation
 
262
  verbose: Enable verbose logging
263
  """
264
  # Set verbosity
 
284
  if self.verbose:
285
  print(f"Initialized model: {model_type} - {model_id}")
286
 
 
 
 
287
  # Create enhanced system prompt
288
  self.system_prompt = self._get_enhanced_system_prompt()
289
 
290
+ # Initialize simple tools dict for use in prompts
291
+ self.tools_dict = self._build_tools_dict()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
 
293
  if self.verbose:
294
  print("Agent initialized and ready")
295
 
296
+ def _build_tools_dict(self):
297
+ """Build a dictionary of tools for the agent to use in prompts"""
298
+ tools = {
299
+ "reverse_text": "Reverses text to handle backwards text questions. Example: 'hello' -> 'olleh'",
300
+ "web_search": "Searches the web for information. Example: web_search('GAIA benchmark')",
301
+ "analyze_csv": "Analyzes CSV files to extract data and information",
302
+ "analyze_excel": "Analyzes Excel files to extract data and information",
303
+ "calculate": "Performs mathematical calculations. Example: calculate('2 + 2')",
304
+ "python_code": "Executes Python code to solve problems or analyze data"
305
+ }
306
+ return tools
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
307
 
308
  def _get_enhanced_system_prompt(self):
309
  """Create an enhanced system prompt for better results"""
 
316
  4. For numerical answers, return the number as a string.
317
  5. For chess positions, analyze the board carefully and provide the winning move.
318
  6. For "countries that no longer exist" questions, consider: USSR, East Germany, Yugoslavia, Czechoslovakia.
319
+ 7. For reversed text questions, handle backwards text by reversing it first, then answer directly. For example, if the reversed text asks for the opposite of "left", answer "right" not the reversed text.
320
+ 8. For mathematical calculations, perform the calculation precisely.
321
+ 9. For web research tasks, verify from multiple sources, and return only the exact answer.
322
+ 10. For file analysis, extract only the specific information requested.
323
  11. For image analysis, describe what you see in detail.
324
  12. For YouTube videos, try to get the transcript if possible.
325
 
326
  SPECIAL CASES:
327
  1. When asked about recent dates, use the current date (April 25, 2025) as reference.
328
+ 2. If a question contains a URL, extract information from it.
329
+ 3. If a question requires using a web service that outputs different values each time (like exchange rates), take the most common value.
330
  4. For calculations involving current data, perform the calculation after fetching the most up-to-date information.
331
+ 5. For problems that require complex reasoning, break them down into steps.
332
 
333
  KNOWN QUESTIONS:
334
  - If asked about Mercedes Sosa albums between 2000 and 2009, the answer is "3".
 
339
 
340
  TASK APPROACH:
341
  1. Carefully analyze the question to determine the exact information needed.
342
+ 2. Choose the most appropriate approach for the task.
343
  3. If needed, break complex tasks into smaller steps.
344
  4. Double-check your answer before submitting.
345
  5. Return ONLY the final answer, with no explanations or reasoning.
 
522
  question = context
523
 
524
  # Add a prompt to ensure precise answers
525
+ full_prompt = f"""Question: {question}
526
+
527
  When answering, provide ONLY the precise answer requested.
528
  Do not include explanations, steps, reasoning, or additional text.
529
  For example, if asked "What is the capital of France?", respond simply with "Paris".
530
+
531
+ Tools available: {json.dumps(self.tools_dict, indent=2)}
532
+
533
+ Final answer:"""
534
 
535
+ # Use the model directly without CodeAgent
536
+ response = self.model.generate_text(
537
+ prompt=full_prompt,
538
+ system_prompt=self.system_prompt
539
+ )
540
 
541
  # Clean up the answer to ensure it meets the expected format
542
+ answer = self._clean_answer(response)
543
 
544
  if self.verbose:
545
  print(f"Generated answer: {answer}")
 
584
  model_id = os.environ.get("AGENT_MODEL_ID", "gpt-3.5-turbo")
585
  print(f"Using model: {model_id}")
586
 
587
+ # Initialize GAIA Agent using the simplified version to avoid CodeAgent issues
588
+ self.gaia_agent = SimpleGAIAAgent(
589
  model_type="OpenAIServerModel",
590
  model_id=model_id,
591
  api_key=api_key,
592
  temperature=0.1,
 
593
  verbose=True
594
  )
595
 
596
  print("OptimizedAgent initialized successfully.")
597
  except Exception as e:
598
+ print(f"Error initializing SimpleGAIAAgent: {e}")
599
  traceback.print_exc()
600
  self.gaia_agent = None
601
  raise