Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,14 +10,64 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
+
from smolagents import (
|
| 14 |
+
CodeAgent,
|
| 15 |
+
DuckDuckGoSearchTool,
|
| 16 |
+
HfApiModel
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
+
|
| 22 |
+
print("Initializing Smart Agent...")
|
| 23 |
+
|
| 24 |
+
# Web search tool
|
| 25 |
+
search_tool = DuckDuckGoSearchTool()
|
| 26 |
+
|
| 27 |
+
# Free Hugging Face model
|
| 28 |
+
model = HfApiModel(
|
| 29 |
+
model_id="meta-llama/Llama-3.1-8B-Instruct"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Main Agent
|
| 33 |
+
self.agent = CodeAgent(
|
| 34 |
+
tools=[search_tool],
|
| 35 |
+
model=model,
|
| 36 |
+
add_base_tools=True,
|
| 37 |
+
max_steps=5
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
def __call__(self, question: str) -> str:
|
| 41 |
+
|
| 42 |
+
print(f"Question: {question}")
|
| 43 |
+
|
| 44 |
+
prompt = f"""
|
| 45 |
+
Answer the following question.
|
| 46 |
+
|
| 47 |
+
IMPORTANT:
|
| 48 |
+
- Return ONLY the final answer
|
| 49 |
+
- Do NOT explain
|
| 50 |
+
- Do NOT write 'FINAL ANSWER'
|
| 51 |
+
- Keep the answer short and exact
|
| 52 |
+
|
| 53 |
+
Question:
|
| 54 |
+
{question}
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
response = self.agent.run(prompt)
|
| 59 |
+
|
| 60 |
+
# convert to clean string
|
| 61 |
+
answer = str(response).strip()
|
| 62 |
+
|
| 63 |
+
print(f"Agent answer: {answer}")
|
| 64 |
+
|
| 65 |
+
return answer
|
| 66 |
+
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Error: {e}")
|
| 69 |
+
|
| 70 |
+
return "Error"
|
| 71 |
|
| 72 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 73 |
"""
|