Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
index: One of 'sp500', 'dowjones', or 'nasdaq'.
|
| 17 |
"""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Mapping user input to FMP-compatible symbols
|
| 21 |
symbols = {
|
| 22 |
"sp500": "^GSPC",
|
| 23 |
"dowjones": "^DJI",
|
| 24 |
"nasdaq": "^IXIC"
|
| 25 |
}
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
return "Please choose one of: 'sp500', 'dowjones', or 'nasdaq'."
|
| 29 |
|
| 30 |
-
symbol = symbols[index]
|
| 31 |
-
|
| 32 |
try:
|
| 33 |
-
#
|
| 34 |
-
url = f"https://
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Return a prompt for the LLM to expand into a story
|
| 47 |
return (
|
| 48 |
-
f"{
|
| 49 |
-
f"which is a change of {
|
| 50 |
-
f"Write a short, realistic story
|
| 51 |
)
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
-
return f"Error
|
| 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
|