Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ import inspect
|
|
5 |
import pandas as pd
|
6 |
import smolagents
|
7 |
from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
|
|
|
|
|
|
|
8 |
|
9 |
search_tool = DuckDuckGoSearchTool()
|
10 |
visit_webpage = VisitWebpageTool()
|
@@ -13,6 +16,11 @@ visit_webpage = VisitWebpageTool()
|
|
13 |
# --- Constants ---
|
14 |
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 |
class BasicAgent:
|
@@ -29,8 +37,8 @@ class BasicAgent:
|
|
29 |
print(f"Agent returning answer: {final_answer[:50]}...")
|
30 |
return final_answer
|
31 |
|
32 |
-
def
|
33 |
-
search_results =
|
34 |
return f"Based on my analysis of '{question}', I found: {search_results[:500]}..."
|
35 |
|
36 |
|
@@ -98,6 +106,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
98 |
submitted_answer = agent(question_text)
|
99 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
100 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
101 |
except Exception as e:
|
102 |
print(f"Error running agent on task {task_id}: {e}")
|
103 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
5 |
import pandas as pd
|
6 |
import smolagents
|
7 |
from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
|
8 |
+
import time # Added: For sleep functionality
|
9 |
+
from functools import lru_cache # Added: For caching search results
|
10 |
+
|
11 |
|
12 |
search_tool = DuckDuckGoSearchTool()
|
13 |
visit_webpage = VisitWebpageTool()
|
|
|
16 |
# --- Constants ---
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
18 |
|
19 |
+
# Cache Wrapper
|
20 |
+
@lru_cache(maxsize=100)
|
21 |
+
def cached_search(query):
|
22 |
+
return search_tool(query)
|
23 |
+
|
24 |
# --- Basic Agent Definition ---
|
25 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
26 |
class BasicAgent:
|
|
|
37 |
print(f"Agent returning answer: {final_answer[:50]}...")
|
38 |
return final_answer
|
39 |
|
40 |
+
def process_question (self, question:str) -> str:
|
41 |
+
search_results = cached_search(question) if self.tools and search_tool in self.tools else "No search results available."
|
42 |
return f"Based on my analysis of '{question}', I found: {search_results[:500]}..."
|
43 |
|
44 |
|
|
|
106 |
submitted_answer = agent(question_text)
|
107 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
108 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
109 |
+
if idx % 5 == 0 and idx > 0: # Added: Add delay every 5 questions
|
110 |
+
time.sleep(1) # Wait 1 second between batches to avoid rate limiting
|
111 |
except Exception as e:
|
112 |
print(f"Error running agent on task {task_id}: {e}")
|
113 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|