Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,64 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from agent import BasicAgent
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
#class BasicAgent:
|
| 15 |
-
# def __init__(self):
|
| 16 |
-
# print("BasicAgent initialized.")
|
| 17 |
-
# def __call__(self, question: str) -> str:
|
| 18 |
-
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 19 |
-
# fixed_answer = "This is a default answer."
|
| 20 |
-
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 21 |
-
# return fixed_answer
|
| 22 |
-
|
| 23 |
-
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 24 |
"""
|
| 25 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 26 |
and displays the results.
|
| 27 |
"""
|
| 28 |
-
# --- Determine HF Space Runtime URL and Repo URL
|
| 29 |
-
space_id = os.getenv("SPACE_ID")
|
| 30 |
|
| 31 |
if profile:
|
| 32 |
-
username= f"{profile.username}"
|
| 33 |
print(f"User logged in: {username}")
|
| 34 |
else:
|
| 35 |
print("User not logged in.")
|
| 36 |
-
return "Please
|
| 37 |
|
| 38 |
api_url = DEFAULT_API_URL
|
| 39 |
questions_url = f"{api_url}/questions"
|
| 40 |
submit_url = f"{api_url}/submit"
|
| 41 |
|
| 42 |
-
# 1. Instantiate
|
| 43 |
try:
|
| 44 |
agent = BasicAgent()
|
| 45 |
except Exception as e:
|
| 46 |
print(f"Error instantiating agent: {e}")
|
| 47 |
return f"Error initializing agent: {e}", None
|
| 48 |
-
|
| 49 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 50 |
print(agent_code)
|
| 51 |
|
| 52 |
-
# 2. Fetch
|
| 53 |
print(f"Fetching questions from: {questions_url}")
|
| 54 |
try:
|
| 55 |
response = requests.get(questions_url, timeout=15)
|
| 56 |
response.raise_for_status()
|
| 57 |
questions_data = response.json()
|
| 58 |
if not questions_data:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
print(f"Fetched {len(questions_data)} questions.")
|
| 62 |
except requests.exceptions.RequestException as e:
|
| 63 |
print(f"Error fetching questions: {e}")
|
| 64 |
return f"Error fetching questions: {e}", None
|
| 65 |
except requests.exceptions.JSONDecodeError as e:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
except Exception as e:
|
| 70 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 71 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 72 |
|
| 73 |
-
# 3. Run
|
| 74 |
results_log = []
|
| 75 |
answers_payload = []
|
| 76 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
@@ -82,22 +70,42 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 82 |
continue
|
| 83 |
try:
|
| 84 |
submitted_answer = agent(question_text)
|
| 85 |
-
answers_payload.append(
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if not answers_payload:
|
| 92 |
print("Agent did not produce any answers to submit.")
|
| 93 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 94 |
|
| 95 |
-
# 4. Prepare
|
| 96 |
-
submission_data = {
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
print(status_update)
|
| 99 |
|
| 100 |
-
# 5. Submit
|
| 101 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 102 |
try:
|
| 103 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
@@ -141,21 +149,21 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 141 |
return status_message, results_df
|
| 142 |
|
| 143 |
|
| 144 |
-
# --- Build Gradio Interface using Blocks
|
| 145 |
with gr.Blocks() as demo:
|
| 146 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 147 |
gr.Markdown(
|
| 148 |
"""
|
| 149 |
**Instructions:**
|
| 150 |
|
| 151 |
-
1.
|
| 152 |
-
2. Log in to your Hugging Face account
|
| 153 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see
|
| 154 |
|
| 155 |
---
|
| 156 |
**Disclaimers:**
|
| 157 |
-
|
| 158 |
-
This
|
| 159 |
"""
|
| 160 |
)
|
| 161 |
|
|
@@ -163,35 +171,32 @@ with gr.Blocks() as demo:
|
|
| 163 |
|
| 164 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 165 |
|
| 166 |
-
status_output = gr.Textbox(
|
| 167 |
-
|
|
|
|
| 168 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 169 |
|
| 170 |
-
run_button.click(
|
| 171 |
-
fn=run_and_submit_all,
|
| 172 |
-
outputs=[status_output, results_table]
|
| 173 |
-
)
|
| 174 |
|
|
|
|
| 175 |
if __name__ == "__main__":
|
| 176 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 177 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 178 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 179 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 180 |
|
| 181 |
if space_host_startup:
|
| 182 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 183 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 184 |
else:
|
| 185 |
-
print("ℹ️ SPACE_HOST
|
| 186 |
|
| 187 |
-
if space_id_startup:
|
| 188 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 189 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 190 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 191 |
else:
|
| 192 |
-
print("ℹ️ SPACE_ID
|
| 193 |
-
|
| 194 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 195 |
|
|
|
|
| 196 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 197 |
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from agent import BasicAgent # ← importa il vero agente smolagents
|
| 6 |
|
| 7 |
+
# --- Constants -------------------------------------------------
|
|
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
+
# ---------------------------------------------------------------
|
| 11 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 14 |
and displays the results.
|
| 15 |
"""
|
| 16 |
+
# --- Determine HF Space Runtime URL and Repo URL ------------
|
| 17 |
+
space_id = os.getenv("SPACE_ID") # for the agent_code link
|
| 18 |
|
| 19 |
if profile:
|
| 20 |
+
username = f"{profile.username}"
|
| 21 |
print(f"User logged in: {username}")
|
| 22 |
else:
|
| 23 |
print("User not logged in.")
|
| 24 |
+
return "Please login to Hugging Face with the button.", None
|
| 25 |
|
| 26 |
api_url = DEFAULT_API_URL
|
| 27 |
questions_url = f"{api_url}/questions"
|
| 28 |
submit_url = f"{api_url}/submit"
|
| 29 |
|
| 30 |
+
# 1. Instantiate the agent -----------------------------------
|
| 31 |
try:
|
| 32 |
agent = BasicAgent()
|
| 33 |
except Exception as e:
|
| 34 |
print(f"Error instantiating agent: {e}")
|
| 35 |
return f"Error initializing agent: {e}", None
|
| 36 |
+
|
| 37 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 38 |
print(agent_code)
|
| 39 |
|
| 40 |
+
# 2. Fetch questions -----------------------------------------
|
| 41 |
print(f"Fetching questions from: {questions_url}")
|
| 42 |
try:
|
| 43 |
response = requests.get(questions_url, timeout=15)
|
| 44 |
response.raise_for_status()
|
| 45 |
questions_data = response.json()
|
| 46 |
if not questions_data:
|
| 47 |
+
print("Fetched questions list is empty.")
|
| 48 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 49 |
print(f"Fetched {len(questions_data)} questions.")
|
| 50 |
except requests.exceptions.RequestException as e:
|
| 51 |
print(f"Error fetching questions: {e}")
|
| 52 |
return f"Error fetching questions: {e}", None
|
| 53 |
except requests.exceptions.JSONDecodeError as e:
|
| 54 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 55 |
+
print(f"Response text: {response.text[:500]}")
|
| 56 |
+
return f"Error decoding server response for questions: {e}", None
|
| 57 |
except Exception as e:
|
| 58 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 59 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 60 |
|
| 61 |
+
# 3. Run the agent -------------------------------------------
|
| 62 |
results_log = []
|
| 63 |
answers_payload = []
|
| 64 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 70 |
continue
|
| 71 |
try:
|
| 72 |
submitted_answer = agent(question_text)
|
| 73 |
+
answers_payload.append(
|
| 74 |
+
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 75 |
+
)
|
| 76 |
+
results_log.append(
|
| 77 |
+
{
|
| 78 |
+
"Task ID": task_id,
|
| 79 |
+
"Question": question_text,
|
| 80 |
+
"Submitted Answer": submitted_answer,
|
| 81 |
+
}
|
| 82 |
+
)
|
| 83 |
except Exception as e:
|
| 84 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 85 |
+
results_log.append(
|
| 86 |
+
{
|
| 87 |
+
"Task ID": task_id,
|
| 88 |
+
"Question": question_text,
|
| 89 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
| 90 |
+
}
|
| 91 |
+
)
|
| 92 |
|
| 93 |
if not answers_payload:
|
| 94 |
print("Agent did not produce any answers to submit.")
|
| 95 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 96 |
|
| 97 |
+
# 4. Prepare submission --------------------------------------
|
| 98 |
+
submission_data = {
|
| 99 |
+
"username": username.strip(),
|
| 100 |
+
"agent_code": agent_code,
|
| 101 |
+
"answers": answers_payload,
|
| 102 |
+
}
|
| 103 |
+
status_update = (
|
| 104 |
+
f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 105 |
+
)
|
| 106 |
print(status_update)
|
| 107 |
|
| 108 |
+
# 5. Submit ---------------------------------------------------
|
| 109 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 110 |
try:
|
| 111 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 149 |
return status_message, results_df
|
| 150 |
|
| 151 |
|
| 152 |
+
# --- Build Gradio Interface using Blocks -----------------------
|
| 153 |
with gr.Blocks() as demo:
|
| 154 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 155 |
gr.Markdown(
|
| 156 |
"""
|
| 157 |
**Instructions:**
|
| 158 |
|
| 159 |
+
1. Clone this space, then modify the code to define your agent logic, tools, etc.
|
| 160 |
+
2. Log in to your Hugging Face account with the button below. Your HF username is used for submission.
|
| 161 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch the questions, run your agent, submit answers, and see your score.
|
| 162 |
|
| 163 |
---
|
| 164 |
**Disclaimers:**
|
| 165 |
+
Running can take some time because the agent must answer all questions.
|
| 166 |
+
This setup is intentionally minimal; feel free to optimise (e.g. async, caching).
|
| 167 |
"""
|
| 168 |
)
|
| 169 |
|
|
|
|
| 171 |
|
| 172 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 173 |
|
| 174 |
+
status_output = gr.Textbox(
|
| 175 |
+
label="Run Status / Submission Result", lines=5, interactive=False
|
| 176 |
+
)
|
| 177 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 178 |
|
| 179 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
# --- Launch -----------------------------------------------------
|
| 182 |
if __name__ == "__main__":
|
| 183 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
|
|
|
| 184 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 185 |
+
space_id_startup = os.getenv("SPACE_ID")
|
| 186 |
|
| 187 |
if space_host_startup:
|
| 188 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 189 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 190 |
else:
|
| 191 |
+
print("ℹ️ SPACE_HOST env variable not found (running locally?).")
|
| 192 |
|
| 193 |
+
if space_id_startup:
|
| 194 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 195 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 196 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 197 |
else:
|
| 198 |
+
print("ℹ️ SPACE_ID env variable not found (running locally?).")
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 201 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 202 |
demo.launch(debug=True, share=False)
|