Spaces:
Running
Running
Dhahlan2000
commited on
Commit
·
19bc86f
1
Parent(s):
a10faf8
Add stock analysis tools to app.py: implement sentiment analysis, stock price prediction, and stock comparison features; update requirements.txt to include statsmodels
Browse files- app.py +58 -1
- requirements.txt +2 -1
app.py
CHANGED
@@ -11,6 +11,7 @@ from langchain_huggingface import HuggingFacePipeline
|
|
11 |
from langchain import LLMChain
|
12 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
13 |
from langchain.memory import ConversationBufferWindowMemory
|
|
|
14 |
import torch
|
15 |
import re
|
16 |
from typing import List, Union
|
@@ -64,6 +65,37 @@ def calculate_moving_average(ticker, window=5):
|
|
64 |
hist[f"{window}-day MA"] = hist["Close"].rolling(window=window).mean()
|
65 |
return hist[["Close", f"{window}-day MA"]].tail(5)
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
# Define LangChain tools
|
68 |
stock_data_tool = Tool(
|
69 |
name="Stock Data Fetcher",
|
@@ -83,7 +115,32 @@ moving_average_tool = Tool(
|
|
83 |
description="Calculate the moving average of a stock over a 5-day window."
|
84 |
)
|
85 |
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
# Set up a prompt template with history
|
89 |
template_with_history = """You are SearchGPT, a professional search engine who provides informative answers to users. Answer the following questions as best you can. You have access to the following tools:
|
|
|
11 |
from langchain import LLMChain
|
12 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
13 |
from langchain.memory import ConversationBufferWindowMemory
|
14 |
+
from statsmodels.tsa.arima.model import ARIMA
|
15 |
import torch
|
16 |
import re
|
17 |
from typing import List, Union
|
|
|
65 |
hist[f"{window}-day MA"] = hist["Close"].rolling(window=window).mean()
|
66 |
return hist[["Close", f"{window}-day MA"]].tail(5)
|
67 |
|
68 |
+
def analyze_sentiment(news_articles):
|
69 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
70 |
+
results = [{"title": article["title"],
|
71 |
+
"sentiment": sentiment_pipeline(article["description"] or article["title"])[0]}
|
72 |
+
for article in news_articles]
|
73 |
+
return results
|
74 |
+
|
75 |
+
def predict_stock_price(ticker, days=5):
|
76 |
+
stock = yf.Ticker(ticker)
|
77 |
+
hist = stock.history(period="6mo")
|
78 |
+
if hist.empty:
|
79 |
+
return {"error": f"No data found for ticker {ticker}"}
|
80 |
+
|
81 |
+
|
82 |
+
model = ARIMA(hist["Close"], order=(5, 1, 0))
|
83 |
+
model_fit = model.fit()
|
84 |
+
forecast = model_fit.forecast(steps=days)
|
85 |
+
return forecast.tolist()
|
86 |
+
|
87 |
+
def compare_stocks(ticker1, ticker2):
|
88 |
+
data1 = fetch_stock_data(ticker1)
|
89 |
+
data2 = fetch_stock_data(ticker2)
|
90 |
+
if "error" in data1 or "error" in data2:
|
91 |
+
return {"error": "Could not fetch stock data for comparison."}
|
92 |
+
comparison = {
|
93 |
+
ticker1: {"recent_close": data1["Close"][-1]},
|
94 |
+
ticker2: {"recent_close": data2["Close"][-1]},
|
95 |
+
}
|
96 |
+
return comparison
|
97 |
+
|
98 |
+
|
99 |
# Define LangChain tools
|
100 |
stock_data_tool = Tool(
|
101 |
name="Stock Data Fetcher",
|
|
|
115 |
description="Calculate the moving average of a stock over a 5-day window."
|
116 |
)
|
117 |
|
118 |
+
sentiment_tool = Tool(
|
119 |
+
name="News Sentiment Analyzer",
|
120 |
+
func=lambda ticker: analyze_sentiment(fetch_stock_news(ticker, NEWSAPI_KEY)),
|
121 |
+
description="Analyze the sentiment of recent news articles about a stock ticker."
|
122 |
+
)
|
123 |
+
|
124 |
+
stock_prediction_tool = Tool(
|
125 |
+
name="Stock Price Predictor",
|
126 |
+
func=predict_stock_price,
|
127 |
+
description="Predict future stock prices for a given ticker based on historical data."
|
128 |
+
)
|
129 |
+
|
130 |
+
stock_comparator_tool = Tool(
|
131 |
+
name="Stock Comparator",
|
132 |
+
func=lambda tickers: compare_stocks(*tickers.split(',')),
|
133 |
+
description="Compare the recent performance of two stocks given their tickers, e.g., 'AAPL,MSFT'."
|
134 |
+
)
|
135 |
+
|
136 |
+
tools = [
|
137 |
+
stock_data_tool,
|
138 |
+
stock_news_tool,
|
139 |
+
moving_average_tool,
|
140 |
+
sentiment_tool,
|
141 |
+
stock_prediction_tool,
|
142 |
+
stock_comparator_tool
|
143 |
+
]
|
144 |
|
145 |
# Set up a prompt template with history
|
146 |
template_with_history = """You are SearchGPT, a professional search engine who provides informative answers to users. Answer the following questions as best you can. You have access to the following tools:
|
requirements.txt
CHANGED
@@ -4,4 +4,5 @@ requests
|
|
4 |
pandas
|
5 |
langchain
|
6 |
langchain_huggingface
|
7 |
-
python-dotenv
|
|
|
|
4 |
pandas
|
5 |
langchain
|
6 |
langchain_huggingface
|
7 |
+
python-dotenv
|
8 |
+
statsmodels
|