Clean up
Browse files
app.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
-
|
| 8 |
-
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
@@ -19,16 +17,14 @@ class BasicAgent:
|
|
| 19 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
return fixed_answer
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
-
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 28 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 29 |
-
|
| 30 |
if profile:
|
| 31 |
-
username=
|
| 32 |
print(f"User logged in: {username}")
|
| 33 |
else:
|
| 34 |
print("User not logged in.")
|
|
@@ -44,8 +40,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 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
|
| 48 |
-
agent_code = f"https://huggingface.co/spaces/{
|
| 49 |
print(agent_code)
|
| 50 |
|
| 51 |
# 2. Fetch Questions
|
|
@@ -53,7 +49,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 53 |
try:
|
| 54 |
response = requests.get(questions_url, timeout=15)
|
| 55 |
response.raise_for_status()
|
| 56 |
-
questions_data = response.json()
|
| 57 |
if not questions_data:
|
| 58 |
print("Fetched questions list is empty.")
|
| 59 |
return "Fetched questions list is empty or invalid format.", None
|
|
@@ -140,57 +136,38 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 140 |
return status_message, results_df
|
| 141 |
|
| 142 |
|
| 143 |
-
#
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
-
gr.Markdown("#
|
| 146 |
gr.Markdown(
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 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 seperate action or even to answer the questions in async.
|
| 158 |
-
"""
|
| 159 |
-
)
|
| 160 |
|
| 161 |
-
|
| 162 |
|
|
|
|
|
|
|
|
|
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
run_button.click(
|
| 170 |
fn=run_and_submit_all,
|
| 171 |
outputs=[status_output, results_table]
|
| 172 |
)
|
| 173 |
|
| 174 |
-
if __name__ == "__main__":
|
| 175 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 176 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 177 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
| 178 |
-
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 179 |
-
|
| 180 |
-
if space_host_startup:
|
| 181 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 182 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 183 |
-
else:
|
| 184 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 190 |
-
else:
|
| 191 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 192 |
-
|
| 193 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
-
|
| 195 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
|
|
|
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
+
|
|
|
|
| 11 |
class BasicAgent:
|
| 12 |
def __init__(self):
|
| 13 |
print("BasicAgent initialized.")
|
|
|
|
| 17 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 18 |
return fixed_answer
|
| 19 |
|
| 20 |
+
|
| 21 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 22 |
"""
|
| 23 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 24 |
and displays the results.
|
| 25 |
"""
|
|
|
|
|
|
|
|
|
|
| 26 |
if profile:
|
| 27 |
+
username:str = profile.username
|
| 28 |
print(f"User logged in: {username}")
|
| 29 |
else:
|
| 30 |
print("User not logged in.")
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
print(f"Error instantiating agent: {e}")
|
| 42 |
return f"Error initializing agent: {e}", None
|
| 43 |
+
# In the case of an app running as a HF Space, this link points toward your codebase
|
| 44 |
+
agent_code = f"https://huggingface.co/spaces/{os.getenv("SPACE_ID")}/tree/main"
|
| 45 |
print(agent_code)
|
| 46 |
|
| 47 |
# 2. Fetch Questions
|
|
|
|
| 49 |
try:
|
| 50 |
response = requests.get(questions_url, timeout=15)
|
| 51 |
response.raise_for_status()
|
| 52 |
+
questions_data: list[dict] = response.json()
|
| 53 |
if not questions_data:
|
| 54 |
print("Fetched questions list is empty.")
|
| 55 |
return "Fetched questions list is empty or invalid format.", None
|
|
|
|
| 136 |
return status_message, results_df
|
| 137 |
|
| 138 |
|
| 139 |
+
# Demo app
|
| 140 |
with gr.Blocks() as demo:
|
| 141 |
+
gr.Markdown("# Agents Course Final Assignment Evaluation Runner")
|
| 142 |
gr.Markdown(
|
| 143 |
+
"""\
|
| 144 |
+
**Instructions:**
|
| 145 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 146 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 147 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 148 |
+
Note: 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).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
**Implementation:**
|
| 151 |
|
| 152 |
+
"""
|
| 153 |
+
)
|
| 154 |
+
gr.LoginButton()
|
| 155 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 156 |
+
status_output = gr.Textbox(
|
| 157 |
+
label="Run Status / Submission Result",
|
| 158 |
+
lines=5,
|
| 159 |
+
interactive=False
|
| 160 |
+
)
|
| 161 |
+
results_table = gr.DataFrame(
|
| 162 |
+
label="Questions and Agent Answers",
|
| 163 |
+
wrap=True
|
| 164 |
+
)
|
| 165 |
run_button.click(
|
| 166 |
fn=run_and_submit_all,
|
| 167 |
outputs=[status_output, results_table]
|
| 168 |
)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
+
# Run app
|
| 172 |
+
if __name__ == "__main__":
|
| 173 |
+
demo.launch(debug=True, share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|