phakphoomq commited on
Commit
1801b40
·
verified ·
1 Parent(s): d44ffea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -75
app.py CHANGED
@@ -1,18 +1,17 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
- import smtplib
7
- from email.mime.multipart import MIMEMultipart
8
- from email.mime.text import MIMEText
9
  from tools.final_answer import FinalAnswerTool
10
- from Gradio_UI import GradioUI
11
 
12
- # -- Your Custom Tools --
13
 
 
14
  @tool
15
- def my_custom_tool(arg1: str, arg2: int) -> str:
 
16
  """A tool that does nothing yet
17
  Args:
18
  arg1: the first argument
@@ -27,88 +26,37 @@ def get_current_time_in_timezone(timezone: str) -> str:
27
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
28
  """
29
  try:
 
30
  tz = pytz.timezone(timezone)
 
31
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
  return f"The current local time in {timezone} is: {local_time}"
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
- @tool
37
- def send_html_email(
38
- smtp_server: str,
39
- smtp_port: int,
40
- sender_email: str,
41
- sender_password: str,
42
- recipient_email: str,
43
- subject: str,
44
- html_content: str
45
- ) -> str:
46
- """
47
- A tool to send an HTML email.
48
- Args:
49
- smtp_server: The SMTP server address (e.g., 'smtp.gmail.com')
50
- smtp_port: The SMTP server port (e.g., 587 for TLS)
51
- sender_email: The sender's email address
52
- sender_password: The sender's email password or app-specific password
53
- recipient_email: The recipient's email address
54
- subject: The subject of the email
55
- html_content: The HTML content of the email
56
- Returns:
57
- A status message indicating success or failure.
58
- """
59
- try:
60
- message = MIMEMultipart()
61
- message['From'] = sender_email
62
- message['To'] = recipient_email
63
- message['Subject'] = subject
64
- message.attach(MIMEText(html_content, 'html'))
65
-
66
- session = smtplib.SMTP(smtp_server, smtp_port)
67
- session.starttls()
68
- session.login(sender_email, sender_password)
69
- session.sendmail(sender_email, recipient_email, message.as_string())
70
- session.quit()
71
-
72
- return "Email sent successfully!"
73
- except Exception as e:
74
- return f"Failed to send email: {str(e)}"
75
 
76
- # -- Custom Final Answer Tool --
 
77
 
78
- class CustomFinalAnswer(FinalAnswerTool):
79
- def __call__(self, answer: str) -> str:
80
- return f"Phoom Said: {super().__call__(answer)}"
81
-
82
- final_answer = CustomFinalAnswer()
83
-
84
- # -- Load Prompts --
85
-
86
- with open("prompts.yaml", 'r') as stream:
87
- prompt_templates = yaml.safe_load(stream)
88
-
89
- prompt_templates['system'] = prompt_templates.get('system', '') + \
90
- "\n\nIMPORTANT: Always begin your final answer with 'Phoom Said:' followed by the response."
91
-
92
- # -- Load Model and Tools --
93
 
94
  model = HfApiModel(
95
- max_tokens=2096,
96
- temperature=0.5,
97
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
98
- custom_role_conversions=None,
99
  )
100
 
 
101
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
102
 
 
 
 
103
  agent = CodeAgent(
104
  model=model,
105
- tools=[
106
- final_answer,
107
- get_current_time_in_timezone,
108
- image_generation_tool,
109
- my_custom_tool,
110
- send_html_email # <<<<< ADDED EMAIL TOOL
111
- ],
112
  max_steps=6,
113
  verbosity_level=1,
114
  grammar=None,
@@ -118,6 +66,5 @@ agent = CodeAgent(
118
  prompt_templates=prompt_templates
119
  )
120
 
121
- # -- Launch UI --
122
 
123
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
 
 
6
  from tools.final_answer import FinalAnswerTool
7
+ from tools.visit_webpage import VisitWebpageTool
8
 
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
 
26
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
27
  """
28
  try:
29
+ # Create timezone object
30
  tz = pytz.timezone(timezone)
31
+ # Get current time in that timezone
32
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
33
  return f"The current local time in {timezone} is: {local_time}"
34
  except Exception as e:
35
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ final_answer = FinalAnswerTool()
39
+ VisitWebpage = VisitWebpageTool()
40
 
41
+ # 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:
42
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  model = HfApiModel(
45
+ max_tokens=2096,
46
+ temperature=0.5,
47
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
48
+ custom_role_conversions=None,
49
  )
50
 
51
+ # Import tool from Hub
52
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
53
 
54
+ with open("prompts.yaml", 'r') as stream:
55
+ prompt_templates = yaml.safe_load(stream)
56
+
57
  agent = CodeAgent(
58
  model=model,
59
+ tools=[final_answer,my_custom_tool,get_current_time_in_timezone,image_generation_tool,VisitWebpage], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
60
  max_steps=6,
61
  verbosity_level=1,
62
  grammar=None,
 
66
  prompt_templates=prompt_templates
67
  )
68
 
 
69
 
70
+ GradioUI(agent).launch()