Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool,
|
| 5 |
|
| 6 |
-
# 1.
|
| 7 |
@tool
|
| 8 |
def get_file_content_tool(task_id: str) -> str:
|
| 9 |
"""
|
| 10 |
-
Downloads
|
| 11 |
|
| 12 |
Args:
|
| 13 |
task_id: The ID of the task requiring a file.
|
|
@@ -18,12 +18,11 @@ def get_file_content_tool(task_id: str) -> str:
|
|
| 18 |
filename = f"temp_{task_id}"
|
| 19 |
with open(filename, "wb") as f:
|
| 20 |
f.write(response.content)
|
| 21 |
-
return f"File downloaded successfully as {filename}.
|
| 22 |
return f"Failed to download file for task {task_id}"
|
| 23 |
|
| 24 |
-
# 2. Initialize the model and the
|
| 25 |
-
|
| 26 |
-
model = HfApiModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
|
| 27 |
|
| 28 |
agent = CodeAgent(
|
| 29 |
tools=[DuckDuckGoSearchTool(), get_file_content_tool],
|
|
@@ -35,14 +34,14 @@ agent = CodeAgent(
|
|
| 35 |
# 3. Core function to run evaluation across questions
|
| 36 |
def run_evaluation(username):
|
| 37 |
if not username:
|
| 38 |
-
return "Please log in with your Hugging Face account first."
|
| 39 |
|
| 40 |
api_base = "https://agents-course-unit4-eval.hf.space"
|
| 41 |
|
| 42 |
# Fetch questions
|
| 43 |
resp = requests.get(f"{api_base}/questions")
|
| 44 |
if resp.status_code != 200:
|
| 45 |
-
return f"Failed to fetch questions: {resp.text}"
|
| 46 |
|
| 47 |
questions_data = resp.json()
|
| 48 |
answers = []
|
|
@@ -55,12 +54,8 @@ def run_evaluation(username):
|
|
| 55 |
log_output += f"\n--- Processing Task ID: {task_id} ---\nQuestion: {question_text}\n"
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
# Run the agent on the question prompt
|
| 59 |
-
# Instruction reminder: Keep answers direct for exact match scoring!
|
| 60 |
prompt = f"{question_text}\nProvide ONLY the direct, final answer string with no conversational padding."
|
| 61 |
raw_answer = agent.run(prompt)
|
| 62 |
-
|
| 63 |
-
# Clean up output string format if needed
|
| 64 |
clean_answer = str(raw_answer).strip()
|
| 65 |
|
| 66 |
answers.append({
|
|
@@ -76,7 +71,7 @@ def run_evaluation(username):
|
|
| 76 |
})
|
| 77 |
|
| 78 |
# 4. Submit results to the API
|
| 79 |
-
code_link = f"https://huggingface.co/spaces/{username}/
|
| 80 |
|
| 81 |
payload = {
|
| 82 |
"username": username,
|
|
@@ -91,9 +86,9 @@ def run_evaluation(username):
|
|
| 91 |
else:
|
| 92 |
log_output += f"\nSubmission Failed: {sub_resp.text}"
|
| 93 |
|
| 94 |
-
return log_output
|
| 95 |
|
| 96 |
-
# 5. Build Gradio UI Wrapper
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("# Custom Agent Evaluation Runner")
|
| 99 |
gr.Markdown("Click below to run your custom `smolagents` code against the Unit 4 benchmark questions and submit automatically.")
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfHubModel, tool
|
| 5 |
|
| 6 |
+
# 1. Custom tool to download files if needed for tasks
|
| 7 |
@tool
|
| 8 |
def get_file_content_tool(task_id: str) -> str:
|
| 9 |
"""
|
| 10 |
+
Downloads files associated with a specific task ID from the course API.
|
| 11 |
|
| 12 |
Args:
|
| 13 |
task_id: The ID of the task requiring a file.
|
|
|
|
| 18 |
filename = f"temp_{task_id}"
|
| 19 |
with open(filename, "wb") as f:
|
| 20 |
f.write(response.content)
|
| 21 |
+
return f"File downloaded successfully as {filename}."
|
| 22 |
return f"Failed to download file for task {task_id}"
|
| 23 |
|
| 24 |
+
# 2. Initialize the model and the CodeAgent using the correct HfHubModel import
|
| 25 |
+
model = HfHubModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
|
|
|
|
| 26 |
|
| 27 |
agent = CodeAgent(
|
| 28 |
tools=[DuckDuckGoSearchTool(), get_file_content_tool],
|
|
|
|
| 34 |
# 3. Core function to run evaluation across questions
|
| 35 |
def run_evaluation(username):
|
| 36 |
if not username:
|
| 37 |
+
return "Please log in with your Hugging Face account first."
|
| 38 |
|
| 39 |
api_base = "https://agents-course-unit4-eval.hf.space"
|
| 40 |
|
| 41 |
# Fetch questions
|
| 42 |
resp = requests.get(f"{api_base}/questions")
|
| 43 |
if resp.status_code != 200:
|
| 44 |
+
return f"Failed to fetch questions: {resp.text}"
|
| 45 |
|
| 46 |
questions_data = resp.json()
|
| 47 |
answers = []
|
|
|
|
| 54 |
log_output += f"\n--- Processing Task ID: {task_id} ---\nQuestion: {question_text}\n"
|
| 55 |
|
| 56 |
try:
|
|
|
|
|
|
|
| 57 |
prompt = f"{question_text}\nProvide ONLY the direct, final answer string with no conversational padding."
|
| 58 |
raw_answer = agent.run(prompt)
|
|
|
|
|
|
|
| 59 |
clean_answer = str(raw_answer).strip()
|
| 60 |
|
| 61 |
answers.append({
|
|
|
|
| 71 |
})
|
| 72 |
|
| 73 |
# 4. Submit results to the API
|
| 74 |
+
code_link = f"https://huggingface.co/spaces/{username}/Final_Assignment_Template/tree/main"
|
| 75 |
|
| 76 |
payload = {
|
| 77 |
"username": username,
|
|
|
|
| 86 |
else:
|
| 87 |
log_output += f"\nSubmission Failed: {sub_resp.text}"
|
| 88 |
|
| 89 |
+
return log_output
|
| 90 |
|
| 91 |
+
# 5. Build Gradio UI Wrapper
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
gr.Markdown("# Custom Agent Evaluation Runner")
|
| 94 |
gr.Markdown("Click below to run your custom `smolagents` code against the Unit 4 benchmark questions and submit automatically.")
|