Spaces:
Sleeping
Sleeping
Updated the code to use less time to run
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
|
@@ -12,6 +13,20 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
|
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# =========================
|
| 16 |
# Basic Agent Definition
|
| 17 |
# =========================
|
|
@@ -19,31 +34,30 @@ class BasicAgent:
|
|
| 19 |
def __init__(self):
|
| 20 |
print("BasicAgent initialized.")
|
| 21 |
|
| 22 |
-
# Get OpenAI API key from environment (HF Secrets)
|
| 23 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 24 |
if not OPENAI_API_KEY:
|
| 25 |
raise ValueError(
|
| 26 |
-
"OPENAI_API_KEY
|
| 27 |
"Add it in Hugging Face → Settings → Secrets."
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# Initialize OpenAI model
|
| 31 |
model = OpenAIServerModel(
|
| 32 |
-
model_id="gpt-4o-mini", #
|
| 33 |
api_key=OPENAI_API_KEY
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Initialize tools
|
| 37 |
search_tool = DuckDuckGoSearchTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
self.agent = CodeAgent(
|
| 41 |
model=model,
|
| 42 |
-
tools=[search_tool]
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
def __call__(self, question: str) -> str:
|
| 46 |
-
print(f"Agent received question: {question[:
|
| 47 |
return self.agent.run(question)
|
| 48 |
|
| 49 |
|
|
@@ -53,12 +67,12 @@ class BasicAgent:
|
|
| 53 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 54 |
space_id = os.getenv("SPACE_ID")
|
| 55 |
|
| 56 |
-
if profile:
|
| 57 |
-
username = profile.username
|
| 58 |
-
print(f"User logged in: {username}")
|
| 59 |
-
else:
|
| 60 |
return "Please login to Hugging Face first.", None
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 63 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 64 |
|
|
@@ -81,18 +95,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 81 |
results_log = []
|
| 82 |
answers_payload = []
|
| 83 |
|
| 84 |
-
# Run agent
|
| 85 |
for item in questions_data:
|
| 86 |
task_id = item.get("task_id")
|
| 87 |
question_text = item.get("question")
|
| 88 |
|
| 89 |
-
if not task_id or
|
| 90 |
continue
|
| 91 |
|
| 92 |
try:
|
|
|
|
| 93 |
answer = agent(question_text)
|
| 94 |
except Exception as e:
|
| 95 |
answer = f"AGENT ERROR: {e}"
|
|
|
|
|
|
|
| 96 |
|
| 97 |
answers_payload.append(
|
| 98 |
{"task_id": task_id, "submitted_answer": answer}
|
|
@@ -147,16 +164,18 @@ with gr.Blocks() as demo:
|
|
| 147 |
**Instructions**
|
| 148 |
1. Login with your Hugging Face account.
|
| 149 |
2. Click **Run Evaluation & Submit All Answers**.
|
| 150 |
-
3. Wait
|
| 151 |
|
| 152 |
---
|
| 153 |
-
**
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 156 |
"""
|
| 157 |
)
|
| 158 |
|
| 159 |
gr.LoginButton()
|
|
|
|
| 160 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 161 |
|
| 162 |
status_output = gr.Textbox(
|
|
@@ -177,7 +196,7 @@ with gr.Blocks() as demo:
|
|
| 177 |
|
| 178 |
|
| 179 |
# =========================
|
| 180 |
-
#
|
| 181 |
# =========================
|
| 182 |
if __name__ == "__main__":
|
| 183 |
print("Launching Gradio app...")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import signal
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
|
|
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
|
| 16 |
+
# =========================
|
| 17 |
+
# Timeout Utilities
|
| 18 |
+
# =========================
|
| 19 |
+
class TimeoutException(Exception):
|
| 20 |
+
pass
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def timeout_handler(signum, frame):
|
| 24 |
+
raise TimeoutException("Agent timed out")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
signal.signal(signal.SIGALRM, timeout_handler)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
# =========================
|
| 31 |
# Basic Agent Definition
|
| 32 |
# =========================
|
|
|
|
| 34 |
def __init__(self):
|
| 35 |
print("BasicAgent initialized.")
|
| 36 |
|
|
|
|
| 37 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 38 |
if not OPENAI_API_KEY:
|
| 39 |
raise ValueError(
|
| 40 |
+
"OPENAI_API_KEY not set. "
|
| 41 |
"Add it in Hugging Face → Settings → Secrets."
|
| 42 |
)
|
| 43 |
|
|
|
|
| 44 |
model = OpenAIServerModel(
|
| 45 |
+
model_id="gpt-4o-mini", # fast + cheap + sufficient
|
| 46 |
api_key=OPENAI_API_KEY
|
| 47 |
)
|
| 48 |
|
|
|
|
| 49 |
search_tool = DuckDuckGoSearchTool()
|
| 50 |
|
| 51 |
+
# IMPORTANT: step limit prevents infinite loops
|
| 52 |
self.agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
+
tools=[search_tool],
|
| 55 |
+
max_steps=3,
|
| 56 |
+
verbosity=0
|
| 57 |
)
|
| 58 |
|
| 59 |
def __call__(self, question: str) -> str:
|
| 60 |
+
print(f"Agent received question: {question[:60]}...")
|
| 61 |
return self.agent.run(question)
|
| 62 |
|
| 63 |
|
|
|
|
| 67 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 68 |
space_id = os.getenv("SPACE_ID")
|
| 69 |
|
| 70 |
+
if not profile:
|
|
|
|
|
|
|
|
|
|
| 71 |
return "Please login to Hugging Face first.", None
|
| 72 |
|
| 73 |
+
username = profile.username
|
| 74 |
+
print(f"User logged in: {username}")
|
| 75 |
+
|
| 76 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 77 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 78 |
|
|
|
|
| 95 |
results_log = []
|
| 96 |
answers_payload = []
|
| 97 |
|
| 98 |
+
# Run agent on each question
|
| 99 |
for item in questions_data:
|
| 100 |
task_id = item.get("task_id")
|
| 101 |
question_text = item.get("question")
|
| 102 |
|
| 103 |
+
if not task_id or not question_text:
|
| 104 |
continue
|
| 105 |
|
| 106 |
try:
|
| 107 |
+
signal.alarm(40) # ⏱ max 40 seconds per question
|
| 108 |
answer = agent(question_text)
|
| 109 |
except Exception as e:
|
| 110 |
answer = f"AGENT ERROR: {e}"
|
| 111 |
+
finally:
|
| 112 |
+
signal.alarm(0)
|
| 113 |
|
| 114 |
answers_payload.append(
|
| 115 |
{"task_id": task_id, "submitted_answer": answer}
|
|
|
|
| 164 |
**Instructions**
|
| 165 |
1. Login with your Hugging Face account.
|
| 166 |
2. Click **Run Evaluation & Submit All Answers**.
|
| 167 |
+
3. Wait for the agent to finish (this can take a few minutes).
|
| 168 |
|
| 169 |
---
|
| 170 |
+
**Requirements**
|
| 171 |
+
- Uses **OpenAI (gpt-4o-mini)**
|
| 172 |
+
- Requires `OPENAI_API_KEY` in HF Space Secrets
|
| 173 |
+
- Agent is step-limited and timeout-safe
|
| 174 |
"""
|
| 175 |
)
|
| 176 |
|
| 177 |
gr.LoginButton()
|
| 178 |
+
|
| 179 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 180 |
|
| 181 |
status_output = gr.Textbox(
|
|
|
|
| 196 |
|
| 197 |
|
| 198 |
# =========================
|
| 199 |
+
# Entry Point
|
| 200 |
# =========================
|
| 201 |
if __name__ == "__main__":
|
| 202 |
print("Launching Gradio app...")
|