d19
Browse files
app.py
CHANGED
|
@@ -3,46 +3,47 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from
|
| 7 |
-
from dotenv import load_dotenv
|
| 8 |
-
import json
|
| 9 |
-
from huggingface_hub import InferenceClient
|
| 10 |
-
import traceback
|
| 11 |
-
|
| 12 |
-
load_dotenv()
|
| 13 |
|
|
|
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
|
| 17 |
# --- Basic Agent Definition ---
|
| 18 |
-
|
| 19 |
-
|
| 20 |
class BasicAgent:
|
| 21 |
def __init__(self):
|
| 22 |
-
|
| 23 |
-
self.
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def __call__(self, question: str) -> str:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"Error calling inference API: {e}")
|
| 33 |
-
traceback.print_exc() # This will print the full traceback
|
| 34 |
-
return f"error: {e} | token used: {self.token}"
|
| 35 |
-
|
| 36 |
|
| 37 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 38 |
"""
|
| 39 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 40 |
and displays the results.
|
| 41 |
"""
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
if profile:
|
| 45 |
-
username
|
| 46 |
print(f"User logged in: {username}")
|
| 47 |
else:
|
| 48 |
print("User not logged in.")
|
|
@@ -52,13 +53,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 52 |
questions_url = f"{api_url}/questions"
|
| 53 |
submit_url = f"{api_url}/submit"
|
| 54 |
|
| 55 |
-
# 1. Instantiate Agent
|
| 56 |
try:
|
| 57 |
agent = BasicAgent()
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error instantiating agent: {e}")
|
| 60 |
return f"Error initializing agent: {e}", None
|
| 61 |
-
|
| 62 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 63 |
print(agent_code)
|
| 64 |
|
|
@@ -69,16 +70,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 69 |
response.raise_for_status()
|
| 70 |
questions_data = response.json()
|
| 71 |
if not questions_data:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
print(f"Fetched {len(questions_data)} questions.")
|
| 75 |
except requests.exceptions.RequestException as e:
|
| 76 |
print(f"Error fetching questions: {e}")
|
| 77 |
return f"Error fetching questions: {e}", None
|
| 78 |
-
except
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
except Exception as e:
|
| 83 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 84 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -96,29 +97,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 96 |
try:
|
| 97 |
submitted_answer = agent(question_text)
|
| 98 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 99 |
-
results_log.append({
|
| 100 |
-
"Task ID": task_id,
|
| 101 |
-
"Question": question_text,
|
| 102 |
-
"Submitted Answer": submitted_answer
|
| 103 |
-
})
|
| 104 |
except Exception as e:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
"Task ID": task_id,
|
| 108 |
-
"Question": question_text,
|
| 109 |
-
"Submitted Answer": f"AGENT ERROR: {e}"
|
| 110 |
-
})
|
| 111 |
|
| 112 |
if not answers_payload:
|
| 113 |
print("Agent did not produce any answers to submit.")
|
| 114 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 115 |
|
| 116 |
-
# 4. Prepare Submission
|
| 117 |
-
submission_data = {
|
| 118 |
-
"username": username.strip(),
|
| 119 |
-
"agent_code": agent_code,
|
| 120 |
-
"answers": answers_payload
|
| 121 |
-
}
|
| 122 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 123 |
print(status_update)
|
| 124 |
|
|
@@ -143,39 +132,36 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 143 |
try:
|
| 144 |
error_json = e.response.json()
|
| 145 |
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 146 |
-
except
|
| 147 |
error_detail += f" Response: {e.response.text[:500]}"
|
| 148 |
status_message = f"Submission Failed: {error_detail}"
|
| 149 |
print(status_message)
|
| 150 |
-
|
|
|
|
| 151 |
except requests.exceptions.Timeout:
|
| 152 |
status_message = "Submission Failed: The request timed out."
|
| 153 |
print(status_message)
|
| 154 |
-
|
|
|
|
| 155 |
except requests.exceptions.RequestException as e:
|
| 156 |
status_message = f"Submission Failed: Network error - {e}"
|
| 157 |
print(status_message)
|
| 158 |
-
|
|
|
|
| 159 |
except Exception as e:
|
| 160 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 161 |
print(status_message)
|
| 162 |
-
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
# --- Build Gradio Interface using Blocks ---
|
| 166 |
with gr.Blocks() as demo:
|
| 167 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 168 |
gr.Markdown(
|
| 169 |
-
""
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 173 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 174 |
-
---
|
| 175 |
-
**Disclaimers:**
|
| 176 |
-
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).
|
| 177 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance, to speed up the process, you could cache answers or run asynchronously.
|
| 178 |
-
"""
|
| 179 |
)
|
| 180 |
|
| 181 |
gr.LoginButton()
|
|
@@ -183,6 +169,7 @@ with gr.Blocks() as demo:
|
|
| 183 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 184 |
|
| 185 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 186 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 187 |
|
| 188 |
run_button.click(
|
|
@@ -191,24 +178,25 @@ with gr.Blocks() as demo:
|
|
| 191 |
)
|
| 192 |
|
| 193 |
if __name__ == "__main__":
|
| 194 |
-
print("\n" + "-"
|
|
|
|
| 195 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 196 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 197 |
|
| 198 |
if space_host_startup:
|
| 199 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 200 |
-
print(f"
|
| 201 |
else:
|
| 202 |
-
print("ℹ️
|
| 203 |
|
| 204 |
-
if space_id_startup:
|
| 205 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 206 |
-
print(f"
|
| 207 |
-
print(f"
|
| 208 |
else:
|
| 209 |
-
print("ℹ️
|
| 210 |
|
| 211 |
-
print("-"
|
| 212 |
|
| 213 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 214 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# (Keep Constants and BasicAgent class as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
# --- Basic Agent Definition ---
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
+
print("BasicAgent initialized.")
|
| 16 |
+
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
|
| 17 |
+
|
| 18 |
+
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and
|
| 19 |
+
finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 20 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated
|
| 21 |
+
list of numbers and/or strings.
|
| 22 |
+
If you are asked for a number, don't use comma to write your number neither use units such as $ or
|
| 23 |
+
percent sign unless specified otherwise.
|
| 24 |
+
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the
|
| 25 |
+
digits in plain text unless specified otherwise.
|
| 26 |
+
If you are asked for a comma separated list, apply the above rules depending of whether the element
|
| 27 |
+
to be put in the list is a number or a string.
|
| 28 |
+
"""
|
| 29 |
+
self.agent.prompt_templates["system_prompt"] = self.agent.prompt_templates["system_prompt"] + SYSTEM_PROMPT
|
| 30 |
+
|
| 31 |
def __call__(self, question: str) -> str:
|
| 32 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 33 |
+
final_answer = self.agent.run(question)
|
| 34 |
+
print(f"Agent returning final answer: {final_answer}")
|
| 35 |
+
return final_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 38 |
"""
|
| 39 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 40 |
and displays the results.
|
| 41 |
"""
|
| 42 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 43 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 44 |
|
| 45 |
if profile:
|
| 46 |
+
username= f"{profile.username}"
|
| 47 |
print(f"User logged in: {username}")
|
| 48 |
else:
|
| 49 |
print("User not logged in.")
|
|
|
|
| 53 |
questions_url = f"{api_url}/questions"
|
| 54 |
submit_url = f"{api_url}/submit"
|
| 55 |
|
| 56 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 57 |
try:
|
| 58 |
agent = BasicAgent()
|
| 59 |
except Exception as e:
|
| 60 |
print(f"Error instantiating agent: {e}")
|
| 61 |
return f"Error initializing agent: {e}", None
|
| 62 |
+
# 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)
|
| 63 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 64 |
print(agent_code)
|
| 65 |
|
|
|
|
| 70 |
response.raise_for_status()
|
| 71 |
questions_data = response.json()
|
| 72 |
if not questions_data:
|
| 73 |
+
print("Fetched questions list is empty.")
|
| 74 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 75 |
print(f"Fetched {len(questions_data)} questions.")
|
| 76 |
except requests.exceptions.RequestException as e:
|
| 77 |
print(f"Error fetching questions: {e}")
|
| 78 |
return f"Error fetching questions: {e}", None
|
| 79 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 80 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 81 |
+
print(f"Response text: {response.text[:500]}")
|
| 82 |
+
return f"Error decoding server response for questions: {e}", None
|
| 83 |
except Exception as e:
|
| 84 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 85 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 97 |
try:
|
| 98 |
submitted_answer = agent(question_text)
|
| 99 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 100 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
except Exception as e:
|
| 102 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 103 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
if not answers_payload:
|
| 106 |
print("Agent did not produce any answers to submit.")
|
| 107 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 108 |
|
| 109 |
+
# 4. Prepare Submission
|
| 110 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 112 |
print(status_update)
|
| 113 |
|
|
|
|
| 132 |
try:
|
| 133 |
error_json = e.response.json()
|
| 134 |
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 135 |
+
except requests.exceptions.JSONDecodeError:
|
| 136 |
error_detail += f" Response: {e.response.text[:500]}"
|
| 137 |
status_message = f"Submission Failed: {error_detail}"
|
| 138 |
print(status_message)
|
| 139 |
+
results_df = pd.DataFrame(results_log)
|
| 140 |
+
return status_message, results_df
|
| 141 |
except requests.exceptions.Timeout:
|
| 142 |
status_message = "Submission Failed: The request timed out."
|
| 143 |
print(status_message)
|
| 144 |
+
results_df = pd.DataFrame(results_log)
|
| 145 |
+
return status_message, results_df
|
| 146 |
except requests.exceptions.RequestException as e:
|
| 147 |
status_message = f"Submission Failed: Network error - {e}"
|
| 148 |
print(status_message)
|
| 149 |
+
results_df = pd.DataFrame(results_log)
|
| 150 |
+
return status_message, results_df
|
| 151 |
except Exception as e:
|
| 152 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 153 |
print(status_message)
|
| 154 |
+
results_df = pd.DataFrame(results_log)
|
| 155 |
+
return status_message, results_df
|
| 156 |
|
| 157 |
|
| 158 |
# --- Build Gradio Interface using Blocks ---
|
| 159 |
with gr.Blocks() as demo:
|
| 160 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 161 |
gr.Markdown(
|
| 162 |
+
"Please clone this space, then modify the code to define your agent's logic within the `BasicAgent` class. "
|
| 163 |
+
"Log in to your Hugging Face account using the button below. This uses your HF username for submission. "
|
| 164 |
+
"Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
)
|
| 166 |
|
| 167 |
gr.LoginButton()
|
|
|
|
| 169 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 170 |
|
| 171 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 172 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 173 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 174 |
|
| 175 |
run_button.click(
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
| 181 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 182 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 183 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 184 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 185 |
|
| 186 |
if space_host_startup:
|
| 187 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 188 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 189 |
else:
|
| 190 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 191 |
|
| 192 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 193 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 194 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 195 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 196 |
else:
|
| 197 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 198 |
|
| 199 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 200 |
|
| 201 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 202 |
+
demo.launch(debug=True, share=False)
|