Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,13 @@ import requests
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -11,13 +18,105 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
11 |
# --- Basic Agent Definition ---
|
12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
13 |
class BasicAgent:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def __init__(self):
|
15 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def __call__(self, question: str) -> str:
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
23 |
"""
|
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
|
7 |
+
try:
|
8 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
9 |
+
SMOLAGENTS_AVAILABLE = True
|
10 |
+
except ImportError:
|
11 |
+
print("Warning: smolagents not available, using fallback implementation")
|
12 |
+
SMOLAGENTS_AVAILABLE = False
|
13 |
+
|
14 |
# (Keep Constants as is)
|
15 |
# --- Constants ---
|
16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
18 |
# --- Basic Agent Definition ---
|
19 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
20 |
class BasicAgent:
|
21 |
+
"""
|
22 |
+
Simple agent using smolagents CodeAgent with DuckDuckGoSearchTool.
|
23 |
+
|
24 |
+
Based on Unit 2 course content:
|
25 |
+
- Uses CodeAgent from smolagents (writes Python code to solve problems)
|
26 |
+
- Has access to DuckDuckGoSearchTool for web search
|
27 |
+
- Follows ReAct pattern automatically within CodeAgent
|
28 |
+
|
29 |
+
How ReAct works in smolagents:
|
30 |
+
1. CodeAgent receives a question
|
31 |
+
2. INTERNAL LOOP (hidden from us):
|
32 |
+
- Think: LLM reasons about what to do next
|
33 |
+
- Act: LLM writes Python code (may call tools like DuckDuckGoSearchTool)
|
34 |
+
- Observe: LLM sees the code execution results
|
35 |
+
- Repeat: If not satisfied, continues the loop (up to max_steps)
|
36 |
+
3. Returns final answer when LLM decides it's complete
|
37 |
+
|
38 |
+
The while loop is built into CodeAgent - we just call agent.run(question)!
|
39 |
+
"""
|
40 |
+
|
41 |
def __init__(self):
|
42 |
+
print("SimpleCodeAgent initialized.")
|
43 |
+
|
44 |
+
if SMOLAGENTS_AVAILABLE:
|
45 |
+
try:
|
46 |
+
# Initialize the model (from Unit 2)
|
47 |
+
self.model = HfApiModel()
|
48 |
+
|
49 |
+
# Create CodeAgent with DuckDuckGoSearchTool (core Unit 2 concept)
|
50 |
+
self.agent = CodeAgent(
|
51 |
+
tools=[DuckDuckGoSearchTool()],
|
52 |
+
model=self.model,
|
53 |
+
additional_authorized_imports=[
|
54 |
+
'math', 'statistics', 're', # Basic computation
|
55 |
+
'requests', 'json', # Web requests and JSON
|
56 |
+
'pandas', 'numpy', # Data analysis
|
57 |
+
'zipfile', 'os', # File processing
|
58 |
+
'datetime', 'time' # Date/time operations
|
59 |
+
],
|
60 |
+
system_prompt="""You are an AI assistant that answers questions accurately and concisely.
|
61 |
+
|
62 |
+
When answering questions:
|
63 |
+
- Provide ONLY the direct answer requested
|
64 |
+
- For numbers, give just the number (e.g., "42", not "The answer is 42")
|
65 |
+
- For names/words, give just the name/word (e.g., "Paris", not "The capital is Paris")
|
66 |
+
- For yes/no questions, answer just "Yes" or "No"
|
67 |
+
- Do not include explanations unless specifically asked
|
68 |
+
- Do not add prefixes like "The answer is" or "According to my search"
|
69 |
+
|
70 |
+
You have access to web search via DuckDuckGoSearchTool when you need current information.
|
71 |
+
You can write Python code to perform calculations or process data.
|
72 |
+
|
73 |
+
Use the Think-Act-Observe pattern:
|
74 |
+
1. Think about what you need to do
|
75 |
+
2. Act by writing code or using tools
|
76 |
+
3. Observe the results
|
77 |
+
4. Continue until you have the final answer
|
78 |
+
|
79 |
+
Always end with just the direct answer the question asks for."""
|
80 |
+
)
|
81 |
+
|
82 |
+
self.available = True
|
83 |
+
print("✅ Smolagents CodeAgent initialized successfully")
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
print(f"⚠️ Error initializing smolagents: {e}")
|
87 |
+
self.available = False
|
88 |
+
else:
|
89 |
+
self.available = False
|
90 |
+
|
91 |
def __call__(self, question: str) -> str:
|
92 |
+
"""
|
93 |
+
Main agent execution - just pass the question to CodeAgent.
|
94 |
+
The CodeAgent will Think-Act-Observe automatically and format the answer properly.
|
95 |
+
"""
|
96 |
+
print(f"Agent processing: {question[:80]}...")
|
97 |
+
|
98 |
+
if not self.available:
|
99 |
+
return self._fallback_response(question)
|
100 |
+
|
101 |
+
try:
|
102 |
+
# Let the CodeAgent handle everything with ReAct pattern
|
103 |
+
# The LLM is smart enough to format answers properly for GAIA
|
104 |
+
result = self.agent.run(question)
|
105 |
+
|
106 |
+
final_answer = str(result).strip()
|
107 |
+
print(f"Agent answer: {final_answer}")
|
108 |
+
return final_answer
|
109 |
+
|
110 |
+
except Exception as e:
|
111 |
+
print(f"Agent error: {e}")
|
112 |
+
return "Unable to process question"
|
113 |
+
|
114 |
+
def _fallback_response(self, question: str) -> str:
|
115 |
+
"""
|
116 |
+
Fallback if smolagents is not available.
|
117 |
+
"""
|
118 |
+
print("Smolagents not available - using basic fallback")
|
119 |
+
return "Smolagents required but not available"
|
120 |
|
121 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
122 |
"""
|