Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,31 +21,17 @@ load_dotenv()
|
|
21 |
|
22 |
|
23 |
|
24 |
-
|
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 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
except requests.exceptions.RequestException as e:
|
48 |
-
return f"Error fetching Wikipedia data: {str(e)}"
|
49 |
|
50 |
|
51 |
|
|
|
21 |
|
22 |
|
23 |
|
24 |
+
import wikipedia
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
@tool
|
27 |
+
class WikipediaSearchTool(Tool):
|
28 |
+
def use(self, query: str) -> str:
|
29 |
+
try:
|
30 |
+
return wikipedia.summary(query, sentences=2)
|
31 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
32 |
+
return f"Disambiguation Error: {e.options}"
|
33 |
+
except wikipedia.exceptions.PageError:
|
34 |
+
return "No page found."
|
|
|
|
|
|
|
35 |
|
36 |
|
37 |
|