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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -10,48 +10,43 @@ from Gradio_UI import GradioUI
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
 
10
  #S&P500 Sentiment StoryTeller
11
  @tool
12
  def market_sentiment_story(index: str) -> str:
13
+ """Fetches market data for a stock index and returns a prompt for generating a story.
 
14
  Args:
15
  index: One of 'sp500', 'dowjones', or 'nasdaq'.
16
  """
17
+ # 1. Map user input to Yahoo Finance symbols
 
 
18
  symbols = {
19
  "sp500": "^GSPC",
20
  "dowjones": "^DJI",
21
  "nasdaq": "^IXIC"
22
  }
23
 
24
+ # 2. Get the correct symbol for the requested index
25
+ symbol = symbols.get(index.lower())
26
+ if not symbol:
27
  return "Please choose one of: 'sp500', 'dowjones', or 'nasdaq'."
28
 
 
 
29
  try:
30
+ # 3. Fetch data from Yahoo Finance
31
+ url = f"https://query1.finance.yahoo.com/v8/finance/chart/{symbol}"
32
+ data = requests.get(url).json()
33
+
34
+ # 4. Extract current and previous prices
35
+ result = data["chart"]["result"][0]["meta"]
36
+ current = result["regularMarketPrice"]
37
+ previous = result["chartPreviousClose"]
38
+ change = current - previous
39
+ percent = (change / previous) * 100
40
+
41
+ # 5. Return a prompt for the LLM to generate a story
 
 
42
  return (
43
+ f"The {index.upper()} is currently at {current:.2f}, "
44
+ f"which is a change of {percent:.2f}% from the previous close. "
45
+ f"Write a short, realistic story that explains why it moved in this direction."
46
  )
47
 
48
  except Exception as e:
49
+ return f"Error: {str(e)}"
50
 
51
 
52
  @tool