SSSSSSSiao commited on
Commit
b2fb510
·
verified ·
1 Parent(s): 3cef09e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -30
app.py CHANGED
@@ -1,5 +1,8 @@
 
 
 
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
- import os
3
  import datetime
4
  import requests
5
  import pytz
@@ -10,35 +13,33 @@ 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:
@@ -68,12 +69,6 @@ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may
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)
@@ -83,7 +78,7 @@ with open("prompts.yaml", 'r') as stream:
83
 
84
  agent = CodeAgent(
85
  model=model,
86
- tools=[final_answer, get_coin_price, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
87
  max_steps=6,
88
  verbosity_level=1,
89
  grammar=None,
 
1
+ #!pip install -q yfinance
2
+
3
+
4
+ import yfinance as yf
5
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
 
6
  import datetime
7
  import requests
8
  import pytz
 
13
 
14
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
15
  @tool
16
+ def my_custom_tool(arg1:str, arg2:str)-> str: #it's import to specify the return type
17
  #Keep this format for the description / args / args description but feel free to modify the tool
18
+ """A tool fetches stock information of the provided symbol from Yahoo Finance API
19
  Args:
20
+ arg1: company ticker symbol
21
+ arg2: information requested from the company data dict
22
  """
23
+
24
+ symbol: str = arg1.upper()
25
+ information: str = arg2.lower()
26
+
27
+ try:
28
+
29
+ tick = yf.Ticker(symbol)
30
+ company_data = tick.info
31
 
32
+ if information in company_data:
33
+ return f"{symbol} : {information} = {company_data[information]}"
 
34
 
 
 
 
 
 
 
 
 
 
35
  else:
36
+ available_keys = ", ".join(company_data.keys())
37
+ return f"Request information '{information}' is not available for {symbol}. Available informtion {available_keys} "
38
+
39
  except Exception as e:
40
+ return f"Error fetching data for {symbol}: {str(e)}"
41
+
42
+ # return "What magic will you build ?"
43
 
44
  @tool
45
  def get_current_time_in_timezone(timezone: str) -> str:
 
69
  custom_role_conversions=None,
70
  )
71
 
 
 
 
 
 
 
72
 
73
  # Import tool from Hub
74
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
78
 
79
  agent = CodeAgent(
80
  model=model,
81
+ tools=[final_answer, get_current_time_in_timezone, image_generation_tool,my_custom_tool], ## add your tools here (don't remove final answer)
82
  max_steps=6,
83
  verbosity_level=1,
84
  grammar=None,