llllllllllllllllllllllllllleeeeeeeeeeeeee
commited on
Commit
โข
7628d13
1
Parent(s):
56c0ae0
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,4 @@
|
|
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
|
@@ -16,13 +11,171 @@ from PIL import Image
|
|
16 |
from datetime import datetime, timedelta
|
17 |
from openai import OpenAI
|
18 |
|
19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ (3์ด ๋ ์ด์์)
|
22 |
with gr.Blocks() as demo:
|
23 |
gr.Markdown("## ์ฃผ๊ฐ ๊ทธ๋ํ์ ๋ด์ค ์์ฝ")
|
24 |
|
25 |
-
# CSS๋ก 3์ด ๋ด์ค ์์ฝ ํ์ดํ์ด ๋ฌ๋ฆฐ ํ์ ๋ค๋ชจ ์นธ ์์ฑ
|
26 |
demo.css("#news_output_box { background-color: #f0f0f0; padding: 20px; }")
|
27 |
|
28 |
with gr.Row():
|
@@ -33,7 +186,7 @@ with gr.Blocks() as demo:
|
|
33 |
|
34 |
submit_btn = gr.Button("Submit")
|
35 |
|
36 |
-
# ์์
|
37 |
examples = [["SK๋ฐ์ด์คํ"],
|
38 |
["๋์ค๋ฅ ์์ด 1์"],
|
39 |
["๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์"],
|
@@ -47,7 +200,6 @@ with gr.Blocks() as demo:
|
|
47 |
date_dropdown = gr.Dropdown(label="์กฐ๊ฑด์ ํด๋นํ๋ ๋ ์ง ์ ํ", choices=[])
|
48 |
|
49 |
with gr.Column(): # ๋ด์ค ์์ฝ์ ์ถ๋ ฅํ ์ธ ๋ฒ์งธ ์ด
|
50 |
-
# news_output์ gr.Markdown์ผ๋ก ๋ณ๊ฒฝ, ๋น ์นธ์ ํ์ ๋ฐฐ๊ฒฝ์ผ๋ก ์ค์
|
51 |
news_output = gr.Markdown(label="๋ด์ค ์์ฝ", value="", elem_id="news_output_box")
|
52 |
|
53 |
# Submit ๋ฒํผ ํด๋ฆญ ์ ๊ทธ๋ํ ๋ฐ ๋ ์ง ๋๋กญ๋ค์ด ์
๋ฐ์ดํธ
|
|
|
1 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
import matplotlib.font_manager as fm
|
4 |
import matplotlib.pyplot as plt
|
|
|
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 |
+
subprocess.run(["apt-get", "update"], check=True)
|
28 |
+
subprocess.run(["apt-get", "install", "-y", "fonts-nanum"], check=True)
|
29 |
+
subprocess.run(["fc-cache", "-fv"], check=True)
|
30 |
+
except Exception as e:
|
31 |
+
print(f"ํฐํธ ์ค์น ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}")
|
32 |
+
|
33 |
+
install_nanum_font()
|
34 |
+
|
35 |
+
# ๋๋๊ณ ๋ ํฐํธ ๊ฒฝ๋ก ์ค์ ๋ฐ ๊ฐ์ ์ ์ฉ
|
36 |
+
font_path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf'
|
37 |
+
if os.path.exists(font_path):
|
38 |
+
fm.fontManager.addfont(font_path)
|
39 |
+
else:
|
40 |
+
print("ํฐํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
41 |
+
# ๋๋๊ณ ๋ ํฐํธ ๊ฐ์ ์ ์ฉ
|
42 |
+
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. Perplexity AI API ์ค์
|
47 |
+
API_KEY = "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"
|
48 |
+
|
49 |
+
# ๋ด์ค ์์ฝ์ ๊ฐ์ ธ์ค๋ ํจ์
|
50 |
+
def get_real_news_summary(company, date):
|
51 |
+
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
52 |
+
client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
|
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 selected_date == "" or selected_date is None:
|
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",
|
103 |
+
"Apple": "AAPL",
|
104 |
+
"Nvidia": "NVDA",
|
105 |
+
"Vertex": "VRTX",
|
106 |
+
"ํ๋์ฐจ": "005380.KS",
|
107 |
+
"์นด์นด์ค": "035720.KS",
|
108 |
+
"LGํํ": "051910.KS",
|
109 |
+
"์
ํธ๋ฆฌ์จ": "068270.KS",
|
110 |
+
"๋ค์ด๋ฒ": "035420.KS",
|
111 |
+
"์์ฝํ๋ก๋น์ ": "247540.KS",
|
112 |
+
"์ํ
์ค์ ": "196170.KQ",
|
113 |
+
# ์ต์ ์๊ฐ์ด์ก ์์ ์ข
๋ชฉ์ ๋ฐ์ํ์ฌ ์
๋ฐ์ดํธ
|
114 |
+
"๋์ค๋ฅ ์์ด 1์": "AAPL", # Apple
|
115 |
+
"๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์": "VRTX", # Vertex Pharmaceuticals
|
116 |
+
"๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์": "LLY", # Eli Lilly
|
117 |
+
"์ฝ์คํผ ์์ด 1์": "005930.KS", # ์ผ์ฑ์ ์
|
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") # ์ต๊ทผ 5๋
๋ฐ์ดํฐ๋ก ์ ํ
|
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 == "์์น":
|
137 |
+
highlight_data = stock_data[stock_data['Change'] >= percent_change]
|
138 |
+
color = "darkorange"
|
139 |
+
elif change_type == "ํ๋ฝ":
|
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.to_numpy()
|
146 |
+
closing_prices = stock_data['Close'].to_numpy()
|
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=10, fontweight='bold', color=color, ha='right')
|
154 |
+
plt.axvline(x=index, color=color, linestyle='--', linewidth=1) # x์ถ๊ณผ์ ์ฐ๊ฒฐ์ ์ ์ ์ผ๋ก ํ์
|
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)
|
160 |
+
plt.ylabel('์ข
๊ฐ', fontproperties=font_prop)
|
161 |
+
plt.legend()
|
162 |
+
|
163 |
+
buf = io.BytesIO()
|
164 |
+
plt.savefig(buf, format='png')
|
165 |
+
plt.close()
|
166 |
+
buf.seek(0)
|
167 |
+
img = Image.open(buf)
|
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 |
+
return f"Error processing data: {e}", gr.update(choices=[])
|
174 |
|
175 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ (3์ด ๋ ์ด์์)
|
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():
|
|
|
186 |
|
187 |
submit_btn = gr.Button("Submit")
|
188 |
|
189 |
+
# ์์
|
190 |
examples = [["SK๋ฐ์ด์คํ"],
|
191 |
["๋์ค๋ฅ ์์ด 1์"],
|
192 |
["๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์"],
|
|
|
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 |
# Submit ๋ฒํผ ํด๋ฆญ ์ ๊ทธ๋ํ ๋ฐ ๋ ์ง ๋๋กญ๋ค์ด ์
๋ฐ์ดํธ
|