Spaces:
Sleeping
Sleeping
Commit
·
294ea9c
1
Parent(s):
ae7a494
add tools
Browse files
app.py
CHANGED
@@ -7,31 +7,76 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
"""A tool that does nothing yet
|
15 |
Args:
|
16 |
-
|
17 |
-
arg2: the second argument
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
-
def
|
23 |
-
"""
|
24 |
Args:
|
25 |
-
|
|
|
26 |
"""
|
27 |
try:
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
except Exception as e:
|
34 |
-
return f"Error
|
|
|
|
|
35 |
|
36 |
|
37 |
final_answer = FinalAnswerTool()
|
@@ -55,7 +100,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
|
11 |
@tool
|
12 |
+
def lookup_wikipedia_page(search_query: str) -> str:
|
13 |
+
"""Looks up the exact Wikipedia page title for a given search query
|
|
|
14 |
Args:
|
15 |
+
search_query: The search term to find the Wikipedia page for
|
|
|
16 |
"""
|
17 |
+
try:
|
18 |
+
# Use Wikipedia's API to search for pages
|
19 |
+
url = "https://en.wikipedia.org/w/api.php"
|
20 |
+
params = {
|
21 |
+
"action": "query",
|
22 |
+
"format": "json",
|
23 |
+
"list": "search",
|
24 |
+
"srsearch": search_query,
|
25 |
+
"srlimit": 1
|
26 |
+
}
|
27 |
+
|
28 |
+
response = requests.get(url, params=params)
|
29 |
+
|
30 |
+
if response.status_code == 200:
|
31 |
+
data = response.json()
|
32 |
+
if data["query"]["search"]:
|
33 |
+
# Return the exact page title
|
34 |
+
return data["query"]["search"][0]["title"]
|
35 |
+
else:
|
36 |
+
return f"No Wikipedia pages found for '{search_query}'"
|
37 |
+
else:
|
38 |
+
return f"Error searching Wikipedia: {response.status_code}"
|
39 |
+
|
40 |
+
except Exception as e:
|
41 |
+
return f"Error processing request: {str(e)}"
|
42 |
+
|
43 |
|
44 |
@tool
|
45 |
+
def get_wikipedia_views(article_title: str, window_size_in_days: int) -> str:
|
46 |
+
"""Fetches view statistics for a Wikipedia article
|
47 |
Args:
|
48 |
+
article_title: The title of the Wikipedia article to get views for
|
49 |
+
window_size_in_days: The number of days to get views for
|
50 |
"""
|
51 |
try:
|
52 |
+
# Format the dates (get views for last 7 days)
|
53 |
+
end_date = datetime.datetime.now()
|
54 |
+
start_date = end_date - datetime.timedelta(days=window_size_in_days)
|
55 |
+
|
56 |
+
# Format dates as YYYYMMDD for the API
|
57 |
+
start_str = start_date.strftime('%Y%m%d')
|
58 |
+
end_str = end_date.strftime('%Y%m%d')
|
59 |
+
|
60 |
+
# Clean article title for URL
|
61 |
+
article = article_title.replace(' ', '_')
|
62 |
+
|
63 |
+
# Construct API URL
|
64 |
+
url = f"https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/all-agents/{article}/daily/{start_str}/{end_str}"
|
65 |
+
|
66 |
+
# Make request
|
67 |
+
response = requests.get(url)
|
68 |
+
|
69 |
+
if response.status_code == 200:
|
70 |
+
data = response.json()
|
71 |
+
total_views = sum(item['views'] for item in data['items'])
|
72 |
+
return f"The article '{article_title}' had {total_views} views in the last 7 days"
|
73 |
+
else:
|
74 |
+
return f"Error fetching data: {response.status_code}"
|
75 |
+
|
76 |
except Exception as e:
|
77 |
+
return f"Error processing request: {str(e)}"
|
78 |
+
|
79 |
+
|
80 |
|
81 |
|
82 |
final_answer = FinalAnswerTool()
|
|
|
100 |
|
101 |
agent = CodeAgent(
|
102 |
model=model,
|
103 |
+
tools=[final_answer, lookup_wikipedia_page, get_wikipedia_views], ## add your tools here (don't remove final answer)
|
104 |
max_steps=6,
|
105 |
verbosity_level=1,
|
106 |
grammar=None,
|