Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
import
|
| 7 |
-
from agent import ClaudeAgent
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
-
import random
|
| 10 |
|
| 11 |
# Load environment variables
|
| 12 |
load_dotenv()
|
|
@@ -21,6 +19,17 @@ else:
|
|
| 21 |
# --- Constants ---
|
| 22 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 25 |
"""
|
| 26 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -43,7 +52,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 43 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 44 |
try:
|
| 45 |
# agent = BasicAgent()
|
| 46 |
-
agent =
|
| 47 |
except Exception as e:
|
| 48 |
print(f"Error instantiating agent: {e}")
|
| 49 |
return f"Error initializing agent: {e}", None
|
|
@@ -76,42 +85,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 76 |
results_log = []
|
| 77 |
answers_payload = []
|
| 78 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 79 |
-
|
| 80 |
-
# Process with rate limiting to avoid Anthropic API limits
|
| 81 |
-
for i, item in enumerate(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 |
-
|
| 88 |
-
# Add a progress indicator
|
| 89 |
-
print(f"Processing question {i+1}/{len(questions_data)}: Task ID {task_id}")
|
| 90 |
-
|
| 91 |
try:
|
| 92 |
-
# Add a delay between requests to respect rate limits
|
| 93 |
-
if i > 0:
|
| 94 |
-
# Random delay between 2-5 seconds
|
| 95 |
-
sleep_time = random.uniform(2, 5)
|
| 96 |
-
print(f"Waiting {sleep_time:.2f} seconds before next question...")
|
| 97 |
-
time.sleep(sleep_time)
|
| 98 |
-
|
| 99 |
-
# Process the question
|
| 100 |
submitted_answer = agent(question_text)
|
| 101 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 102 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 103 |
-
|
| 104 |
-
# Print answer for monitoring
|
| 105 |
-
print(f"Answer for task {task_id}: {submitted_answer[:100]}..." if len(submitted_answer) > 100 else f"Answer: {submitted_answer}")
|
| 106 |
except Exception as e:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# If this was a rate limit error, add a longer cooldown period
|
| 111 |
-
if "rate_limit" in str(e).lower():
|
| 112 |
-
cool_down = 60 # 1 minute cooldown
|
| 113 |
-
print(f"Rate limit detected. Cooling down for {cool_down} seconds...")
|
| 114 |
-
time.sleep(cool_down)
|
| 115 |
|
| 116 |
if not answers_payload:
|
| 117 |
print("Agent did not produce any answers to submit.")
|
|
@@ -168,18 +154,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 168 |
|
| 169 |
# --- Build Gradio Interface using Blocks ---
|
| 170 |
with gr.Blocks() as demo:
|
| 171 |
-
gr.Markdown("#
|
| 172 |
gr.Markdown(
|
| 173 |
"""
|
| 174 |
**Instructions:**
|
| 175 |
-
1.
|
| 176 |
-
2.
|
| 177 |
-
3.
|
| 178 |
-
|
| 179 |
-
**
|
| 180 |
-
|
| 181 |
-
-
|
| 182 |
-
- The entire process may take several minutes to complete
|
| 183 |
"""
|
| 184 |
)
|
| 185 |
|
|
@@ -188,6 +173,7 @@ with gr.Blocks() as demo:
|
|
| 188 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 189 |
|
| 190 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 191 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 192 |
|
| 193 |
run_button.click(
|
|
@@ -216,5 +202,5 @@ if __name__ == "__main__":
|
|
| 216 |
|
| 217 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 218 |
|
| 219 |
-
print("Launching Gradio Interface for
|
| 220 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from agent import GeminiAgent
|
|
|
|
| 7 |
from dotenv import load_dotenv
|
|
|
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
|
|
|
| 19 |
# --- Constants ---
|
| 20 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 21 |
|
| 22 |
+
# --- Basic Agent Definition ---
|
| 23 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 24 |
+
class BasicAgent:
|
| 25 |
+
def __init__(self):
|
| 26 |
+
print("BasicAgent initialized.")
|
| 27 |
+
def __call__(self, question: str) -> str:
|
| 28 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 29 |
+
fixed_answer = "This is a default answer."
|
| 30 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 31 |
+
return fixed_answer
|
| 32 |
+
|
| 33 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 34 |
"""
|
| 35 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 52 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 53 |
try:
|
| 54 |
# agent = BasicAgent()
|
| 55 |
+
agent = GeminiAgent()
|
| 56 |
except Exception as e:
|
| 57 |
print(f"Error instantiating agent: {e}")
|
| 58 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 85 |
results_log = []
|
| 86 |
answers_payload = []
|
| 87 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 88 |
+
for item in questions_data:
|
|
|
|
|
|
|
| 89 |
task_id = item.get("task_id")
|
| 90 |
question_text = item.get("question")
|
| 91 |
if not task_id or question_text is None:
|
| 92 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 93 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
submitted_answer = agent(question_text)
|
| 96 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 97 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 100 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
if not answers_payload:
|
| 103 |
print("Agent did not produce any answers to submit.")
|
|
|
|
| 154 |
|
| 155 |
# --- Build Gradio Interface using Blocks ---
|
| 156 |
with gr.Blocks() as demo:
|
| 157 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 158 |
gr.Markdown(
|
| 159 |
"""
|
| 160 |
**Instructions:**
|
| 161 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 162 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 163 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 164 |
+
---
|
| 165 |
+
**Disclaimers:**
|
| 166 |
+
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).
|
| 167 |
+
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.
|
|
|
|
| 168 |
"""
|
| 169 |
)
|
| 170 |
|
|
|
|
| 173 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 174 |
|
| 175 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 176 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 177 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 178 |
|
| 179 |
run_button.click(
|
|
|
|
| 202 |
|
| 203 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 204 |
|
| 205 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 206 |
demo.launch(debug=True, share=False)
|