-
Notifications
You must be signed in to change notification settings - Fork 7
/
create_real_article.py
154 lines (120 loc) · 5.59 KB
/
create_real_article.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from datetime import datetime, timedelta
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import pandas as pd
import yfinance as yf
from langchain.prompts import ChatPromptTemplate
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from dotenv import load_dotenv, find_dotenv
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://user:password@db:5432/dbname"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
class Article(db.Model):
__tablename__ = "articles"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), nullable=False)
author = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text, nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
stock_data = db.relationship("StockData", backref="article", lazy=True)
class StockData(db.Model):
__tablename__ = "stock_data"
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime, nullable=False)
ticker = db.Column(db.String(20), nullable=False)
value = db.Column(db.Float, nullable=False)
article_id = db.Column(db.Integer, db.ForeignKey("articles.id"), nullable=False)
class StockDataFetcher:
def __init__(self, tickers):
self.tickers = tickers
def fetch(self):
all_data = []
end_date = datetime.today().strftime("%Y-%m-%d")
start_date = (datetime.today() - timedelta(days=7)).strftime("%Y-%m-%d")
for ticker in self.tickers:
data = yf.download(ticker, start=start_date, end=end_date, interval="1d")
data.reset_index(inplace=True)
data["Ticker"] = ticker
all_data.append(data[["Date", "Ticker", "Close"]])
all_data = pd.concat(all_data)
pivoted_data = all_data.pivot(index="Date", columns="Ticker", values="Close")
pivoted_data.fillna("N/A", inplace=True)
return pivoted_data
class StockDataAnalyzer:
def __init__(self):
self.llm = OpenAI()
self.template = """
"You are a financial expert specializing in analyzing trends in the stock market. Your role involves the weekly assessment of market performance, evaluating key indicators such as opening and closing prices, highs and lows, volumes of trade, and changes in percentages. You dissect this data to understand the market's sentiment, whether positive, neutral, or negative. You distill complex financial jargon into clear, digestible reports, helping others to understand the subject matter at hand. Utilizing your deep understanding of the market, you interpret these factors to provide a concise summary of the week's events in the financial world."
Include the dates in your analysis and start with: In this week (startdate - enddate) ...
Weekly Stock data: {data}
"""
self.prompt_template = ChatPromptTemplate.from_template(template=self.template)
self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
def analyze(self, data):
return self.chain.predict(data=data)
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import SQLAlchemyError
from contextlib import contextmanager
class ArticleDatabase:
def __init__(self, db_url):
self.engine = create_engine(db_url)
self.Session = sessionmaker(bind=self.engine)
@contextmanager
def session_scope(self):
"""Provide a transactional scope around a series of operations."""
session = self.Session()
try:
yield session
session.commit()
except SQLAlchemyError as e:
print("Failed to commit transaction. Error: ", str(e))
session.rollback()
raise
finally:
session.close()
def insert_article(self, title, content, author):
with self.session_scope() as session:
article = Article(
title=title,
content=content,
author=author,
date_posted=datetime.utcnow(),
)
session.add(article)
session.commit()
return article.id
def insert_stock_data(self, article_id, stock_data):
with self.session_scope() as session:
for date, data_per_date in stock_data.iterrows():
for ticker, value in data_per_date.items():
stock_data = StockData(
date=date, ticker=ticker, value=value, article_id=article_id
)
session.add(stock_data)
def insert_article_with_stock_data(self, title, content, author, stock_data):
try:
article_id = self.insert_article(title, content, author)
self.insert_stock_data(article_id, stock_data)
except Exception as e:
print(
f"Failed to insert stock data because article insertion failed. Error: {str(e)}"
)
import os
if __name__ == "__main__":
load_dotenv(find_dotenv())
tickers = ["AAPL", "MSFT", "AMZN", "GOOGL", "BRK-B", "V", "JNJ", "WMT", "PG"]
article_title = "Weekly Stock Market Analysis"
article_author = "Financial Expert"
fetcher = StockDataFetcher(tickers)
analyzer = StockDataAnalyzer()
db_url = "postgresql://user:password@db:5432/dbname"
db = ArticleDatabase(db_url=db_url)
stock_data = fetcher.fetch()
analysis = analyzer.analyze(stock_data)
db.insert_article_with_stock_data(
article_title, analysis, article_author, stock_data
)