Spaces:
Sleeping
Sleeping
mmmm
Browse files
app.py
CHANGED
|
@@ -1,96 +1,153 @@
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
AGENT_CODE_URL = f"https://huggingface.co/spaces/{HF_USERNAME}/YOUR_SPACE_NAME/tree/main"
|
| 7 |
-
API_BASE_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
-
|
| 9 |
-
def fetch_questions():
|
| 10 |
-
"""Retrieves the 20 GAIA level 1 validation questions from the API."""
|
| 11 |
-
print("Fetching questions from the API...")
|
| 12 |
-
response = requests.get(f"{API_BASE_URL}/questions")
|
| 13 |
-
if response.status_code == 200:
|
| 14 |
-
return response.json()
|
| 15 |
-
else:
|
| 16 |
-
raise Exception(f"Failed to fetch questions: {response.text}")
|
| 17 |
-
|
| 18 |
-
def download_task_file(task_id):
|
| 19 |
-
"""Downloads associated files if the task requires it."""
|
| 20 |
-
# The API documentation specifies: GET /files/{task_id}
|
| 21 |
-
# Use this if your agent needs to read PDFs, images, or CSVs locally
|
| 22 |
-
url = f"{API_BASE_URL}/files/{task_id}"
|
| 23 |
-
response = requests.get(url)
|
| 24 |
-
if response.status_code == 200:
|
| 25 |
-
file_path = f"./{task_id}_file" # You'll want to parse the header for exact extension
|
| 26 |
-
with open(file_path, "wb") as f:
|
| 27 |
-
f.write(response.content)
|
| 28 |
-
return file_path
|
| 29 |
-
return None
|
| 30 |
-
|
| 31 |
-
def dummy_agent_fallback(question_text):
|
| 32 |
-
"""
|
| 33 |
-
Placeholder for your real agent call.
|
| 34 |
-
REPLACE THIS with your actual LLM/Agent call!
|
| 35 |
-
"""
|
| 36 |
-
# Strict prompt constraint to force exact matches
|
| 37 |
-
system_prompt = (
|
| 38 |
-
"You are a precise QA bot. Answer the question using your tools. "
|
| 39 |
-
"Output ONLY the final string or number representing the answer. "
|
| 40 |
-
"Do not include 'The answer is...', do not include 'FINAL ANSWER', "
|
| 41 |
-
"and do not include any conversational filler."
|
| 42 |
-
)
|
| 43 |
-
# Your agent logic goes here (e.g., agent.run(question_text))
|
| 44 |
-
return "example_answer"
|
| 45 |
-
|
| 46 |
-
def submit_answers(submission_payload):
|
| 47 |
-
"""Submits the payload to the final leaderboard API."""
|
| 48 |
-
print("Submitting answers to the leaderboard...")
|
| 49 |
-
headers = {"Content-Type": "application/json"}
|
| 50 |
-
response = requests.post(f"{API_BASE_URL}/submit", json=submission_payload, headers=headers)
|
| 51 |
-
|
| 52 |
-
if response.status_code == 200:
|
| 53 |
-
print("🎉 Submission Successful!")
|
| 54 |
-
print("API Response:", response.json())
|
| 55 |
-
else:
|
| 56 |
-
print(f"❌ Submission Failed ({response.status_code}):", response.text)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|
| 96 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import inspect
|
| 5 |
+
import pandas as pd
|
| 6 |
+
# Import smolagents components
|
| 7 |
+
from smolagents import CodeAgent, HfApiClient, DuckDuckGoSearchTool, HfDropInEngine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# --- Constants ---
|
| 10 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
+
|
| 12 |
+
# --- Smart Agent Definition ---
|
| 13 |
+
# This overrides the original BasicAgent with a functioning tool-using agent loop
|
| 14 |
+
class BasicAgent:
|
| 15 |
+
def __init__(self):
|
| 16 |
+
print("Initializing smart CodeAgent...")
|
| 17 |
+
# 1. Setup the LLM engine (Llama-3.3-70B is great for reasoning tasks)
|
| 18 |
+
# Note: Ensure HUGGINGFACE_TOKEN is set in your Space Secrets
|
| 19 |
+
self.engine = HfDropInEngine(model_id="meta-llama/Llama-3.3-70B-Instruct")
|
| 20 |
|
| 21 |
+
# 2. Build the agent and equip it with a web search tool
|
| 22 |
+
self.agent = CodeAgent(
|
| 23 |
+
tools=[DuckDuckGoSearchTool()],
|
| 24 |
+
engine=self.engine,
|
| 25 |
+
max_steps=5 # Gives the agent steps to search, think, and fix errors
|
| 26 |
+
)
|
| 27 |
+
print("Smart Agent initialized successfully.")
|
| 28 |
+
|
| 29 |
+
def __call__(self, question: str) -> str:
|
| 30 |
+
print(f"\n[Agent Processing] Received question: {question[:100]}...")
|
| 31 |
|
| 32 |
+
# 3. Shape the prompt to force the exact match required by the leaderboard
|
| 33 |
+
strict_prompt = (
|
| 34 |
+
f"You are a precise, truth-seeking QA bot. Answer the following question using your tools:\n"
|
| 35 |
+
f"\"{question}\"\n\n"
|
| 36 |
+
"CRITICAL INSTRUCTION: Output ONLY the final answer value (e.g., a number, a specific date, or a name). "
|
| 37 |
+
"Do not write conversational filler, do not explain your steps in the final output, and DO NOT include "
|
| 38 |
+
"phrases like 'The answer is:' or 'FINAL ANSWER'."
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
try:
|
| 42 |
+
# Run the agent framework
|
| 43 |
+
raw_result = self.agent.run(strict_prompt)
|
| 44 |
+
final_answer = str(raw_result).strip()
|
| 45 |
+
print(f"[Agent Success] Output: {final_answer}")
|
| 46 |
+
return final_answer
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"[Agent Error] Failed to process task: {e}")
|
| 49 |
+
return "Error calculating answer"
|
| 50 |
+
|
| 51 |
+
# =====================================================================
|
| 52 |
+
# THE REST OF YOUR GRADIO CODE REMAINS EXACTLY THE SAME BELOW
|
| 53 |
+
# =====================================================================
|
| 54 |
+
|
| 55 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 56 |
+
"""
|
| 57 |
+
Fetches all questions, runs the upgraded BasicAgent on them, submits all answers,
|
| 58 |
+
and displays the results.
|
| 59 |
+
"""
|
| 60 |
+
space_id = os.getenv("SPACE_ID")
|
| 61 |
+
|
| 62 |
+
if profile:
|
| 63 |
+
username = f"{profile.username}"
|
| 64 |
+
print(f"User logged in: {username}")
|
| 65 |
+
else:
|
| 66 |
+
print("User not logged in.")
|
| 67 |
+
return "Please Login to Hugging Face with the button.", None
|
| 68 |
+
|
| 69 |
+
api_url = DEFAULT_API_URL
|
| 70 |
+
questions_url = f"{api_url}/questions"
|
| 71 |
+
submit_url = f"{api_url}/submit"
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
agent = BasicAgent()
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"Error instantiating agent: {e}")
|
| 77 |
+
return f"Error initializing agent: {e}", None
|
| 78 |
|
| 79 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 80 |
+
print(agent_code)
|
| 81 |
+
|
| 82 |
+
print(f"Fetching questions from: {questions_url}")
|
| 83 |
+
try:
|
| 84 |
+
response = requests.get(questions_url, timeout=15)
|
| 85 |
+
response.raise_for_status()
|
| 86 |
+
questions_data = response.json()
|
| 87 |
+
if not questions_data:
|
| 88 |
+
print("Fetched questions list is empty.")
|
| 89 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 90 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 93 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
| 94 |
+
|
| 95 |
+
results_log = []
|
| 96 |
+
answers_payload = []
|
| 97 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 98 |
+
for item in questions_data:
|
| 99 |
+
task_id = item.get("task_id")
|
| 100 |
+
question_text = item.get("question")
|
| 101 |
+
if not task_id or question_text is None:
|
| 102 |
+
continue
|
| 103 |
+
try:
|
| 104 |
+
submitted_answer = agent(question_text)
|
| 105 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 106 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 107 |
+
except Exception as e:
|
| 108 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 109 |
+
|
| 110 |
+
if not answers_payload:
|
| 111 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 112 |
+
|
| 113 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 114 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 115 |
|
| 116 |
+
try:
|
| 117 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 118 |
+
response.raise_for_status()
|
| 119 |
+
result_data = response.json()
|
| 120 |
+
final_status = (
|
| 121 |
+
f"Submission Successful!\n"
|
| 122 |
+
f"User: {result_data.get('username')}\n"
|
| 123 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 124 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 125 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 126 |
+
)
|
| 127 |
+
return final_status, pd.DataFrame(results_log)
|
| 128 |
+
except Exception as e:
|
| 129 |
+
status_message = f"Submission Failed: {e}"
|
| 130 |
+
return status_message, pd.DataFrame(results_log)
|
| 131 |
+
|
| 132 |
+
with gr.Blocks() as demo:
|
| 133 |
+
gr.Markdown("# Smart Agent Evaluation Runner")
|
| 134 |
+
gr.Markdown(
|
| 135 |
+
"""
|
| 136 |
+
**Instructions:**
|
| 137 |
+
1. Log in to your Hugging Face account using the button below.
|
| 138 |
+
2. Click 'Run Evaluation & Submit All Answers' to process the GAIA benchmark.
|
| 139 |
+
"""
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
gr.LoginButton()
|
| 143 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 144 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 145 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 146 |
+
|
| 147 |
+
run_button.click(
|
| 148 |
+
fn=run_and_submit_all,
|
| 149 |
+
outputs=[status_output, results_table]
|
| 150 |
+
)
|
| 151 |
|
| 152 |
if __name__ == "__main__":
|
| 153 |
+
demo.launch(debug=True, share=False)
|