Update app.py
Browse files
app.py
CHANGED
@@ -15,59 +15,11 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
15 |
|
16 |
# --- Basic Agent Definition ---
|
17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
18 |
-
|
19 |
-
class RateLimitException(Exception):
|
20 |
-
pass
|
21 |
-
|
22 |
-
@retry(
|
23 |
-
reraise=True,
|
24 |
-
stop=stop_after_attempt(10),
|
25 |
-
wait=wait_exponential(multiplier=20, min=20, max=120),
|
26 |
-
retry=retry_if_exception_type(RateLimitException),
|
27 |
-
)
|
28 |
-
def call_openai_with_retry(**kwargs):
|
29 |
-
try:
|
30 |
-
return openai.ChatCompletion.create(**kwargs)
|
31 |
-
except openai.error.RateLimitError as e:
|
32 |
-
print("Rate limit error detected. Will retry...")
|
33 |
-
raise RateLimitException from e
|
34 |
-
except Exception as e:
|
35 |
-
print(f"Non-rate-limit error occurred: {e}")
|
36 |
-
raise
|
37 |
-
|
38 |
-
class BasicAgent:
|
39 |
-
def __init__(self):
|
40 |
-
self.agent = CodeAgent(
|
41 |
-
model=OpenAIServerModel(model_id="gpt-4o"),
|
42 |
-
tools=[
|
43 |
-
DuckDuckGoSearchTool(),
|
44 |
-
WikipediaSearchTool()
|
45 |
-
],
|
46 |
-
add_base_tools=True,
|
47 |
-
)
|
48 |
-
print("BasicAgent initialized.")
|
49 |
-
|
50 |
-
def __call__(self, question: str) -> str:
|
51 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
52 |
-
try:
|
53 |
-
fixed_answer = call_openai_with_retry(
|
54 |
-
model="gpt-4o",
|
55 |
-
messages=[
|
56 |
-
{"role": "user", "content": question}
|
57 |
-
],
|
58 |
-
temperature=0.2,
|
59 |
-
)['choices'][0]['message']['content']
|
60 |
-
except Exception as e:
|
61 |
-
print(f"Agent error: {e}")
|
62 |
-
raise
|
63 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
64 |
-
return fixed_answer
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
def safe_agent_call(agent, question, retries=5, wait_time=20):
|
69 |
"""
|
70 |
-
|
|
|
71 |
"""
|
72 |
for attempt in range(retries):
|
73 |
try:
|
@@ -82,15 +34,37 @@ def safe_agent_call(agent, question, retries=5, wait_time=20):
|
|
82 |
raise e
|
83 |
raise Exception(f"Failed after {retries} retries due to repeated rate limit errors.")
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
86 |
"""
|
87 |
-
Fetches all questions, runs
|
88 |
submits all answers, and displays the results.
|
89 |
"""
|
90 |
|
91 |
-
#
|
92 |
space_id = os.getenv("SPACE_ID")
|
93 |
-
|
94 |
if profile:
|
95 |
username = f"{profile.username}"
|
96 |
print(f"User logged in: {username}")
|
@@ -98,23 +72,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
98 |
print("User not logged in.")
|
99 |
return "Please Login to Hugging Face with the button.", None
|
100 |
|
101 |
-
# --- Constants ---
|
102 |
api_url = DEFAULT_API_URL
|
103 |
questions_url = f"{api_url}/questions"
|
104 |
submit_url = f"{api_url}/submit"
|
105 |
|
106 |
-
#
|
107 |
try:
|
108 |
agent = BasicAgent()
|
109 |
except Exception as e:
|
110 |
print(f"Error instantiating agent: {e}")
|
111 |
return f"Error initializing agent: {e}", None
|
112 |
|
113 |
-
# Build agent code link
|
114 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
115 |
-
print(agent_code)
|
116 |
|
117 |
-
#
|
118 |
print(f"Fetching questions from: {questions_url}")
|
119 |
try:
|
120 |
response = requests.get(questions_url, timeout=15)
|
@@ -124,22 +96,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
124 |
print("Fetched questions list is empty.")
|
125 |
return "Fetched questions list is empty or invalid format.", None
|
126 |
print(f"Fetched {len(questions_data)} questions.")
|
127 |
-
except
|
128 |
print(f"Error fetching questions: {e}")
|
129 |
return f"Error fetching questions: {e}", None
|
130 |
-
except requests.exceptions.JSONDecodeError as e:
|
131 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
132 |
-
print(f"Response text: {response.text[:500]}")
|
133 |
-
return f"Error decoding server response for questions: {e}", None
|
134 |
-
except Exception as e:
|
135 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
136 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
137 |
|
138 |
-
#
|
139 |
results_log = []
|
140 |
answers_payload = []
|
141 |
print(f"Running agent on {len(questions_data)} questions...")
|
142 |
-
|
143 |
for item in questions_data:
|
144 |
task_id = item.get("task_id")
|
145 |
question_text = item.get("question")
|
@@ -147,70 +111,54 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
147 |
print(f"Skipping item with missing task_id or question: {item}")
|
148 |
continue
|
149 |
try:
|
150 |
-
# Using safe_agent_call to handle rate limit retries
|
151 |
submitted_answer = safe_agent_call(agent, question_text)
|
152 |
-
answers_payload.append({
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
except Exception as e:
|
155 |
print(f"Error running agent on task {task_id}: {e}")
|
156 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
157 |
|
158 |
if not answers_payload:
|
159 |
print("Agent did not produce any answers to submit.")
|
160 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
161 |
|
162 |
-
#
|
163 |
submission_data = {
|
164 |
"username": username.strip(),
|
165 |
"agent_code": agent_code,
|
166 |
-
"answers": answers_payload
|
167 |
}
|
168 |
-
|
169 |
-
print(status_update)
|
170 |
|
171 |
-
#
|
172 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
173 |
try:
|
174 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
175 |
response.raise_for_status()
|
176 |
result_data = response.json()
|
177 |
final_status = (
|
178 |
-
f"Submission Successful!\n"
|
179 |
f"User: {result_data.get('username')}\n"
|
180 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
181 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
182 |
f"Message: {result_data.get('message', 'No message received.')}"
|
183 |
)
|
184 |
-
print("Submission successful.")
|
185 |
results_df = pd.DataFrame(results_log)
|
186 |
return final_status, results_df
|
187 |
-
except requests.exceptions.HTTPError as e:
|
188 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
189 |
-
try:
|
190 |
-
error_json = e.response.json()
|
191 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
192 |
-
except requests.exceptions.JSONDecodeError:
|
193 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
194 |
-
status_message = f"Submission Failed: {error_detail}"
|
195 |
-
print(status_message)
|
196 |
-
results_df = pd.DataFrame(results_log)
|
197 |
-
return status_message, results_df
|
198 |
-
except requests.exceptions.Timeout:
|
199 |
-
status_message = "Submission Failed: The request timed out."
|
200 |
-
print(status_message)
|
201 |
-
results_df = pd.DataFrame(results_log)
|
202 |
-
return status_message, results_df
|
203 |
-
except requests.exceptions.RequestException as e:
|
204 |
-
status_message = f"Submission Failed: Network error - {e}"
|
205 |
-
print(status_message)
|
206 |
-
results_df = pd.DataFrame(results_log)
|
207 |
-
return status_message, results_df
|
208 |
except Exception as e:
|
209 |
-
|
210 |
-
print(status_message)
|
211 |
results_df = pd.DataFrame(results_log)
|
212 |
-
return
|
213 |
-
|
214 |
|
215 |
# --- Build Gradio Interface using Blocks ---
|
216 |
with gr.Blocks() as demo:
|
|
|
15 |
|
16 |
# --- Basic Agent Definition ---
|
17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
18 |
+
# --- Retry Helper for Agent Call ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def safe_agent_call(agent, question, retries=5, wait_time=20):
|
20 |
"""
|
21 |
+
Safely call the agent with retry on rate limit errors (HTTP 429).
|
22 |
+
Retries up to `retries` times, waits `wait_time` seconds between attempts.
|
23 |
"""
|
24 |
for attempt in range(retries):
|
25 |
try:
|
|
|
34 |
raise e
|
35 |
raise Exception(f"Failed after {retries} retries due to repeated rate limit errors.")
|
36 |
|
37 |
+
# --- Basic Agent Definition ---
|
38 |
+
class BasicAgent:
|
39 |
+
def __init__(self):
|
40 |
+
self.agent = CodeAgent(
|
41 |
+
model=OpenAIServerModel(model_id="gpt-4o"),
|
42 |
+
tools=[
|
43 |
+
DuckDuckGoSearchTool(),
|
44 |
+
WikipediaSearchTool(),
|
45 |
+
],
|
46 |
+
add_base_tools=True,
|
47 |
+
)
|
48 |
+
print("✅ BasicAgent initialized.")
|
49 |
+
|
50 |
+
def __call__(self, question: str) -> str:
|
51 |
+
"""
|
52 |
+
Calls the agent's run method to generate a response to the question.
|
53 |
+
"""
|
54 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
55 |
+
fixed_answer = self.agent.run(question)
|
56 |
+
print(f"Agent returning answer: {fixed_answer}")
|
57 |
+
return fixed_answer
|
58 |
+
|
59 |
+
# --- Main Logic for Fetching Questions, Running Agent, Submitting Answers ---
|
60 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
61 |
"""
|
62 |
+
Fetches all questions, runs BasicAgent on them with retry logic on rate limit,
|
63 |
submits all answers, and displays the results.
|
64 |
"""
|
65 |
|
66 |
+
# Determine HF Space runtime info
|
67 |
space_id = os.getenv("SPACE_ID")
|
|
|
68 |
if profile:
|
69 |
username = f"{profile.username}"
|
70 |
print(f"User logged in: {username}")
|
|
|
72 |
print("User not logged in.")
|
73 |
return "Please Login to Hugging Face with the button.", None
|
74 |
|
|
|
75 |
api_url = DEFAULT_API_URL
|
76 |
questions_url = f"{api_url}/questions"
|
77 |
submit_url = f"{api_url}/submit"
|
78 |
|
79 |
+
# Instantiate Agent
|
80 |
try:
|
81 |
agent = BasicAgent()
|
82 |
except Exception as e:
|
83 |
print(f"Error instantiating agent: {e}")
|
84 |
return f"Error initializing agent: {e}", None
|
85 |
|
|
|
86 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
87 |
+
print(f"Agent Code Repository: {agent_code}")
|
88 |
|
89 |
+
# Fetch Questions
|
90 |
print(f"Fetching questions from: {questions_url}")
|
91 |
try:
|
92 |
response = requests.get(questions_url, timeout=15)
|
|
|
96 |
print("Fetched questions list is empty.")
|
97 |
return "Fetched questions list is empty or invalid format.", None
|
98 |
print(f"Fetched {len(questions_data)} questions.")
|
99 |
+
except Exception as e:
|
100 |
print(f"Error fetching questions: {e}")
|
101 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
# Run Agent on Questions
|
104 |
results_log = []
|
105 |
answers_payload = []
|
106 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
107 |
for item in questions_data:
|
108 |
task_id = item.get("task_id")
|
109 |
question_text = item.get("question")
|
|
|
111 |
print(f"Skipping item with missing task_id or question: {item}")
|
112 |
continue
|
113 |
try:
|
|
|
114 |
submitted_answer = safe_agent_call(agent, question_text)
|
115 |
+
answers_payload.append({
|
116 |
+
"task_id": task_id,
|
117 |
+
"submitted_answer": submitted_answer,
|
118 |
+
})
|
119 |
+
results_log.append({
|
120 |
+
"Task ID": task_id,
|
121 |
+
"Question": question_text,
|
122 |
+
"Submitted Answer": submitted_answer,
|
123 |
+
})
|
124 |
except Exception as e:
|
125 |
print(f"Error running agent on task {task_id}: {e}")
|
126 |
+
results_log.append({
|
127 |
+
"Task ID": task_id,
|
128 |
+
"Question": question_text,
|
129 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
130 |
+
})
|
131 |
|
132 |
if not answers_payload:
|
133 |
print("Agent did not produce any answers to submit.")
|
134 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
135 |
|
136 |
+
# Prepare Submission
|
137 |
submission_data = {
|
138 |
"username": username.strip(),
|
139 |
"agent_code": agent_code,
|
140 |
+
"answers": answers_payload,
|
141 |
}
|
142 |
+
print(f"Submitting {len(answers_payload)} answers...")
|
|
|
143 |
|
144 |
+
# Submit Answers
|
|
|
145 |
try:
|
146 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
147 |
response.raise_for_status()
|
148 |
result_data = response.json()
|
149 |
final_status = (
|
150 |
+
f"✅ Submission Successful!\n"
|
151 |
f"User: {result_data.get('username')}\n"
|
152 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
153 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
154 |
f"Message: {result_data.get('message', 'No message received.')}"
|
155 |
)
|
|
|
156 |
results_df = pd.DataFrame(results_log)
|
157 |
return final_status, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
except Exception as e:
|
159 |
+
print(f"Submission error: {e}")
|
|
|
160 |
results_df = pd.DataFrame(results_log)
|
161 |
+
return f"Submission Failed: {e}", results_df
|
|
|
162 |
|
163 |
# --- Build Gradio Interface using Blocks ---
|
164 |
with gr.Blocks() as demo:
|