Spaces:
Runtime error
Runtime error
Refactor SmolAgent to remove DuckDuckGoSearchTool, update SYSTEM_PROMPT for improved response format, and enhance answer parsing logic.
Browse files
app.py
CHANGED
|
@@ -3,26 +3,13 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import CodeAgent, InferenceClientModel
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
-
SYSTEM_PROMPT = """
|
| 13 |
-
You are a helpful assistant answering evaluation questions that will be scored using exact string match.
|
| 14 |
-
|
| 15 |
-
Instructions:
|
| 16 |
-
- Return ONLY the final answer on a single line.
|
| 17 |
-
- Do NOT include explanations or reasoning.
|
| 18 |
-
- Do NOT write "Answer:" or "FINAL ANSWER:" before your response.
|
| 19 |
-
- If the answer is a number, return the plain number with no commas, currency symbols, or units unless explicitly required.
|
| 20 |
-
- If the answer is a string, use lowercase, no articles, no abbreviations, and digits in plain text unless specified otherwise.
|
| 21 |
-
- If the answer is a comma-separated list, follow the above rules for each element with no spaces after commas.
|
| 22 |
-
- If unsure, guess concisely.
|
| 23 |
-
|
| 24 |
-
Always strictly follow these instructions to maximize exact match accuracy.
|
| 25 |
-
"""
|
| 26 |
|
| 27 |
|
| 28 |
# --- Smol Agent Definition ---
|
|
@@ -33,9 +20,6 @@ class SmolAgent:
|
|
| 33 |
if not hf_token:
|
| 34 |
raise ValueError("Hugging Face token not found. Please set HF_TOKEN environment variable in HF Spaces settings.")
|
| 35 |
|
| 36 |
-
# Initialize the tool
|
| 37 |
-
search_tool = DuckDuckGoSearchTool()
|
| 38 |
-
|
| 39 |
# 3. Initialize the SmolLM model
|
| 40 |
model = InferenceClientModel(
|
| 41 |
model_id="HuggingFaceTB/SmolLM-1.7B-Instruct",
|
|
@@ -48,17 +32,33 @@ class SmolAgent:
|
|
| 48 |
model=model,
|
| 49 |
instructions=SYSTEM_PROMPT,
|
| 50 |
)
|
| 51 |
-
print("SmolAgent initialized")
|
| 52 |
|
| 53 |
def __call__(self, question: str) -> str:
|
| 54 |
-
print(f"
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
-
print(f"
|
| 61 |
-
return f"
|
| 62 |
|
| 63 |
|
| 64 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
@@ -189,15 +189,15 @@ with gr.Blocks() as demo:
|
|
| 189 |
"""
|
| 190 |
**Instructions:**
|
| 191 |
|
| 192 |
-
1. This space uses SmolLM-
|
| 193 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 194 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 195 |
|
| 196 |
---
|
| 197 |
**Model Information:**
|
| 198 |
-
- Using: HuggingFaceTB/SmolLM-
|
| 199 |
- Framework: smolagents CodeAgent
|
| 200 |
-
-
|
| 201 |
|
| 202 |
**Disclaimers:**
|
| 203 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, InferenceClientModel
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
+
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
# --- Smol Agent Definition ---
|
|
|
|
| 20 |
if not hf_token:
|
| 21 |
raise ValueError("Hugging Face token not found. Please set HF_TOKEN environment variable in HF Spaces settings.")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
# 3. Initialize the SmolLM model
|
| 24 |
model = InferenceClientModel(
|
| 25 |
model_id="HuggingFaceTB/SmolLM-1.7B-Instruct",
|
|
|
|
| 32 |
model=model,
|
| 33 |
instructions=SYSTEM_PROMPT,
|
| 34 |
)
|
| 35 |
+
print("SmolAgent initialized.")
|
| 36 |
|
| 37 |
def __call__(self, question: str) -> str:
|
| 38 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 39 |
try:
|
| 40 |
+
# The system prompt is already set, just pass the question.
|
| 41 |
+
full_response = self._agent.run(question)
|
| 42 |
+
print(f"Agent full response: {full_response}")
|
| 43 |
+
|
| 44 |
+
# Parse the response to extract the final answer
|
| 45 |
+
if "FINAL ANSWER:" in full_response:
|
| 46 |
+
# Split and get the part after "FINAL ANSWER:"
|
| 47 |
+
final_answer_part = full_response.split("FINAL ANSWER:")[1]
|
| 48 |
+
# Remove brackets if they exist and strip whitespace
|
| 49 |
+
final_answer = final_answer_part.strip()
|
| 50 |
+
if final_answer.startswith('[') and final_answer.endswith(']'):
|
| 51 |
+
final_answer = final_answer[1:-1]
|
| 52 |
+
|
| 53 |
+
print(f"Agent returning parsed answer: {final_answer}")
|
| 54 |
+
return final_answer
|
| 55 |
+
else:
|
| 56 |
+
print("Warning: 'FINAL ANSWER:' not found in response. Returning full response.")
|
| 57 |
+
return full_response
|
| 58 |
+
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"Error running agent: {e}")
|
| 61 |
+
return f"AGENT ERROR: {e}"
|
| 62 |
|
| 63 |
|
| 64 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 189 |
"""
|
| 190 |
**Instructions:**
|
| 191 |
|
| 192 |
+
1. This space uses SmolLM-1.7B-Instruct model with smolagents for question answering.
|
| 193 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 194 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 195 |
|
| 196 |
---
|
| 197 |
**Model Information:**
|
| 198 |
+
- Using: HuggingFaceTB/SmolLM-1.7B-Instruct
|
| 199 |
- Framework: smolagents CodeAgent
|
| 200 |
+
- No additional tools (pure reasoning)
|
| 201 |
|
| 202 |
**Disclaimers:**
|
| 203 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|