Kyleshechtman commited on
Commit
4cf532c
·
verified ·
1 Parent(s): b23408e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -29
app.py CHANGED
@@ -9,41 +9,49 @@ from Gradio_UI import GradioUI
9
 
10
  #S&P500 Sentiment StoryTeller
11
  @tool
12
- def market_sentiment_story(index: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 fetches the current price of the a stock exchanges and based on whether the price is up or down generate a short story or explanation for why that might be — even if speculative.
15
  Args:
16
- index: A string representing one of the 3 major exchanges (e.g,'sp500','dowjones', or 'nasdaq')
17
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  try:
19
- # Map user input to Yahoo Finance symbols (I probably can make another function down the line)
20
- ticker = {
21
- "sp500": "^GSPC",
22
- "dowjones": "^DJI",
23
- "nasdaq": "^IXIC"
24
- }
25
- #Get the correct symbol for the requested index
26
- ticker = symbols.get(index.lower())
27
-
28
- # 3. Fetch data from Yahoo Finance
29
- url = f"https://query1.finance.yahoo.com/v8/finance/chart/{symbol}"
30
- data = requests.get(url).json()
31
-
32
- # 4. Extract current and previous prices
33
- result = data["chart"]["result"][0]["meta"]
34
- current = result["regularMarketPrice"]
35
- previous = result["chartPreviousClose"]
36
- change = current - previous
37
- percent = (change / previous) * 100
38
-
39
- # 5. Return a prompt for the LLM to generate a story
40
  return (
41
- f"The {index.upper()} is currently at {current:.2f}, "
42
- f"which is a change of {percent:.2f}% from the previous close. "
43
- f"Write a short, realistic story that explains why it moved in this direction."
44
  )
 
45
  except Exception as e:
46
- return f"Error: {str(e)}"
47
 
48
 
49
  @tool
 
9
 
10
  #S&P500 Sentiment StoryTeller
11
  @tool
12
+ def market_sentiment_story(index: str) -> str:
13
+ """Fetches market index data from Financial Modeling Prep and returns a story prompt.
14
+
15
  Args:
16
+ index: One of 'sp500', 'dowjones', or 'nasdaq'.
17
  """
18
+ index = index.lower()
19
+
20
+ # Mapping user input to FMP-compatible symbols
21
+ symbols = {
22
+ "sp500": "^GSPC",
23
+ "dowjones": "^DJI",
24
+ "nasdaq": "^IXIC"
25
+ }
26
+
27
+ if index not in symbols:
28
+ return "Please choose one of: 'sp500', 'dowjones', or 'nasdaq'."
29
+
30
+ symbol = symbols[index]
31
+
32
  try:
33
+ # Use demo API key from FMP
34
+ url = f"https://financialmodelingprep.com/api/v3/quote/{symbol}?apikey=demo"
35
+ response = requests.get(url)
36
+ data = response.json()
37
+
38
+ if not data or not isinstance(data, list):
39
+ return "Could not retrieve market data at the moment."
40
+
41
+ price_info = data[0]
42
+ name = price_info.get("name", index.upper())
43
+ current_price = price_info["price"]
44
+ change_percent = price_info["changesPercentage"]
45
+
46
+ # Return a prompt for the LLM to expand into a story
 
 
 
 
 
 
 
47
  return (
48
+ f"{name} is currently at {current_price:.2f}, "
49
+ f"which is a change of {change_percent:.2f}% today. "
50
+ f"Write a short, realistic story explaining this market movement."
51
  )
52
+
53
  except Exception as e:
54
+ return f"Error fetching data: {str(e)}"
55
 
56
 
57
  @tool