Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,23 +4,18 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
-
# On importe l'agent depuis le bon fichier !
|
| 8 |
from agent import BasicAgent
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
-
# --- Basic Agent Definition ---
|
| 15 |
-
# ON A SUPPRIMÉ L'ANCIENNE CLASSE BasicAgent D'ICI.
|
| 16 |
-
# C'EST MAINTENANT GÉRÉ PAR L'IMPORT CI-DESSUS.
|
| 17 |
|
| 18 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 19 |
"""
|
| 20 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 21 |
and displays the results.
|
| 22 |
"""
|
| 23 |
-
#
|
| 24 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 25 |
|
| 26 |
if profile:
|
|
@@ -34,7 +29,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 34 |
questions_url = f"{api_url}/questions"
|
| 35 |
submit_url = f"{api_url}/submit"
|
| 36 |
|
| 37 |
-
#
|
| 38 |
try:
|
| 39 |
agent = BasicAgent()
|
| 40 |
except Exception as e:
|
|
@@ -44,7 +39,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 44 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 45 |
print(agent_code)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
print(f"Fetching questions from: {questions_url}")
|
| 49 |
try:
|
| 50 |
response = requests.get(questions_url, timeout=15)
|
|
@@ -64,7 +59,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 64 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 65 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 66 |
|
| 67 |
-
#
|
| 68 |
results_log = []
|
| 69 |
answers_payload = []
|
| 70 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
@@ -86,12 +81,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 86 |
print("Agent did not produce any answers to submit.")
|
| 87 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 88 |
|
| 89 |
-
#
|
| 90 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 91 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 92 |
print(status_update)
|
| 93 |
|
| 94 |
-
#
|
| 95 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 96 |
try:
|
| 97 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
@@ -125,7 +120,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 125 |
return status_message, results_df
|
| 126 |
|
| 127 |
|
| 128 |
-
#
|
| 129 |
with gr.Blocks() as demo:
|
| 130 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 131 |
gr.Markdown(
|
|
@@ -146,13 +141,8 @@ with gr.Blocks() as demo:
|
|
| 146 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 147 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 148 |
|
| 149 |
-
# REMOVED the gr.State and the incorrect .login()/.logout() calls
|
| 150 |
-
|
| 151 |
-
# La fonction `run_and_submit_all` est liée au clic du bouton.
|
| 152 |
-
# Gradio passe automatiquement l'objet `profile` depuis `login_button` à la fonction.
|
| 153 |
run_button.click(
|
| 154 |
fn=run_and_submit_all,
|
| 155 |
-
# inputs=[login_button],
|
| 156 |
outputs=[status_output, results_table]
|
| 157 |
)
|
| 158 |
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
| 7 |
from agent import BasicAgent
|
| 8 |
|
| 9 |
+
# Constants
|
|
|
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 14 |
"""
|
| 15 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 16 |
and displays the results.
|
| 17 |
"""
|
| 18 |
+
# Determine HF Space Runtime URL and Repo URL
|
| 19 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 20 |
|
| 21 |
if profile:
|
|
|
|
| 29 |
questions_url = f"{api_url}/questions"
|
| 30 |
submit_url = f"{api_url}/submit"
|
| 31 |
|
| 32 |
+
# Instantiate Agent
|
| 33 |
try:
|
| 34 |
agent = BasicAgent()
|
| 35 |
except Exception as e:
|
|
|
|
| 39 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 40 |
print(agent_code)
|
| 41 |
|
| 42 |
+
# Fetch Questions
|
| 43 |
print(f"Fetching questions from: {questions_url}")
|
| 44 |
try:
|
| 45 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 59 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 60 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 61 |
|
| 62 |
+
# Run your Agent
|
| 63 |
results_log = []
|
| 64 |
answers_payload = []
|
| 65 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 81 |
print("Agent did not produce any answers to submit.")
|
| 82 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 83 |
|
| 84 |
+
# Prepare Submission
|
| 85 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 86 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 87 |
print(status_update)
|
| 88 |
|
| 89 |
+
# Submit
|
| 90 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 91 |
try:
|
| 92 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 120 |
return status_message, results_df
|
| 121 |
|
| 122 |
|
| 123 |
+
# Build Gradio Interface using Blocks
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 126 |
gr.Markdown(
|
|
|
|
| 141 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 142 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
run_button.click(
|
| 145 |
fn=run_and_submit_all,
|
|
|
|
| 146 |
outputs=[status_output, results_table]
|
| 147 |
)
|
| 148 |
|