Implement question counter in BasicAgent and modify response logic in app.py. Update main function in main.py to streamline task execution and improve logging. Prepare for potential rate limiting with a placeholder for wait time.
Browse files
app.py
CHANGED
@@ -1,9 +1,15 @@
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
|
|
|
|
|
|
|
|
|
|
|
7 |
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -15,9 +21,29 @@ class BasicAgent:
|
|
15 |
print("BasicAgent initialized.")
|
16 |
def __call__(self, question: str) -> str:
|
17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
23 |
"""
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import inspect
|
6 |
import pandas as pd
|
7 |
|
8 |
+
from main import main
|
9 |
+
|
10 |
+
global question_counter
|
11 |
+
question_counter = 0
|
12 |
+
|
13 |
# (Keep Constants as is)
|
14 |
# --- Constants ---
|
15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
21 |
print("BasicAgent initialized.")
|
22 |
def __call__(self, question: str) -> str:
|
23 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
24 |
+
|
25 |
+
global question_counter
|
26 |
+
question_counter += 1
|
27 |
+
|
28 |
+
if question_counter > 1:
|
29 |
+
return "This is a default answer."
|
30 |
+
|
31 |
+
# fixed_answer = "This is a default answer."
|
32 |
+
# print(f"Agent returning fixed answer: {fixed_answer}")
|
33 |
+
# return fixed_answer
|
34 |
+
|
35 |
+
# TODO: Set a wait time between each question to avoid rate limiting
|
36 |
+
# print("Sleeping for 60 seconds...")
|
37 |
+
# time.sleep(60)
|
38 |
+
|
39 |
+
final_answer = str(main(task=question))
|
40 |
+
|
41 |
+
print("--------------------------------")
|
42 |
+
print(f"Question: {question}")
|
43 |
+
print(f"Final Answer: {final_answer}")
|
44 |
+
print("--------------------------------")
|
45 |
+
|
46 |
+
return final_answer
|
47 |
|
48 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
49 |
"""
|
main.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import logging
|
2 |
import os
|
3 |
import uuid # for generating thread IDs for checkpointer
|
@@ -231,17 +232,11 @@ async def run_with_streaming(task: str, thread_id: str) -> dict:
|
|
231 |
return {"steps": steps, "final_answer": final_answer_text, "error": error_msg}
|
232 |
|
233 |
|
234 |
-
|
235 |
-
import asyncio
|
236 |
-
import uuid
|
237 |
-
|
238 |
-
# Example Usage
|
239 |
-
task_to_run = "What is the capital of France?"
|
240 |
-
thread_id = str(uuid.uuid4()) # Generate a unique thread ID for this run
|
241 |
logger.info(
|
242 |
-
f"Starting agent run from __main__ for task: '{
|
243 |
)
|
244 |
-
result = asyncio.run(run_with_streaming(
|
245 |
logger.info("Agent run finished.")
|
246 |
|
247 |
# Print final results
|
@@ -253,3 +248,14 @@ if __name__ == "__main__":
|
|
253 |
print(f"Final Answer: {result.get('final_answer') or 'Not found'}")
|
254 |
if err := result.get("error"):
|
255 |
print(f"Error: {err}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
import logging
|
3 |
import os
|
4 |
import uuid # for generating thread IDs for checkpointer
|
|
|
232 |
return {"steps": steps, "final_answer": final_answer_text, "error": error_msg}
|
233 |
|
234 |
|
235 |
+
def main(task: str, thread_id: str = str(uuid.uuid4())):
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
logger.info(
|
237 |
+
f"Starting agent run from __main__ for task: '{task}' with thread_id: {thread_id}"
|
238 |
)
|
239 |
+
result = asyncio.run(run_with_streaming(task, thread_id))
|
240 |
logger.info("Agent run finished.")
|
241 |
|
242 |
# Print final results
|
|
|
248 |
print(f"Final Answer: {result.get('final_answer') or 'Not found'}")
|
249 |
if err := result.get("error"):
|
250 |
print(f"Error: {err}")
|
251 |
+
|
252 |
+
return result.get("final_answer")
|
253 |
+
|
254 |
+
|
255 |
+
if __name__ == "__main__":
|
256 |
+
# Example Usage
|
257 |
+
task_to_run = "What is the capital of France?"
|
258 |
+
thread_id = str(uuid.uuid4()) # Generate a unique thread ID for this run
|
259 |
+
|
260 |
+
final_answer = main(task_to_run, thread_id)
|
261 |
+
print(f"Final Answer: {final_answer}")
|