Spaces:
Runtime error
Runtime error
Refactor app.py to implement SmolAgent with Hugging Face integration, replacing BasicAgent.
Browse files
app.py
CHANGED
|
@@ -3,21 +3,53 @@ import gradio as gr
|
|
| 3 |
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"
|
|
|
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class
|
| 14 |
-
def __init__(self):
|
| 15 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -40,7 +72,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
@@ -142,16 +174,21 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 142 |
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
-
gr.Markdown("#
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
|
| 150 |
-
1.
|
| 151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 153 |
|
| 154 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
**Disclaimers:**
|
| 156 |
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).
|
| 157 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from smolagents import CodeAgent, InferenceClientModel
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
|
| 16 |
+
# --- Smol Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
+
class SmolAgent:
|
| 19 |
+
def __init__(self, hf_token: str):
|
| 20 |
+
print("Initializing SmolAgent...")
|
| 21 |
+
if not hf_token:
|
| 22 |
+
raise ValueError("Hugging Face token not found. Please set HF_TOKEN environment variable.")
|
| 23 |
+
|
| 24 |
+
# 3. Initialize the SmolLM model
|
| 25 |
+
model = InferenceClientModel(
|
| 26 |
+
model_id="HuggingFaceTB/SmolLM-360M-Instruct",
|
| 27 |
+
token=hf_token,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# 4. Replace your current BasicAgent with a smolagents.CodeAgent
|
| 31 |
+
self._agent = CodeAgent(
|
| 32 |
+
tools=[],
|
| 33 |
+
model=model,
|
| 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 |
+
# 6. Prompt carefully - optimized for evaluation tasks
|
| 40 |
+
prompt = f"""Answer the following question with a short, direct response. Be concise and accurate:
|
| 41 |
+
|
| 42 |
+
Question: {question}
|
| 43 |
+
|
| 44 |
+
Answer:"""
|
| 45 |
+
try:
|
| 46 |
+
answer = self._agent.run(prompt)
|
| 47 |
+
print(f"Agent returning answer: {answer}")
|
| 48 |
+
return str(answer)
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error running agent: {e}")
|
| 51 |
+
return f"AGENT ERROR: {e}"
|
| 52 |
+
|
| 53 |
|
| 54 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 55 |
"""
|
|
|
|
| 72 |
|
| 73 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 74 |
try:
|
| 75 |
+
agent = SmolAgent(hf_token=HF_TOKEN)
|
| 76 |
except Exception as e:
|
| 77 |
print(f"Error instantiating agent: {e}")
|
| 78 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 174 |
|
| 175 |
# --- Build Gradio Interface using Blocks ---
|
| 176 |
with gr.Blocks() as demo:
|
| 177 |
+
gr.Markdown("# SmolLM Agent Evaluation Runner")
|
| 178 |
gr.Markdown(
|
| 179 |
"""
|
| 180 |
**Instructions:**
|
| 181 |
|
| 182 |
+
1. This space uses SmolLM-360M-Instruct model with smolagents for question answering.
|
| 183 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 184 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 185 |
|
| 186 |
---
|
| 187 |
+
**Model Information:**
|
| 188 |
+
- Using: HuggingFaceTB/SmolLM-360M-Instruct
|
| 189 |
+
- Framework: smolagents CodeAgent
|
| 190 |
+
- No additional tools (pure reasoning)
|
| 191 |
+
|
| 192 |
**Disclaimers:**
|
| 193 |
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).
|
| 194 |
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|