Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import os
|
|
| 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,35 +10,32 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 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 |
-
|
| 18 |
-
|
| 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-
|
| 24 |
token=hf_token
|
| 25 |
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
|
| 29 |
|
|
|
|
| 30 |
self.agent = CodeAgent(
|
| 31 |
tools=self.tools,
|
| 32 |
model=self.model,
|
| 33 |
-
add_base_tools=True
|
| 34 |
)
|
| 35 |
|
| 36 |
def __call__(self, question: str) -> str:
|
| 37 |
try:
|
| 38 |
-
# We
|
| 39 |
prompt = (
|
| 40 |
f"{question}\n\n"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
)
|
| 44 |
result = self.agent.run(prompt)
|
| 45 |
return str(result)
|
|
@@ -59,7 +55,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 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,22 +62,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 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(),
|
| 83 |
-
"agent_code": agent_code_link,
|
| 84 |
-
"answers": answers_payload
|
| 85 |
-
}
|
| 86 |
|
| 87 |
submit_response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 88 |
submit_response.raise_for_status()
|
|
@@ -91,20 +81,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 91 |
final_status = (
|
| 92 |
f"Submission Successful!\n"
|
| 93 |
f"User: {result_data.get('username')}\n"
|
| 94 |
-
f"
|
| 95 |
f"Message: {result_data.get('message')}"
|
| 96 |
)
|
| 97 |
return final_status, pd.DataFrame(results_log)
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
-
return f"
|
| 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="
|
| 108 |
results_table = gr.DataFrame(label="Agent Reasoning Trace", wrap=True)
|
| 109 |
|
| 110 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool
|
| 6 |
|
| 7 |
# --- Constants ---
|
|
|
|
| 10 |
# --- Agent Definition ---
|
| 11 |
class AgentArchitect:
|
| 12 |
def __init__(self):
|
|
|
|
| 13 |
hf_token = os.getenv("HF_TOKEN")
|
| 14 |
|
| 15 |
+
# We use the 7B Coder model. It is almost certainly on the FREE Serverless tier.
|
| 16 |
+
# This avoids hitting the 'nscale' or other paid Inference Providers.
|
|
|
|
|
|
|
|
|
|
| 17 |
self.model = InferenceClientModel(
|
| 18 |
+
model_id="Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 19 |
token=hf_token
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Essential tools for GAIA: Search + Deep Scrapping
|
| 23 |
self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
|
| 24 |
|
| 25 |
+
# CodeAgent is the best scaffold for technical tasks
|
| 26 |
self.agent = CodeAgent(
|
| 27 |
tools=self.tools,
|
| 28 |
model=self.model,
|
| 29 |
+
add_base_tools=True # Gives it Python for math/data processing
|
| 30 |
)
|
| 31 |
|
| 32 |
def __call__(self, question: str) -> str:
|
| 33 |
try:
|
| 34 |
+
# We add a strict formatting instruction to help with Exact Match scoring
|
| 35 |
prompt = (
|
| 36 |
f"{question}\n\n"
|
| 37 |
+
"Final Answer Requirement: Use your tools to find the information. "
|
| 38 |
+
"Provide ONLY the final answer concisely (e.g., just the name, number, or list)."
|
| 39 |
)
|
| 40 |
result = self.agent.run(prompt)
|
| 41 |
return str(result)
|
|
|
|
| 55 |
|
| 56 |
try:
|
| 57 |
agent_instance = AgentArchitect()
|
|
|
|
| 58 |
response = requests.get(questions_url, timeout=15)
|
| 59 |
response.raise_for_status()
|
| 60 |
questions_data = response.json()
|
|
|
|
| 62 |
results_log = []
|
| 63 |
answers_payload = []
|
| 64 |
|
| 65 |
+
# The agent will now process the questions using the free tier model
|
| 66 |
for item in questions_data:
|
| 67 |
task_id = item.get("task_id")
|
| 68 |
question_text = item.get("question")
|
|
|
|
|
|
|
| 69 |
submitted_answer = agent_instance(question_text)
|
|
|
|
| 70 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 71 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 72 |
|
| 73 |
+
# Submission Logic
|
| 74 |
agent_code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 75 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code_link, "answers": answers_payload}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
submit_response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 78 |
submit_response.raise_for_status()
|
|
|
|
| 81 |
final_status = (
|
| 82 |
f"Submission Successful!\n"
|
| 83 |
f"User: {result_data.get('username')}\n"
|
| 84 |
+
f"Score: {result_data.get('score')}% \n"
|
| 85 |
f"Message: {result_data.get('message')}"
|
| 86 |
)
|
| 87 |
return final_status, pd.DataFrame(results_log)
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
+
return f"Error during run: {e}", None
|
| 91 |
|
| 92 |
# --- Gradio UI ---
|
| 93 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 94 |
+
gr.Markdown("# 🚀 Professional Agent Evaluator (Free Tier Optimized)")
|
| 95 |
gr.LoginButton()
|
| 96 |
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 97 |
+
status_output = gr.Textbox(label="Submission Result", lines=5)
|
| 98 |
results_table = gr.DataFrame(label="Agent Reasoning Trace", wrap=True)
|
| 99 |
|
| 100 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|