Duygu Jones commited on
Commit
1dffacd
·
verified ·
1 Parent(s): d43153e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -18
app.py CHANGED
@@ -19,47 +19,88 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
19
  """
20
  return "What magic will you build ?"
21
 
 
 
 
22
  @tool
23
  def tavily_search(query: str) -> str:
24
- """A tool that performs web search using Tavily API and formats the results.
25
  Args:
26
  query: The search query to look up
27
  Returns:
28
- str: A formatted summary of search results
29
  """
30
  try:
31
  client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])
32
- search_result = client.search(query=query, search_depth="advanced")
 
 
 
 
 
 
 
 
33
 
34
- # Format the results in a user-friendly way
35
- summary = f"Here are the search results for: {query}\n\n"
36
- for result in search_result['results'][:5]: # Top 5 results
37
- summary += f"• {result['title']}\n"
38
- summary += f" Summary: {result['description'][:200]}...\n\n"
 
39
 
40
  return summary
41
  except Exception as e:
42
- return f"Error performing search: {str(e)}"
 
 
43
 
 
44
  @tool
45
- def get_current_time_in_timezone(timezone: str) -> str:
46
- """A tool that fetches and formats the current local time in a specified timezone.
47
  Args:
48
- timezone: A string representing a valid timezone (e.g., 'America/New_York', 'Asia/Tokyo')
49
  Returns:
50
- str: A formatted string containing the timezone and current time
51
  """
52
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  tz = pytz.timezone(timezone)
54
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
55
- # Return a more descriptive string with cleaned timezone name
56
- city = timezone.split('/')[-1].replace('_', ' ')
57
- return f"The current time in {city} is: {local_time}"
 
 
 
 
 
 
 
 
 
58
  except Exception as e:
59
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
60
 
61
 
62
  final_answer = FinalAnswerTool()
 
63
  # Import tool from Hub
64
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
65
 
 
19
  """
20
  return "What magic will you build ?"
21
 
22
+
23
+
24
+ # Tavily safe search tool
25
  @tool
26
  def tavily_search(query: str) -> str:
27
+ """An enhanced search tool that performs web search using Tavily API with better error handling and formatting.
28
  Args:
29
  query: The search query to look up
30
  Returns:
31
+ str: A well-formatted summary of search results with key highlights
32
  """
33
  try:
34
  client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])
35
+ search_result = client.search(
36
+ query=query,
37
+ search_depth="advanced",
38
+ include_domains=["*.gov", "*.edu", "*.org"], # More relaible sources
39
+ exclude_domains=["pinterest.com", "reddit.com"] # Social media excluded
40
+ )
41
+
42
+ summary = f"🔍 Search Results for: {query}\n\n"
43
+ summary += "Key Findings:\n"
44
 
45
+ for idx, result in enumerate(search_result['results'][:5], 1):
46
+ summary += f"\n{idx}. {result['title']}\n"
47
+ summary += f" Source: {result['domain']}\n"
48
+ if 'published_date' in result:
49
+ summary += f" Date: {result['published_date']}\n"
50
+ summary += f" Summary: {result['description'][:250]}...\n"
51
 
52
  return summary
53
  except Exception as e:
54
+ return f"Search Error: {str(e)}. Please try rephrasing your query or try again later."
55
+
56
+
57
 
58
+ # Improved time tool
59
  @tool
60
+ def world_time_tool(location: str) -> str:
61
+ """An improved timezone tool with better location handling and formatted output.
62
  Args:
63
+ location: City name or timezone identifier (e.g., 'Tokyo' or 'Asia/Tokyo')
64
  Returns:
65
+ str: Formatted time information with additional context
66
  """
67
  try:
68
+ # Timezone mapping for common cities
69
+ city_to_timezone = {
70
+ 'tokyo': 'Asia/Tokyo',
71
+ 'new york': 'America/New_York',
72
+ 'london': 'Europe/London',
73
+ 'paris': 'Europe/Paris',
74
+ 'istanbul': 'Europe/Istanbul'
75
+ }
76
+
77
+ # Clean and check input
78
+ clean_location = location.lower().strip()
79
+ if clean_location in city_to_timezone:
80
+ timezone = city_to_timezone[clean_location]
81
+ else:
82
+ timezone = location
83
+
84
  tz = pytz.timezone(timezone)
85
+ current_time = datetime.datetime.now(tz)
86
+
87
+ # Zengin formatlama
88
+ formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
89
+ day_name = current_time.strftime("%A")
90
+
91
+ return f"""
92
+ 📍 Location: {location.title()}
93
+ 🕒 Current Time: {formatted_time}
94
+ 📅 Day: {day_name}
95
+ """
96
+ except pytz.exceptions.UnknownTimeZoneError:
97
+ return f"Error: '{location}' is not a recognized timezone or city. Please try a major city name or standard timezone format."
98
  except Exception as e:
99
+ return f"Time lookup error: {str(e)}"
100
 
101
 
102
  final_answer = FinalAnswerTool()
103
+
104
  # Import tool from Hub
105
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
106