HJeon commited on
Commit
2e3ac28
·
verified ·
1 Parent(s): 81ca09d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -33,29 +33,33 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
- import wikipedia
37
-
38
  @tool
39
  def get_wikipedia_summary(query: str) -> str:
40
- """
41
- A tool that fetches a Wikipedia summary for a given topic.
42
-
43
  Args:
44
- query: The topic to search for in Wikipedia.
45
-
46
  Returns:
47
- A short summary of the topic from Wikipedia.
48
  """
49
-
50
  try:
51
- summary = wikipedia.summary(query, sentences=2)
52
- return f"Here is a short summary of {query} from Wikipedia: {summary}"
53
- except wikipedia.exceptions.DisambiguationError as e:
54
- return f"The topic '{query}' is ambiguous. Try a more specific term. Suggestions: {e.options[:5]}"
55
- except wikipedia.exceptions.PageError:
56
- return f"Sorry, no page was found for '{query}' on Wikipedia."
 
 
 
 
 
 
 
57
  except Exception as e:
58
- return f"An error occurred: {str(e)}"
 
59
 
60
 
61
  final_answer = FinalAnswerTool()
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
+ import wikipediaapi
37
+
38
  @tool
39
  def get_wikipedia_summary(query: str) -> str:
40
+ """ A tool that fetches a Wikipedia summary for a given topic.
 
 
41
  Args:
42
+ query (str): The topic to search for in Wikipedia.
 
43
  Returns:
44
+ str: A short summary of the topic from Wikipedia or an error message if the page does not exist.
45
  """
 
46
  try:
47
+ # Initialize the Wikipedia API object
48
+ wiki_wiki = wikipediaapi.Wikipedia('en')
49
+
50
+ # Fetch the page for the given query
51
+ page = wiki_wiki.page(query)
52
+
53
+ # Check if the page exists
54
+ if not page.exists():
55
+ return f"Sorry, no page was found for '{query}' on Wikipedia."
56
+
57
+ # Return a truncated summary (up to 500 characters)
58
+ return f"Here is a short summary of '{query}': {page.summary[:500]}..."
59
+
60
  except Exception as e:
61
+ # Catch any unexpected errors and return a user-friendly message
62
+ return f"An error occurred while fetching the Wikipedia summary: {str(e)}"
63
 
64
 
65
  final_answer = FinalAnswerTool()