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