sascow commited on
Commit
2e63604
·
verified ·
1 Parent(s): e3d49cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -22
app.py CHANGED
@@ -1,26 +1,56 @@
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
-
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(current_time:str, president_name:str)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that gives the meaning of life
15
  Args:
16
- current_time: the current time
17
- president_name: the name of the current president of the USA
18
  """
19
- return f"42 : {president_name} - {current_time}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
 
24
  Args:
25
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
  """
@@ -33,30 +63,24 @@ 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
-
37
  final_answer = FinalAnswerTool()
38
 
39
- # 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:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
-
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- #model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
47
- custom_role_conversions=None,
48
  )
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=[get_current_time_in_timezone, my_custom_tool, DuckDuckGoSearchTool(), final_answer], ## add your tools here (don't remove final answer)
60
  max_steps=6,
61
  verbosity_level=1,
62
  grammar=None,
@@ -66,5 +90,4 @@ agent = CodeAgent(
66
  prompt_templates=prompt_templates
67
  )
68
 
69
-
70
  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
+ import random
7
  from tools.final_answer import FinalAnswerTool
 
8
  from Gradio_UI import GradioUI
9
 
 
10
  @tool
11
+ def magic_8_ball(question: str, asker_name: str) -> str:
12
+ """A Magic 8 Ball tool that provides fortune-telling responses to yes/no questions
13
+
14
  Args:
15
+ question: The question being asked to the Magic 8 Ball
16
+ asker_name: The name of the person asking the question
17
  """
18
+ responses = [
19
+ # Positive responses
20
+ "It is certain.",
21
+ "It is decidedly so.",
22
+ "Without a doubt.",
23
+ "Yes definitely.",
24
+ "You may rely on it.",
25
+ "As I see it, yes.",
26
+ "Most likely.",
27
+ "Outlook good.",
28
+ "Yes.",
29
+ "Signs point to yes.",
30
+
31
+ # Non-committal responses
32
+ "Reply hazy, try again.",
33
+ "Ask again later.",
34
+ "Better not tell you now.",
35
+ "Cannot predict now.",
36
+ "Concentrate and ask again.",
37
+
38
+ # Negative responses
39
+ "Don't count on it.",
40
+ "My reply is no.",
41
+ "My sources say no.",
42
+ "Outlook not so good.",
43
+ "Very doubtful."
44
+ ]
45
+
46
+ # Get random response
47
+ answer = random.choice(responses)
48
+ return f"🎱 Dear {asker_name}, you asked: '{question}'\nThe Magic 8 Ball says: {answer}"
49
 
50
  @tool
51
  def get_current_time_in_timezone(timezone: str) -> str:
52
  """A tool that fetches the current local time in a specified timezone.
53
+
54
  Args:
55
  timezone: A string representing a valid timezone (e.g., 'America/New_York').
56
  """
 
63
  except Exception as e:
64
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
65
 
 
66
  final_answer = FinalAnswerTool()
67
 
 
 
 
68
  model = HfApiModel(
69
+ max_tokens=2096,
70
+ temperature=0.5,
71
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
72
+ custom_role_conversions=None,
 
73
  )
74
 
 
75
  # Import tool from Hub
76
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
77
 
78
  with open("prompts.yaml", 'r') as stream:
79
  prompt_templates = yaml.safe_load(stream)
80
+
81
  agent = CodeAgent(
82
  model=model,
83
+ tools=[get_current_time_in_timezone, magic_8_ball, DuckDuckGoSearchTool(), final_answer],
84
  max_steps=6,
85
  verbosity_level=1,
86
  grammar=None,
 
90
  prompt_templates=prompt_templates
91
  )
92
 
 
93
  GradioUI(agent).launch()