Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,26 +20,33 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
20 |
load_dotenv()
|
21 |
|
22 |
|
23 |
-
from smolagents import Tool
|
24 |
-
import wikipediaapi
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
def forward(self, query: str):
|
39 |
-
page = self.wiki.page(query)
|
40 |
-
if not page.exists():
|
41 |
-
return "No Wikipedia page found."
|
42 |
-
return page.summary[:1000]
|
43 |
|
44 |
|
45 |
class StringReverseTool(Tool):
|
@@ -180,7 +187,7 @@ class BasicAgent:
|
|
180 |
)
|
181 |
|
182 |
search_tool = DuckDuckGoSearchTool()
|
183 |
-
wiki_search_tool =
|
184 |
str_reverse_tool = StringReverseTool()
|
185 |
keywords_extract_tool = KeywordsExtractorTool()
|
186 |
speech_to_text_tool = SpeechToTextTool()
|
|
|
20 |
load_dotenv()
|
21 |
|
22 |
|
|
|
|
|
23 |
|
24 |
+
@tool
|
25 |
+
def search_wikipedia(query: str) -> str:
|
26 |
+
"""
|
27 |
+
Fetches a summary of a Wikipedia page for a given query.
|
28 |
+
Args:
|
29 |
+
query: The search term to look up on Wikipedia.
|
30 |
+
Returns:
|
31 |
+
str: A summary of the Wikipedia page if successful, or an error message if the request fails.
|
32 |
+
Raises:
|
33 |
+
requests.exceptions.RequestException: If there is an issue with the HTTP request.
|
34 |
+
"""
|
35 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}"
|
36 |
+
|
37 |
+
try:
|
38 |
+
response = requests.get(url)
|
39 |
+
response.raise_for_status()
|
40 |
|
41 |
+
data = response.json()
|
42 |
+
title = data["title"]
|
43 |
+
extract = data["extract"]
|
44 |
+
|
45 |
+
return f"Summary for {title}: {extract}"
|
46 |
+
|
47 |
+
except requests.exceptions.RequestException as e:
|
48 |
+
return f"Error fetching Wikipedia data: {str(e)}"
|
49 |
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
|
52 |
class StringReverseTool(Tool):
|
|
|
187 |
)
|
188 |
|
189 |
search_tool = DuckDuckGoSearchTool()
|
190 |
+
wiki_search_tool = search_wikipedia()
|
191 |
str_reverse_tool = StringReverseTool()
|
192 |
keywords_extract_tool = KeywordsExtractorTool()
|
193 |
speech_to_text_tool = SpeechToTextTool()
|