Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
# 2026 standard: InferenceClientModel
|
| 6 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool
|
| 7 |
|
| 8 |
# --- Constants ---
|
|
@@ -11,19 +11,22 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 11 |
# --- Agent Definition ---
|
| 12 |
class AgentArchitect:
|
| 13 |
def __init__(self):
|
| 14 |
-
#
|
| 15 |
hf_token = os.getenv("HF_TOKEN")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
self.model = InferenceClientModel(
|
| 20 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 21 |
token=hf_token
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
|
| 25 |
|
| 26 |
-
# CodeAgent allows the LLM to write Python to solve the benchmark
|
| 27 |
self.agent = CodeAgent(
|
| 28 |
tools=self.tools,
|
| 29 |
model=self.model,
|
|
@@ -32,11 +35,11 @@ class AgentArchitect:
|
|
| 32 |
|
| 33 |
def __call__(self, question: str) -> str:
|
| 34 |
try:
|
| 35 |
-
# We enforce
|
| 36 |
prompt = (
|
| 37 |
f"{question}\n\n"
|
| 38 |
-
f"Instructions:
|
| 39 |
-
f"Provide ONLY the final
|
| 40 |
)
|
| 41 |
result = self.agent.run(prompt)
|
| 42 |
return str(result)
|
|
@@ -55,10 +58,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 55 |
submit_url = f"{api_url}/submit"
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
# Initialize the new free-tier agent
|
| 59 |
agent_instance = AgentArchitect()
|
| 60 |
|
| 61 |
-
# 1. Fetch Questions
|
| 62 |
response = requests.get(questions_url, timeout=15)
|
| 63 |
response.raise_for_status()
|
| 64 |
questions_data = response.json()
|
|
@@ -66,18 +67,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 66 |
results_log = []
|
| 67 |
answers_payload = []
|
| 68 |
|
| 69 |
-
# 2. Process all 20 questions
|
| 70 |
for item in questions_data:
|
| 71 |
task_id = item.get("task_id")
|
| 72 |
question_text = item.get("question")
|
| 73 |
|
| 74 |
-
# The agent
|
| 75 |
submitted_answer = agent_instance(question_text)
|
| 76 |
|
| 77 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 78 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 79 |
|
| 80 |
-
# 3. Submit to the course leaderboard
|
| 81 |
agent_code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 82 |
submission_data = {
|
| 83 |
"username": username.strip(),
|
|
@@ -98,13 +97,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 98 |
return final_status, pd.DataFrame(results_log)
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
-
return f"
|
| 102 |
|
| 103 |
# --- Gradio UI ---
|
| 104 |
-
with gr.Blocks(theme=gr.themes.
|
| 105 |
-
gr.Markdown("# 🚀
|
| 106 |
gr.LoginButton()
|
| 107 |
-
run_button = gr.Button("Run Evaluation & Submit", variant="primary")
|
| 108 |
status_output = gr.Textbox(label="Leaderboard Status", lines=4)
|
| 109 |
results_table = gr.DataFrame(label="Agent Reasoning Trace", wrap=True)
|
| 110 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
# 2026 standard: InferenceClientModel is the robust way to access free models
|
| 6 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool
|
| 7 |
|
| 8 |
# --- Constants ---
|
|
|
|
| 11 |
# --- Agent Definition ---
|
| 12 |
class AgentArchitect:
|
| 13 |
def __init__(self):
|
| 14 |
+
# SECURE: Fetches the token from your Space Secrets
|
| 15 |
hf_token = os.getenv("HF_TOKEN")
|
| 16 |
|
| 17 |
+
if not hf_token:
|
| 18 |
+
print("CRITICAL: HF_TOKEN is missing. Please add it to your Space Secrets!")
|
| 19 |
+
|
| 20 |
+
# InferenceClientModel is the 2026 gateway for free Serverless Inference.
|
| 21 |
+
# We use Qwen 2.5 Coder 32B because it has the highest reasoning-to-speed ratio.
|
| 22 |
self.model = InferenceClientModel(
|
| 23 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 24 |
token=hf_token
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# This will now work perfectly because 'ddgs' is in your requirements.txt
|
| 28 |
self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
|
| 29 |
|
|
|
|
| 30 |
self.agent = CodeAgent(
|
| 31 |
tools=self.tools,
|
| 32 |
model=self.model,
|
|
|
|
| 35 |
|
| 36 |
def __call__(self, question: str) -> str:
|
| 37 |
try:
|
| 38 |
+
# We enforce a concise output format to satisfy the grader's 'Exact Match' logic.
|
| 39 |
prompt = (
|
| 40 |
f"{question}\n\n"
|
| 41 |
+
f"Instructions: Think step-by-step. Solve the problem using your tools. "
|
| 42 |
+
f"Provide ONLY the final, concise answer."
|
| 43 |
)
|
| 44 |
result = self.agent.run(prompt)
|
| 45 |
return str(result)
|
|
|
|
| 58 |
submit_url = f"{api_url}/submit"
|
| 59 |
|
| 60 |
try:
|
|
|
|
| 61 |
agent_instance = AgentArchitect()
|
| 62 |
|
|
|
|
| 63 |
response = requests.get(questions_url, timeout=15)
|
| 64 |
response.raise_for_status()
|
| 65 |
questions_data = response.json()
|
|
|
|
| 67 |
results_log = []
|
| 68 |
answers_payload = []
|
| 69 |
|
|
|
|
| 70 |
for item in questions_data:
|
| 71 |
task_id = item.get("task_id")
|
| 72 |
question_text = item.get("question")
|
| 73 |
|
| 74 |
+
# The agent now has the 'ddgs' muscles to perform the search
|
| 75 |
submitted_answer = agent_instance(question_text)
|
| 76 |
|
| 77 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 78 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 79 |
|
|
|
|
| 80 |
agent_code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 81 |
submission_data = {
|
| 82 |
"username": username.strip(),
|
|
|
|
| 97 |
return final_status, pd.DataFrame(results_log)
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
+
return f"Submission Failed: {e}", None
|
| 101 |
|
| 102 |
# --- Gradio UI ---
|
| 103 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 104 |
+
gr.Markdown("# 🚀 Professional Agent Evaluator")
|
| 105 |
gr.LoginButton()
|
| 106 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 107 |
status_output = gr.Textbox(label="Leaderboard Status", lines=4)
|
| 108 |
results_table = gr.DataFrame(label="Agent Reasoning Trace", wrap=True)
|
| 109 |
|