Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,48 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import requests
|
| 4 |
import inspect
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# --- Basic Agent Definition ---
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
print("BasicAgent initialized
|
| 16 |
-
def __call__(self, question: str) -> str:
|
|
|
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -38,13 +63,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 38 |
questions_url = f"{api_url}/questions"
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
-
# 1. Instantiate Agent (
|
| 42 |
try:
|
| 43 |
agent = BasicAgent()
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
| 47 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase (
|
| 48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 49 |
print(agent_code)
|
| 50 |
|
|
@@ -75,14 +100,23 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 75 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 76 |
for item in questions_data:
|
| 77 |
task_id = item.get("task_id")
|
|
|
|
| 78 |
question_text = item.get("question")
|
|
|
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
-
|
|
|
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error running agent on task {task_id}: {e}")
|
| 88 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
@@ -146,15 +180,13 @@ with gr.Blocks() as demo:
|
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
-
|
| 150 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 153 |
-
|
| 154 |
---
|
| 155 |
**Disclaimers:**
|
| 156 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 157 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a
|
| 158 |
"""
|
| 159 |
)
|
| 160 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
|
|
|
| 3 |
import inspect
|
| 4 |
+
import urllib3
|
| 5 |
+
import tempfile
|
| 6 |
+
import requests
|
| 7 |
+
import gradio as gr
|
| 8 |
import pandas as pd
|
| 9 |
+
from agent import start_agent
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
urllib3.disable_warnings()
|
| 13 |
+
|
| 14 |
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
|
|
|
|
|
|
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
+
print("BasicAgent initialized")
|
| 22 |
+
def __call__(self, question: str, question_filepath: str = "") -> str:
|
| 23 |
+
answer = start_agent(question, question_filepath)
|
| 24 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 25 |
+
print(f"Agent returning answer: {answer}")
|
| 26 |
+
return answer
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def download_question_file(task_id: str, filename: str)-> str | None:
|
| 30 |
+
try:
|
| 31 |
+
response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", stream=True)
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
|
| 34 |
+
temp_dir = tempfile.gettempdir()
|
| 35 |
+
filepath = os.path.join(temp_dir, filename)
|
| 36 |
+
|
| 37 |
+
with open(filepath, 'wb') as file:
|
| 38 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 39 |
+
file.write(chunk)
|
| 40 |
+
|
| 41 |
+
return filepath
|
| 42 |
+
except requests.exceptions.RequestException as e:
|
| 43 |
+
print(f"Error downloading file: {e}")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
|
| 47 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 48 |
"""
|
|
|
|
| 63 |
questions_url = f"{api_url}/questions"
|
| 64 |
submit_url = f"{api_url}/submit"
|
| 65 |
|
| 66 |
+
# 1. Instantiate Agent (modify this part to create your agent)
|
| 67 |
try:
|
| 68 |
agent = BasicAgent()
|
| 69 |
except Exception as e:
|
| 70 |
print(f"Error instantiating agent: {e}")
|
| 71 |
return f"Error initializing agent: {e}", None
|
| 72 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase (usefull for others, so please keep it public)
|
| 73 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 74 |
print(agent_code)
|
| 75 |
|
|
|
|
| 100 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 101 |
for item in questions_data:
|
| 102 |
task_id = item.get("task_id")
|
| 103 |
+
file_name = item.get("file_name")
|
| 104 |
question_text = item.get("question")
|
| 105 |
+
question_filepath = ""
|
| 106 |
if not task_id or question_text is None:
|
| 107 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 108 |
continue
|
| 109 |
+
|
| 110 |
+
if file_name:
|
| 111 |
+
question_filepath = download_question_file(task_id=task_id, filename=file_name)
|
| 112 |
+
|
| 113 |
try:
|
| 114 |
+
|
| 115 |
+
submitted_answer = agent(question_text, question_filepath)
|
| 116 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 117 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 118 |
+
time.sleep(60) #sleep in order not to hit claude token limit
|
| 119 |
+
|
| 120 |
except Exception as e:
|
| 121 |
print(f"Error running agent on task {task_id}: {e}")
|
| 122 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
| 180 |
gr.Markdown(
|
| 181 |
"""
|
| 182 |
**Instructions:**
|
|
|
|
| 183 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 184 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 185 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
| 186 |
---
|
| 187 |
**Disclaimers:**
|
| 188 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 189 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a separate action or even to answer the questions in async.
|
| 190 |
"""
|
| 191 |
)
|
| 192 |
|