llllllllllllllllllllllllllleeeeeeeeeeeeee
commited on
Commit
โข
a003bf6
1
Parent(s):
91f296a
Update app.py
Browse files
app.py
CHANGED
@@ -1,247 +1,5 @@
|
|
1 |
-
import subprocess
|
2 |
-
|
3 |
-
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ๋ฐ ์
๋ฐ์ดํธ
|
4 |
-
subprocess.run(["pip", "install", "--upgrade", "pip"])
|
5 |
-
subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
|
6 |
-
|
7 |
-
import os
|
8 |
-
import matplotlib.font_manager as fm
|
9 |
-
import matplotlib.pyplot as plt
|
10 |
-
import yfinance as yf
|
11 |
-
import numpy as np
|
12 |
-
import re # ํ๊ธ, ์ซ์, ๊ธฐํธ๋ฅผ ๋จ๊ธฐ๊ธฐ ์ํ ์ ๊ท ํํ์์ ์ฌ์ฉ
|
13 |
-
import gradio as gr
|
14 |
-
import io
|
15 |
-
from PIL import Image
|
16 |
-
from datetime import datetime, timedelta
|
17 |
-
from openai import OpenAI
|
18 |
-
|
19 |
-
# 1. ๋๋๊ณ ๋ ํฐํธ ์ค์น ๋ฐ ์ ์ฉ
|
20 |
-
def install_nanum_font():
|
21 |
-
try:
|
22 |
-
subprocess.run(["apt-get", "update"], check=True)
|
23 |
-
subprocess.run(["apt-get", "install", "-y", "fonts-nanum"], check=True)
|
24 |
-
subprocess.run(["fc-cache", "-fv"], check=True)
|
25 |
-
except Exception as e:
|
26 |
-
print(f"ํฐํธ ์ค์น ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}")
|
27 |
-
|
28 |
-
install_nanum_font()
|
29 |
-
|
30 |
-
# ๋๋๊ณ ๋ ํฐํธ ๊ฒฝ๋ก ์ค์ ๋ฐ ๊ฐ์ ์ ์ฉ
|
31 |
-
font_path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf'
|
32 |
-
|
33 |
-
if os.path.exists(font_path):
|
34 |
-
fm.fontManager.addfont(font_path)
|
35 |
-
else:
|
36 |
-
print("ํฐํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
37 |
-
|
38 |
-
# ๋๋๊ณ ๋ ํฐํธ ๊ฐ์ ์ ์ฉ
|
39 |
-
font_prop = fm.FontProperties(fname=font_path)
|
40 |
-
plt.rcParams['font.family'] = font_prop.get_name()
|
41 |
-
plt.rcParams['axes.unicode_minus'] = False # ๋ง์ด๋์ค ๋ถํธ ๊นจ์ง ๋ฐฉ์ง
|
42 |
-
|
43 |
-
# 2. yfinance๋ก ์ฃผ๊ฐ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ (์ผ์ฑ์ ์ ์์)
|
44 |
-
ticker = '005930.KS' # ์ผ์ฑ์ ์ ํฐ์ปค
|
45 |
-
stock = yf.Ticker(ticker)
|
46 |
-
stock_data = stock.history(period="max")
|
47 |
-
|
48 |
-
# ์ฃผ๊ฐ ๋ฐ์ดํฐ ์ถ๋ ฅ ํ์ธ
|
49 |
-
print(stock_data.head()) # ์ฃผ๊ฐ ๋ฐ์ดํฐ ์ผ๋ถ ํ์ธ
|
50 |
-
|
51 |
-
# 3. ์ฃผ๊ฐ ๋ฐ์ดํฐ ์๊ฐํ (pandas ๋ฐ์ดํฐํ๋ ์์ numpy ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ์ฒ๋ฆฌ)
|
52 |
-
plt.figure(figsize=(10, 6))
|
53 |
-
|
54 |
-
# ์ธ๋ฑ์ค์ ์ข
๊ฐ ๋ฐ์ดํฐ๋ฅผ numpy ๋ฐฐ์ด๋ก ๋ณํ
|
55 |
-
dates = stock_data.index.to_numpy()
|
56 |
-
closing_prices = stock_data['Close'].to_numpy()
|
57 |
-
|
58 |
-
# ์ฃผ๊ฐ ๋ฐ์ดํฐ ์๊ฐํ
|
59 |
-
plt.plot(dates, closing_prices, label='์ผ์ฑ์ ์ ์ข
๊ฐ')
|
60 |
-
|
61 |
-
# ๊ทธ๋ํ ์ ๋ชฉ, ์ถ ๋ผ๋ฒจ์ ํ๊ธ๋ก ์ค์
|
62 |
-
plt.title('์ผ์ฑ์ ์ ์ฃผ๊ฐ ์ถ์ด', fontproperties=font_prop)
|
63 |
-
plt.xlabel('๋ ์ง', fontproperties=font_prop)
|
64 |
-
plt.ylabel('์ข
๊ฐ', fontproperties=font_prop)
|
65 |
-
plt.legend(prop=font_prop)
|
66 |
-
|
67 |
-
# B ์ฝ๋
|
68 |
-
|
69 |
-
# Perplexity AI API ์ค์
|
70 |
-
API_KEY = "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" # ์ฌ๊ธฐ์ Perplexity AI API ํค๋ฅผ ์
๋ ฅํ์ธ์.
|
71 |
-
|
72 |
-
# ๋ด์ค ์์ฝ์ ๊ฐ์ ธ์ค๋ ํจ์
|
73 |
-
def get_real_news_summary(company, date):
|
74 |
-
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
75 |
-
client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
|
76 |
-
|
77 |
-
# ๋ ์ง ํ์์ ๋ง์ถฐ์ฃผ๊ธฐ ์ํ ์ฒ๋ฆฌ
|
78 |
-
target_date = datetime.strptime(date, '%Y-%m-%d')
|
79 |
-
start_date = (target_date - timedelta(days=1)).strftime('%Y-%m-%d')
|
80 |
-
end_date = (target_date + timedelta(days=1)).strftime('%Y-%m-%d')
|
81 |
-
|
82 |
-
# API ์์ฒญ์ ์ํ ๋ฉ์์ง ๊ตฌ์ฑ - ํ๊ตญ์ด๋ก๋ง ์๋ต์ ๋ฐ๋๋ก ์ง์
|
83 |
-
messages = [
|
84 |
-
{"role": "system", "content": "You are a helpful assistant that summarizes stock news strictly in Korean."},
|
85 |
-
{"role": "user", "content": f"Summarize the stock news for {company} between {start_date} and {end_date} in Korean. Only focus on news within this date range."}
|
86 |
-
]
|
87 |
-
|
88 |
-
try:
|
89 |
-
# API ์์ฒญ
|
90 |
-
response = client.chat.completions.create(
|
91 |
-
model="llama-3.1-sonar-large-128k-online",
|
92 |
-
messages=messages
|
93 |
-
)
|
94 |
-
|
95 |
-
# ์๋ต์์ ์์ฝ ์ถ์ถ
|
96 |
-
summary = response.choices[0].message.content
|
97 |
-
|
98 |
-
# ํ๊ธ, ์ซ์, ๊ณต๋ฐฑ, ํน์ ๊ธฐํธ๋ง ๋จ๊ธฐ๋ ์ ๊ท ํํ์
|
99 |
-
korean_only_summary = re.sub(r'[^\w\s#.,!%()\-\[\]]', '', summary)
|
100 |
-
|
101 |
-
# ##๋ก ์์ํ๋ ๋ถ๋ถ์ **์ผ๋ก ๊ฐ์ธ์ Bold ์ฒ๋ฆฌ
|
102 |
-
formatted_summary = re.sub(r'##\s*(.+)', r'**\1**', korean_only_summary)
|
103 |
-
|
104 |
-
return formatted_summary
|
105 |
-
except Exception as e:
|
106 |
-
return f"๋ด์ค ์์ฝ ์ค ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
107 |
-
|
108 |
-
# ๋ด์ค ์์ฝ์ ๊ฐ์ ธ์ค๋ ํจ์
|
109 |
-
def handle_click(company_name, date_clicked):
|
110 |
-
return get_real_news_summary(company_name, date_clicked)
|
111 |
-
|
112 |
-
# Gradio์์ ์ฌ์ฉํ ํจ์ (๋ด์ค ์์ฝ ํฌํจ)
|
113 |
-
def update_news(input_value, selected_date):
|
114 |
-
if selected_date == "" or selected_date is None:
|
115 |
-
return "๋ ์ง๋ฅผ ์ ํํด์ฃผ์ธ์."
|
116 |
-
else:
|
117 |
-
# ์ข
๋ชฉ๋ช
์ ๊ฐ์ ธ์์ Perplexity๋ก ๊ฒ์
|
118 |
-
ticker = name_to_ticker.get(input_value, input_value)
|
119 |
-
company_name = input_value if ticker == input_value else list(name_to_ticker.keys())[list(name_to_ticker.values()).index(ticker)]
|
120 |
-
return handle_click(company_name, selected_date)
|
121 |
-
|
122 |
-
# ์ข
๋ชฉ๋ช
๊ณผ ํฐ์ปค๋ฅผ ๋งคํํ๋ ๋์
๋๋ฆฌ ํ์ฅ ๋ฐ ์กฐ๊ฑด๋ณ ์ข
๋ชฉ ๋งคํ
|
123 |
-
name_to_ticker = {
|
124 |
-
"์ผ์ฑ์ ์": "005930.KS",
|
125 |
-
"SK๋ฐ์ด์คํ": "326030.KS",
|
126 |
-
"Apple": "AAPL",
|
127 |
-
"Nvidia": "NVDA",
|
128 |
-
"Vertex": "VRTX",
|
129 |
-
"ํ๋์ฐจ": "005380.KS",
|
130 |
-
"์นด์นด์ค": "035720.KS",
|
131 |
-
"LGํํ": "051910.KS",
|
132 |
-
"์
ํธ๋ฆฌ์จ": "068270.KS",
|
133 |
-
"๋ค์ด๋ฒ": "035420.KS",
|
134 |
-
"์์ฝํ๋ก๋น์ ": "247540.KS",
|
135 |
-
"์ํ
์ค์ ": "196170.KQ",
|
136 |
-
# ์ต์ ์๊ฐ์ด์ก ์์ ์ข
๋ชฉ์ ๋ฐ์ํ์ฌ ์
๋ฐ์ดํธ
|
137 |
-
"๋์ค๋ฅ ์์ด 1์": "AAPL", # Apple
|
138 |
-
"๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์": "VRTX", # Vertex Pharmaceuticals
|
139 |
-
"๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์": "LLY", # Eli Lilly
|
140 |
-
"์ฝ์คํผ ์์ด 1์": "005930.KS", # ์ผ์ฑ์ ์
|
141 |
-
"์ฝ์ค๋ฅ ์์ด 1์": "196170.KQ", # ์ํ
์ค์
|
142 |
-
}
|
143 |
-
|
144 |
-
# ์ฃผ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์กฐ๊ฑด์ ๋ง๋ ๋ ์ง์ ๊ทธ๋ํ๋ฅผ ๋ฐํํ๋ ํจ์
|
145 |
-
def display_stock_with_highlight(input_value, change_type, percent_change):
|
146 |
-
try:
|
147 |
-
# ์
๋ ฅ๊ฐ์ ํฐ์ปค๋ก ๋ณํ
|
148 |
-
ticker = name_to_ticker.get(input_value, input_value)
|
149 |
-
stock = yf.Ticker(ticker)
|
150 |
-
stock_data = stock.history(period="5y") # ์ต๊ทผ 5๋
๋ฐ์ดํฐ๋ก ์ ํ
|
151 |
-
|
152 |
-
if stock_data.empty:
|
153 |
-
return "์ฃผ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", []
|
154 |
-
|
155 |
-
stock_data['Change'] = stock_data['Close'].pct_change() * 100
|
156 |
-
|
157 |
-
percent_change = float(percent_change)
|
158 |
-
|
159 |
-
if change_type == "์์น":
|
160 |
-
highlight_data = stock_data[stock_data['Change'] >= percent_change]
|
161 |
-
color = "darkorange"
|
162 |
-
elif change_type == "ํ๋ฝ":
|
163 |
-
highlight_data = stock_data[stock_data['Change'] <= -percent_change]
|
164 |
-
color = "purple"
|
165 |
-
else:
|
166 |
-
return "Invalid change type", []
|
167 |
-
|
168 |
-
dates = stock_data.index.to_numpy()
|
169 |
-
closing_prices = stock_data['Close'].to_numpy()
|
170 |
-
|
171 |
-
plt.figure(figsize=(10, 6))
|
172 |
-
plt.plot(dates, closing_prices, color='gray', label=input_value)
|
173 |
-
plt.scatter(highlight_data.index, highlight_data['Close'], color=color, label=f'{change_type} ํฌ์ธํธ')
|
174 |
-
|
175 |
-
for index, row in highlight_data.iterrows():
|
176 |
-
plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
|
177 |
-
plt.axvline(x=index, color=color, linestyle='--', linewidth=1) # x์ถ๊ณผ์ ์ฐ๊ฒฐ์ ์ ์ ์ผ๋ก ํ์
|
178 |
-
|
179 |
-
# ์ข
๋ชฉ๋ช
+ '์ฃผ๊ฐ ์ถ์ด'๋ก ์ ๋ชฉ ์ค์ (์ข
๋ชฉ๋ช
๊ธฐ๋ฐ)
|
180 |
-
company_name = list(name_to_ticker.keys())[list(name_to_ticker.values()).index(ticker)]
|
181 |
-
plt.title(f'{company_name} ์ฃผ๊ฐ ์ถ์ด', fontproperties=font_prop)
|
182 |
-
plt.xlabel('๋ ์ง', fontproperties=font_prop)
|
183 |
-
plt.ylabel('์ข
๊ฐ', fontproperties=font_prop)
|
184 |
-
plt.legend()
|
185 |
-
|
186 |
-
buf = io.BytesIO()
|
187 |
-
plt.savefig(buf, format='png')
|
188 |
-
plt.close()
|
189 |
-
buf.seek(0)
|
190 |
-
img = Image.open(buf)
|
191 |
-
|
192 |
-
highlight_dates = highlight_data.index.strftime('%Y-%m-%d').tolist()
|
193 |
-
|
194 |
-
return img, gr.update(choices=highlight_dates)
|
195 |
-
except Exception as e:
|
196 |
-
return f"Error processing data: {e}", gr.update(choices=[])
|
197 |
-
|
198 |
-
# Gradio ์ธํฐํ์ด์ค ์์ฑ (3์ด ๋ ์ด์์)
|
199 |
with gr.Blocks() as demo:
|
200 |
gr.Markdown("## ์ฃผ๊ฐ ๊ทธ๋ํ์ ๋ด์ค ์์ฝ")
|
201 |
|
202 |
-
# CSS
|
203 |
-
demo.
|
204 |
-
|
205 |
-
with gr.Row():
|
206 |
-
with gr.Column(): # ์
๋ ฅ๊ฐ์ ๋ด์ ์ฒซ ๋ฒ์งธ ์ด
|
207 |
-
input_value = gr.Textbox(label="์ข
๋ชฉ๋ช
๋๋ ํฐ์ปค ์
๋ ฅ", placeholder="์: SK๋ฐ์ด์คํ, AAPL")
|
208 |
-
change_type = gr.Dropdown(choices=["์์น", "ํ๋ฝ"], label="์์น ๋๋ ํ๋ฝ ์ ํ", value="์์น")
|
209 |
-
percent_change = gr.Textbox(label="๋ณ๋ ํผ์ผํธ (%)", placeholder="์: 10", value="10")
|
210 |
-
|
211 |
-
submit_btn = gr.Button("Submit")
|
212 |
-
|
213 |
-
# ์์ (์ด์ ๋ ์ด์์์ผ๋ก ๋ณต์)
|
214 |
-
examples = [["SK๋ฐ์ด์คํ"],
|
215 |
-
["๋์ค๋ฅ ์์ด 1์"],
|
216 |
-
["๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์"],
|
217 |
-
["๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์"],
|
218 |
-
["์ฝ์คํผ ์์ด 1์"],
|
219 |
-
["์ฝ์ค๋ฅ ์์ด 1์"]]
|
220 |
-
gr.Examples(examples=examples, inputs=[input_value])
|
221 |
-
|
222 |
-
with gr.Column(): # ๊ทธ๋ํ๋ฅผ ์ถ๋ ฅํ ๋ ๋ฒ์งธ ์ด
|
223 |
-
plot = gr.Image(label="์ฃผ๊ฐ ๊ทธ๋ํ")
|
224 |
-
date_dropdown = gr.Dropdown(label="์กฐ๊ฑด์ ํด๋นํ๋ ๋ ์ง ์ ํ", choices=[])
|
225 |
-
|
226 |
-
with gr.Column(): # ๋ด์ค ์์ฝ์ ์ถ๋ ฅํ ์ธ ๋ฒ์งธ ์ด
|
227 |
-
# news_output์ gr.Markdown์ผ๋ก ๋ณ๊ฒฝ, ๋น ์นธ์ ํ์ ๋ฐฐ๊ฒฝ์ผ๋ก ์ค์
|
228 |
-
news_output = gr.Markdown(label="๋ด์ค ์์ฝ", value="", elem_id="news_output_box")
|
229 |
-
|
230 |
-
# Submit ๋ฒํผ ํด๋ฆญ ์ ๊ทธ๋ํ ๋ฐ ๋ ์ง ๋๋กญ๋ค์ด ์
๋ฐ์ดํธ
|
231 |
-
submit_btn.click(
|
232 |
-
fn=display_stock_with_highlight,
|
233 |
-
inputs=[input_value, change_type, percent_change],
|
234 |
-
outputs=[plot, date_dropdown]
|
235 |
-
)
|
236 |
-
|
237 |
-
# ๋ ์ง ์ ํ ์ ๋ด์ค ์์ฝ ์
๋ฐ์ดํธ
|
238 |
-
date_dropdown.change(
|
239 |
-
fn=update_news,
|
240 |
-
inputs=[input_value, date_dropdown],
|
241 |
-
outputs=[news_output]
|
242 |
-
)
|
243 |
-
|
244 |
-
# Gradio ์คํ
|
245 |
-
demo.launch()
|
246 |
-
|
247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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