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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
@@ -8,20 +8,18 @@ import os
8
  from tavily import TavilyClient
9
  from Gradio_UI import GradioUI
10
 
11
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
12
  @tool
13
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
14
- #Keep this format for the description / args / args description but feel free to modify the tool
15
  """A tool that does nothing yet
16
  Args:
17
  arg1: the first argument
18
  arg2: the second argument
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.
@@ -35,8 +33,8 @@ def tavily_search(query: str) -> str:
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"
@@ -54,10 +52,8 @@ def tavily_search(query: str) -> str:
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')
@@ -65,7 +61,7 @@ def world_time_tool(location: str) -> str:
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',
@@ -74,7 +70,7 @@ def world_time_tool(location: str) -> str:
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]
@@ -84,44 +80,41 @@ def world_time_tool(location: str) -> str:
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
 
107
-
108
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
109
- model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
110
 
111
  # Model configuration
 
112
  model = HfApiModel(
113
  max_tokens=2096,
114
  temperature=0.3, # Lower temperature for more consistent responses
115
- model_id=model_id, # 'Qwen/Qwen2.5-Coder-32B-Instruct',
116
  custom_role_conversions=None,
117
  )
118
 
119
-
120
-
121
-
122
  with open("prompts.yaml", 'r') as stream:
123
  prompt_templates = yaml.safe_load(stream)
124
-
 
125
  agent = CodeAgent(
126
  model=model,
127
  tools=[final_answer, tavily_search, get_current_time_in_timezone, image_generation_tool], # Added tavily_search
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
 
8
  from tavily import TavilyClient
9
  from Gradio_UI import GradioUI
10
 
11
+ # Example tool template - can be modified for your needs
12
+
13
  @tool
14
+ def my_custom_tool(arg1: str, arg2: int) -> str: # Return type specification is important
 
15
  """A tool that does nothing yet
16
  Args:
17
  arg1: the first argument
18
  arg2: the second argument
19
  """
20
+ return "What magic will you build?"
 
21
 
22
 
 
23
  @tool
24
  def tavily_search(query: str) -> str:
25
  """An enhanced search tool that performs web search using Tavily API with better error handling and formatting.
 
33
  search_result = client.search(
34
  query=query,
35
  search_depth="advanced",
36
+ include_domains=["*.gov", "*.edu", "*.org"], # More reliable sources
37
+ exclude_domains=["pinterest.com", "reddit.com"] # Exclude social media
38
  )
39
 
40
  summary = f"🔍 Search Results for: {query}\n\n"
 
52
  return f"Search Error: {str(e)}. Please try rephrasing your query or try again later."
53
 
54
 
 
 
55
  @tool
56
+ def get_current_time_in_timezone(location: str) -> str:
57
  """An improved timezone tool with better location handling and formatted output.
58
  Args:
59
  location: City name or timezone identifier (e.g., 'Tokyo' or 'Asia/Tokyo')
 
61
  str: Formatted time information with additional context
62
  """
63
  try:
64
+ # Common city to timezone mappings
65
  city_to_timezone = {
66
  'tokyo': 'Asia/Tokyo',
67
  'new york': 'America/New_York',
 
70
  'istanbul': 'Europe/Istanbul'
71
  }
72
 
73
+ # Input validation and normalization
74
  clean_location = location.lower().strip()
75
  if clean_location in city_to_timezone:
76
  timezone = city_to_timezone[clean_location]
 
80
  tz = pytz.timezone(timezone)
81
  current_time = datetime.datetime.now(tz)
82
 
83
+ # Format the output
84
  formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
85
  day_name = current_time.strftime("%A")
86
 
87
  return f"""
88
+ 📍 Location: {location.title()}
89
+ 🕒 Current Time: {formatted_time}
90
+ 📅 Day: {day_name}
91
+ """
92
  except pytz.exceptions.UnknownTimeZoneError:
93
  return f"Error: '{location}' is not a recognized timezone or city. Please try a major city name or standard timezone format."
94
  except Exception as e:
95
  return f"Time lookup error: {str(e)}"
96
 
97
+ # Initialize tools
98
  final_answer = FinalAnswerTool()
 
 
99
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
100
 
101
+ # If the agent does not answer, the model is overloaded, please use another model or
102
+ # the following Hugging Face Endpoint that also contains qwen2.5 coder:
 
103
 
104
  # Model configuration
105
+ model_id = 'https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
106
  model = HfApiModel(
107
  max_tokens=2096,
108
  temperature=0.3, # Lower temperature for more consistent responses
109
+ model_id=model_id, # 'Qwen/Qwen2.5-Coder-32B-Instruct',
110
  custom_role_conversions=None,
111
  )
112
 
113
+ # Load prompt templates
 
 
114
  with open("prompts.yaml", 'r') as stream:
115
  prompt_templates = yaml.safe_load(stream)
116
+
117
+ # Initialize agent with tools
118
  agent = CodeAgent(
119
  model=model,
120
  tools=[final_answer, tavily_search, get_current_time_in_timezone, image_generation_tool], # Added tavily_search