wt002 commited on
Commit
2846e6a
·
verified ·
1 Parent(s): b86709b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
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
- class WikipediaSearchTool(Tool):
27
- name = "wikipedia_search"
28
- description = "Fetches a summary of a Wikipedia page based on a given search query (only one word or group of words)."
29
- inputs = {
30
- "query": {"type": "string", "description": "The search term for the Wikipedia page (only one word or group of words)."}
31
- }
32
- output_type = "string"
 
 
 
 
 
 
 
 
 
33
 
34
- def __init__(self, lang="en"):
35
- super().__init__()
36
- self.wiki = wikipediaapi.Wikipedia(language=lang, user_agent="MinimalAgent/1.0")
 
 
 
 
 
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 = WikipediaSearchTool()
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()