Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
index:
|
| 17 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 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"
|
| 42 |
-
f"which is a change of {
|
| 43 |
-
f"Write a short, realistic story
|
| 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
|