Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,42 +3,35 @@ 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 |
-
class
|
| 13 |
def __init__(self):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
def __call__(self, question: str) -> str:
|
| 21 |
-
print(f"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
},
|
| 30 |
-
{"role": "user", "content": question}
|
| 31 |
-
],
|
| 32 |
-
temperature=0.0
|
| 33 |
-
)
|
| 34 |
-
answer = response.choices[0].message.content.strip()
|
| 35 |
-
print(f"Answer: {answer}")
|
| 36 |
-
return answer
|
| 37 |
-
except Exception as e:
|
| 38 |
-
print(f"β Error in GPT response: {e}")
|
| 39 |
-
return "ERROR: GPT call failed"
|
| 40 |
|
| 41 |
-
# --- Run + Submit Function ---
|
| 42 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 43 |
space_id = os.getenv("SPACE_ID")
|
| 44 |
|
|
@@ -46,99 +39,160 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 46 |
username = f"{profile.username}"
|
| 47 |
print(f"User logged in: {username}")
|
| 48 |
else:
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
api_url = DEFAULT_API_URL
|
| 52 |
questions_url = f"{api_url}/questions"
|
| 53 |
submit_url = f"{api_url}/submit"
|
| 54 |
|
| 55 |
try:
|
| 56 |
-
agent =
|
| 57 |
except Exception as e:
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 61 |
-
print(
|
| 62 |
|
| 63 |
-
|
| 64 |
try:
|
| 65 |
response = requests.get(questions_url, timeout=15)
|
| 66 |
response.raise_for_status()
|
| 67 |
questions_data = response.json()
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
results_log = []
|
| 73 |
answers_payload = []
|
| 74 |
-
|
| 75 |
for item in questions_data:
|
| 76 |
task_id = item.get("task_id")
|
| 77 |
question_text = item.get("question")
|
| 78 |
-
if not task_id or
|
|
|
|
| 79 |
continue
|
| 80 |
try:
|
| 81 |
submitted_answer = agent(question_text)
|
| 82 |
-
answers_payload.append({
|
| 83 |
-
|
| 84 |
-
"submitted_answer": submitted_answer
|
| 85 |
-
})
|
| 86 |
-
results_log.append({
|
| 87 |
-
"Task ID": task_id,
|
| 88 |
-
"Question": question_text,
|
| 89 |
-
"Submitted Answer": submitted_answer
|
| 90 |
-
})
|
| 91 |
except Exception as e:
|
| 92 |
-
print(f"Error on task {task_id}: {e}")
|
| 93 |
-
results_log.append({
|
| 94 |
-
"Task ID": task_id,
|
| 95 |
-
"Question": question_text,
|
| 96 |
-
"Submitted Answer": f"ERROR: {e}"
|
| 97 |
-
})
|
| 98 |
|
| 99 |
if not answers_payload:
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
-
# Submit results
|
| 103 |
submission_data = {
|
| 104 |
"username": username.strip(),
|
| 105 |
"agent_code": agent_code,
|
| 106 |
"answers": answers_payload
|
| 107 |
}
|
|
|
|
|
|
|
| 108 |
|
|
|
|
| 109 |
try:
|
| 110 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 111 |
response.raise_for_status()
|
| 112 |
result_data = response.json()
|
| 113 |
final_status = (
|
| 114 |
-
f"
|
| 115 |
f"User: {result_data.get('username')}\n"
|
| 116 |
-
f"Score: {result_data.get('score', 'N/A')}% "
|
| 117 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 118 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 119 |
)
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
except Exception as e:
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
# --- Gradio Interface ---
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
-
gr.Markdown("#
|
| 127 |
-
gr.Markdown(
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
gr.LoginButton()
|
| 133 |
-
run_button = gr.Button("
|
| 134 |
-
status_output = gr.Textbox(label="
|
| 135 |
-
results_table = gr.DataFrame(label="
|
| 136 |
|
| 137 |
-
run_button.click(
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
-
print("\n
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
|
| 7 |
+
# Try to import Google GenAI SDK
|
| 8 |
+
try:
|
| 9 |
+
from google import genai
|
| 10 |
+
except ImportError:
|
| 11 |
+
raise ImportError("Please install the 'google-genai' package: pip install google-genai")
|
| 12 |
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
+
# --- Gemini Agent Definition ---
|
| 17 |
+
class GeminiAgent:
|
| 18 |
def __init__(self):
|
| 19 |
+
print("GeminiAgent initialized.")
|
| 20 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 21 |
+
if not api_key:
|
| 22 |
+
raise ValueError("GEMINI_API_KEY environment variable not set.")
|
| 23 |
+
self.client = genai.Client(api_key=api_key)
|
| 24 |
|
| 25 |
def __call__(self, question: str) -> str:
|
| 26 |
+
print(f"GeminiAgent received question (first 50 chars): {question[:50]}...")
|
| 27 |
+
prompt = f"Answer concisely without explanation: {question}"
|
| 28 |
+
response = self.client.models.generate_content(
|
| 29 |
+
model="gemini-2.0-flash", contents=prompt
|
| 30 |
+
)
|
| 31 |
+
answer = response.text.strip().rstrip(".!?")
|
| 32 |
+
print(f"GeminiAgent returning answer: {answer}")
|
| 33 |
+
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 36 |
space_id = os.getenv("SPACE_ID")
|
| 37 |
|
|
|
|
| 39 |
username = f"{profile.username}"
|
| 40 |
print(f"User logged in: {username}")
|
| 41 |
else:
|
| 42 |
+
print("User not logged in.")
|
| 43 |
+
return "Please Login to Hugging Face with the button.", None
|
| 44 |
|
| 45 |
api_url = DEFAULT_API_URL
|
| 46 |
questions_url = f"{api_url}/questions"
|
| 47 |
submit_url = f"{api_url}/submit"
|
| 48 |
|
| 49 |
try:
|
| 50 |
+
agent = GeminiAgent()
|
| 51 |
except Exception as e:
|
| 52 |
+
print(f"Error instantiating agent: {e}")
|
| 53 |
+
return f"Error initializing agent: {e}", None
|
| 54 |
|
| 55 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 56 |
+
print(agent_code)
|
| 57 |
|
| 58 |
+
print(f"Fetching questions from: {questions_url}")
|
| 59 |
try:
|
| 60 |
response = requests.get(questions_url, timeout=15)
|
| 61 |
response.raise_for_status()
|
| 62 |
questions_data = response.json()
|
| 63 |
+
if not questions_data:
|
| 64 |
+
print("Fetched questions list is empty.")
|
| 65 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 66 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 67 |
+
except requests.exceptions.RequestException as e:
|
| 68 |
+
print(f"Error fetching questions: {e}")
|
| 69 |
+
return f"Error fetching questions: {e}", None
|
| 70 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 71 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 72 |
+
print(f"Response text: {response.text[:500]}")
|
| 73 |
+
return f"Error decoding server response for questions: {e}", None
|
| 74 |
except Exception as e:
|
| 75 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 76 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
| 77 |
|
| 78 |
results_log = []
|
| 79 |
answers_payload = []
|
| 80 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 81 |
for item in questions_data:
|
| 82 |
task_id = item.get("task_id")
|
| 83 |
question_text = item.get("question")
|
| 84 |
+
if not task_id or question_text is None:
|
| 85 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 86 |
continue
|
| 87 |
try:
|
| 88 |
submitted_answer = agent(question_text)
|
| 89 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 90 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 93 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
if not answers_payload:
|
| 96 |
+
print("Agent did not produce any answers to submit.")
|
| 97 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 98 |
|
|
|
|
| 99 |
submission_data = {
|
| 100 |
"username": username.strip(),
|
| 101 |
"agent_code": agent_code,
|
| 102 |
"answers": answers_payload
|
| 103 |
}
|
| 104 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 105 |
+
print(status_update)
|
| 106 |
|
| 107 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 108 |
try:
|
| 109 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 110 |
response.raise_for_status()
|
| 111 |
result_data = response.json()
|
| 112 |
final_status = (
|
| 113 |
+
f"Submission Successful!\n"
|
| 114 |
f"User: {result_data.get('username')}\n"
|
| 115 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 116 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 117 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 118 |
)
|
| 119 |
+
print("Submission successful.")
|
| 120 |
+
results_df = pd.DataFrame(results_log)
|
| 121 |
+
return final_status, results_df
|
| 122 |
+
except requests.exceptions.HTTPError as e:
|
| 123 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 124 |
+
try:
|
| 125 |
+
error_json = e.response.json()
|
| 126 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 127 |
+
except requests.exceptions.JSONDecodeError:
|
| 128 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 129 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 130 |
+
print(status_message)
|
| 131 |
+
return status_message, pd.DataFrame(results_log)
|
| 132 |
+
except requests.exceptions.Timeout:
|
| 133 |
+
status_message = "Submission Failed: The request timed out."
|
| 134 |
+
print(status_message)
|
| 135 |
+
return status_message, pd.DataFrame(results_log)
|
| 136 |
+
except requests.exceptions.RequestException as e:
|
| 137 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 138 |
+
print(status_message)
|
| 139 |
+
return status_message, pd.DataFrame(results_log)
|
| 140 |
except Exception as e:
|
| 141 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 142 |
+
print(status_message)
|
| 143 |
+
return status_message, pd.DataFrame(results_log)
|
| 144 |
|
| 145 |
+
# --- Build Gradio Interface using Blocks ---
|
| 146 |
with gr.Blocks() as demo:
|
| 147 |
+
gr.Markdown("# Gemini Agent Evaluation Runner")
|
| 148 |
+
gr.Markdown(
|
| 149 |
+
"""
|
| 150 |
+
**Instructions:**
|
| 151 |
+
1. Please clone this space, then modify the code to define your agent's logic,
|
| 152 |
+
the tools, the necessary packages, etc ...
|
| 153 |
+
2. Log in to your Hugging Face account using the button below. This uses your
|
| 154 |
+
HF username for submission.
|
| 155 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your
|
| 156 |
+
agent, submit answers, and see the score.
|
| 157 |
+
|
| 158 |
+
---
|
| 159 |
+
**Disclaimers:**
|
| 160 |
+
Once clicking on the "submit" button, it can take quite some time (this is the
|
| 161 |
+
time for the agent to go through all the questions).
|
| 162 |
+
This space provides a basic setup and is intentionally sub-optimal to
|
| 163 |
+
encourage you to develop your own, more robust solution. For instance, for the
|
| 164 |
+
delay process of the submit button, a solution could be to cache the answers and
|
| 165 |
+
submit in a separate action or even to answer the questions asynchronously.
|
| 166 |
+
"""
|
| 167 |
+
)
|
| 168 |
gr.LoginButton()
|
| 169 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 170 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 171 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 172 |
|
| 173 |
+
run_button.click(
|
| 174 |
+
fn=run_and_submit_all,
|
| 175 |
+
outputs=[status_output, results_table]
|
| 176 |
+
)
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
| 179 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 180 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 181 |
+
space_id_startup = os.getenv("SPACE_ID")
|
| 182 |
+
|
| 183 |
+
if space_host_startup:
|
| 184 |
+
print(f"β
SPACE_HOST found: {space_host_startup}")
|
| 185 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 186 |
+
else:
|
| 187 |
+
print("βΉοΈ SPACE_HOST environment variable not found (running locally?).")
|
| 188 |
+
|
| 189 |
+
if space_id_startup:
|
| 190 |
+
print(f"β
SPACE_ID found: {space_id_startup}")
|
| 191 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 192 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 193 |
+
else:
|
| 194 |
+
print("βΉοΈ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 195 |
+
|
| 196 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 197 |
+
print("Launching Gradio Interface for Gemini Agent Evaluation...")
|
| 198 |
demo.launch(debug=True, share=False)
|