SSSSSSSiao commited on
Commit
8cc9637
·
verified ·
1 Parent(s): 328fcdb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -5
app.py CHANGED
@@ -1,4 +1,5 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
 
2
  import datetime
3
  import requests
4
  import pytz
@@ -7,33 +8,89 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  final_answer = FinalAnswerTool()
12
 
 
 
 
13
  model = HfApiModel(
14
  max_tokens=2096,
15
  temperature=0.5,
16
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
17
  custom_role_conversions=None,
18
  )
19
 
 
 
 
 
 
 
 
20
  # Import tool from Hub
21
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
22
 
23
- # Load system prompt from prompt.yaml file
24
  with open("prompts.yaml", 'r') as stream:
25
  prompt_templates = yaml.safe_load(stream)
26
 
27
  agent = CodeAgent(
28
  model=model,
29
- tools=[final_answer,DuckDuckGoSearchTool()], # add your tools here (don't remove final_answer)
30
  max_steps=6,
31
  verbosity_level=1,
32
  grammar=None,
33
  planning_interval=None,
34
  name=None,
35
  description=None,
36
- prompt_templates=prompt_templates # Pass system prompt to CodeAgent
37
  )
38
 
39
 
 
1
+ from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ import os
3
  import datetime
4
  import requests
5
  import pytz
 
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 get_coin_price(symbol: str, currency: str = "usd")-> 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 fetches the latest price on a cryptocurrency symbol from Coingecko API.
16
+ Args:
17
+ symbol: A string representing a valid symbol as CoinGecko's ID, not ticker (e.g., 'bitcoin' not BTC).
18
+ currency: A string representing the currency the value is returned in (e.g., 'usd').
19
+ """
20
+ # set to lowercase for api compatibility and consistency
21
+ symbol = symbol.lower()
22
+ currency = currency.lower()
23
+
24
+ #define the endpoint with return headers - https://docs.coingecko.com/v3.0.1/reference/simple-price
25
+ url = "https://api.coingecko.com/api/v3/simple/price?ids=" + symbol + "&vs_currencies=" + currency
26
+ headers = {"accept": "application/json"}
27
+
28
+ try:
29
+ #make request to endpoint
30
+ response = requests.get(url, headers=headers)
31
+
32
+ #process response
33
+ if response.status_code == 200:
34
+ data = response.json()
35
+ usd_value = data[symbol][currency]
36
+ return f"The price of " + symbol + " in " + currency + " is " + f"${usd_value:,.2f}" #format as currency
37
+ else:
38
+ # returrn verbose error, so the model can observe and ReAct
39
+ return f"Error: {response.status_code}, {response.text}"
40
+ except Exception as e:
41
+ return f"Error fetching price for '{symbol}' valued in currency '{currency}': {str(e)}"
42
+
43
+ @tool
44
+ def get_current_time_in_timezone(timezone: str) -> str:
45
+ """A tool that fetches the current local time in a specified timezone.
46
+ Args:
47
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
48
+ """
49
+ try:
50
+ # Create timezone object
51
+ tz = pytz.timezone(timezone)
52
+ # Get current time in that timezone
53
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
54
+ return f"The current local time in {timezone} is: {local_time}"
55
+ except Exception as e:
56
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
57
+
58
 
59
  final_answer = FinalAnswerTool()
60
 
61
+ # 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:
62
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
63
+
64
  model = HfApiModel(
65
  max_tokens=2096,
66
  temperature=0.5,
67
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
68
  custom_role_conversions=None,
69
  )
70
 
71
+ # model = HfApiModel(
72
+ # model_id="deepseek-ai/DeepSeek-R1",
73
+ # provider="together",
74
+ # api_key=os.getenv("together_key") # Fetch API key from environment
75
+ # )
76
+
77
+
78
  # Import tool from Hub
79
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
80
 
 
81
  with open("prompts.yaml", 'r') as stream:
82
  prompt_templates = yaml.safe_load(stream)
83
 
84
  agent = CodeAgent(
85
  model=model,
86
+ tools=[final_answer, get_coin_price, get_current_time_in_timezone, DuckDuckGoSearchTool()], ## add your tools here (don't remove final answer)
87
  max_steps=6,
88
  verbosity_level=1,
89
  grammar=None,
90
  planning_interval=None,
91
  name=None,
92
  description=None,
93
+ prompt_templates=prompt_templates
94
  )
95
 
96