Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,71 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
import io
|
6 |
-
from PIL import Image
|
7 |
-
from datetime import datetime, timedelta
|
8 |
-
import gradio as gr
|
9 |
-
import yfinance as yf
|
10 |
-
from openai import OpenAI
|
11 |
|
12 |
-
# 1. ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ๋ฐ ์
๋ฐ์ดํธ
|
13 |
-
subprocess.run(["pip", "install", "--upgrade", "pip"])
|
14 |
-
subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
|
15 |
-
|
16 |
-
# 2. ๋๋๊ณ ๋ ํฐํธ ์ค์น ๋ฐ ์ ์ฉ
|
17 |
-
subprocess.run(["apt-get", "update"])
|
18 |
-
subprocess.run(["apt-get", "install", "-y", "fonts-nanum"])
|
19 |
-
subprocess.run(["fc-cache", "-fv"])
|
20 |
-
|
21 |
-
# 3. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ (ํฐํธ ์ค์น ํ)
|
22 |
-
import matplotlib.font_manager as fm
|
23 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# ๋๋๊ณ ๋ ํฐํธ ๊ฒฝ๋ก ์ค์ ๋ฐ ๊ฐ์ ์ ์ฉ
|
26 |
font_path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf'
|
27 |
|
28 |
if os.path.exists(font_path):
|
29 |
fm.fontManager.addfont(font_path)
|
30 |
-
font_prop = fm.FontProperties(fname=font_path)
|
31 |
-
plt.rcParams['font.family'] = font_prop.get_name()
|
32 |
-
plt.rcParams['axes.unicode_minus'] = False # ๋ง์ด๋์ค ๋ถํธ ๊นจ์ง ๋ฐฉ์ง
|
33 |
else:
|
34 |
print("ํฐํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Perplexity AI API ์ค์
|
37 |
-
API_KEY = "
|
38 |
|
39 |
def get_real_news_summary(company, date):
|
40 |
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
@@ -42,8 +73,8 @@ def get_real_news_summary(company, date):
|
|
42 |
|
43 |
# API ์์ฒญ์ ์ํ ๋ฉ์์ง ๊ตฌ์ฑ
|
44 |
messages = [
|
45 |
-
{"role": "system", "content": "
|
46 |
-
{"role": "user", "content": f"{
|
47 |
]
|
48 |
|
49 |
try:
|
@@ -99,7 +130,7 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
|
|
99 |
highlight_data = stock_data[stock_data['Change'] <= -percent_change]
|
100 |
color = "purple"
|
101 |
else:
|
102 |
-
return "
|
103 |
|
104 |
dates = stock_data.index.to_numpy()
|
105 |
closing_prices = stock_data['Close'].to_numpy()
|
@@ -109,14 +140,13 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
|
|
109 |
plt.scatter(highlight_data.index, highlight_data['Close'], color=color, label=f'{change_type} ํฌ์ธํธ')
|
110 |
|
111 |
for index, row in highlight_data.iterrows():
|
112 |
-
plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right'
|
113 |
-
plt.axvline(x=index, color=color, linestyle='--', linewidth=1)
|
114 |
|
115 |
-
|
116 |
-
plt.
|
117 |
-
plt.
|
118 |
-
plt.
|
119 |
-
plt.legend(prop=font_prop)
|
120 |
|
121 |
buf = io.BytesIO()
|
122 |
plt.savefig(buf, format='png')
|
@@ -128,7 +158,7 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
|
|
128 |
|
129 |
return img, gr.update(choices=highlight_dates)
|
130 |
except Exception as e:
|
131 |
-
return f"
|
132 |
|
133 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ (3์ด ๋ ์ด์์)
|
134 |
with gr.Blocks() as demo:
|
@@ -142,12 +172,13 @@ with gr.Blocks() as demo:
|
|
142 |
|
143 |
submit_btn = gr.Button("Submit")
|
144 |
|
145 |
-
# ์์
|
146 |
examples = [["SK๋ฐ์ด์คํ"],
|
147 |
-
["
|
148 |
-
["
|
149 |
-
["
|
150 |
-
["
|
|
|
151 |
gr.Examples(examples=examples, inputs=[input_value])
|
152 |
|
153 |
with gr.Column(): # ๊ทธ๋ํ๋ฅผ ์ถ๋ ฅํ ๋ ๋ฒ์งธ ์ด
|
|
|
1 |
+
# A ์ฝ๋
|
2 |
+
# 1. ๋๋๊ณ ๋ ํฐํธ ์ค์น ๋ฐ ์ ์ฉ
|
3 |
+
!apt-get install -y fonts-nanum
|
4 |
+
!fc-cache -fv
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import matplotlib.pyplot as plt
|
7 |
+
import matplotlib.font_manager as fm
|
8 |
+
import yfinance as yf
|
9 |
+
import numpy as np
|
10 |
+
import os
|
11 |
|
12 |
# ๋๋๊ณ ๋ ํฐํธ ๊ฒฝ๋ก ์ค์ ๋ฐ ๊ฐ์ ์ ์ฉ
|
13 |
font_path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf'
|
14 |
|
15 |
if os.path.exists(font_path):
|
16 |
fm.fontManager.addfont(font_path)
|
|
|
|
|
|
|
17 |
else:
|
18 |
print("ํฐํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
19 |
|
20 |
+
# ๋๋๊ณ ๋ ํฐํธ ๊ฐ์ ์ ์ฉ
|
21 |
+
font_prop = fm.FontProperties(fname=font_path)
|
22 |
+
plt.rcParams['font.family'] = font_prop.get_name()
|
23 |
+
plt.rcParams['axes.unicode_minus'] = False # ๋ง์ด๋์ค ๋ถํธ ๊นจ์ง ๋ฐฉ์ง
|
24 |
+
|
25 |
+
# 2. yfinance๋ก ์ฃผ๊ฐ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ (์ผ์ฑ์ ์ ์์)
|
26 |
+
ticker = '005930.KS' # ์ผ์ฑ์ ์ ํฐ์ปค
|
27 |
+
stock = yf.Ticker(ticker)
|
28 |
+
stock_data = stock.history(period="max")
|
29 |
+
|
30 |
+
# ์ฃผ๊ฐ ๋ฐ์ดํฐ ์ถ๋ ฅ ํ์ธ
|
31 |
+
print(stock_data.head()) # ์ฃผ๊ฐ ๋ฐ์ดํฐ ์ผ๋ถ ํ์ธ
|
32 |
+
|
33 |
+
# 3. ์ฃผ๊ฐ ๋ฐ์ดํฐ ์๊ฐํ (pandas ๋ฐ์ดํฐํ๋ ์์ numpy ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ์ฒ๋ฆฌ)
|
34 |
+
plt.figure(figsize=(10, 6))
|
35 |
+
|
36 |
+
# ์ธ๋ฑ์ค์ ์ข
๊ฐ ๋ฐ์ดํฐ๋ฅผ numpy ๋ฐฐ์ด๋ก ๋ณํ
|
37 |
+
dates = stock_data.index.to_numpy()
|
38 |
+
closing_prices = stock_data['Close'].to_numpy()
|
39 |
+
|
40 |
+
# ์ฃผ๊ฐ ๋ฐ์ดํฐ ์๊ฐํ
|
41 |
+
plt.plot(dates, closing_prices, label='์ผ์ฑ์ ์ ์ข
๊ฐ')
|
42 |
+
|
43 |
+
# ๊ทธ๋ํ ์ ๋ชฉ, ์ถ ๋ผ๋ฒจ์ ํ๊ธ๋ก ์ค์
|
44 |
+
plt.title('์ผ์ฑ์ ์ ์ฃผ๊ฐ ์ถ์ด', fontproperties=font_prop)
|
45 |
+
plt.xlabel('๋ ์ง', fontproperties=font_prop)
|
46 |
+
plt.ylabel('์ข
๊ฐ', fontproperties=font_prop)
|
47 |
+
plt.legend(prop=font_prop)
|
48 |
+
|
49 |
+
# B ์ฝ๋
|
50 |
+
import subprocess
|
51 |
+
|
52 |
+
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ๋ฐ ์
๋ฐ์ดํธ
|
53 |
+
subprocess.run(["pip", "install", "--upgrade", "pip"])
|
54 |
+
subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
|
55 |
+
|
56 |
+
# ์ดํ ์ฝ๋ ์คํ
|
57 |
+
import requests
|
58 |
+
import gradio as gr
|
59 |
+
import yfinance as yf
|
60 |
+
import matplotlib.pyplot as plt
|
61 |
+
import io
|
62 |
+
from PIL import Image
|
63 |
+
from datetime import datetime, timedelta
|
64 |
+
from openai import OpenAI
|
65 |
+
import os
|
66 |
+
|
67 |
# Perplexity AI API ์ค์
|
68 |
+
API_KEY = "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" # ์ฌ๊ธฐ์ Perplexity AI API ํค๋ฅผ ์
๋ ฅํ์ธ์.
|
69 |
|
70 |
def get_real_news_summary(company, date):
|
71 |
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
|
|
73 |
|
74 |
# API ์์ฒญ์ ์ํ ๋ฉ์์ง ๊ตฌ์ฑ
|
75 |
messages = [
|
76 |
+
{"role": "system", "content": "You are a helpful assistant that summarizes stock news in Korean."},
|
77 |
+
{"role": "user", "content": f"Summarize the latest stock news for {company} on {date} in Korean. If there's no specific news for that date, provide the most recent relevant information."}
|
78 |
]
|
79 |
|
80 |
try:
|
|
|
130 |
highlight_data = stock_data[stock_data['Change'] <= -percent_change]
|
131 |
color = "purple"
|
132 |
else:
|
133 |
+
return "Invalid change type", []
|
134 |
|
135 |
dates = stock_data.index.to_numpy()
|
136 |
closing_prices = stock_data['Close'].to_numpy()
|
|
|
140 |
plt.scatter(highlight_data.index, highlight_data['Close'], color=color, label=f'{change_type} ํฌ์ธํธ')
|
141 |
|
142 |
for index, row in highlight_data.iterrows():
|
143 |
+
plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
|
144 |
+
plt.axvline(x=index, color=color, linestyle='--', linewidth=1) # x์ถ๊ณผ์ ์ฐ๊ฒฐ์ ์ ์ ์ผ๋ก ํ์
|
145 |
|
146 |
+
plt.title(f'{input_value} ์ฃผ๊ฐ ์ถ์ด ({change_type} ํ์)')
|
147 |
+
plt.xlabel('๋ ์ง')
|
148 |
+
plt.ylabel('์ข
๊ฐ')
|
149 |
+
plt.legend()
|
|
|
150 |
|
151 |
buf = io.BytesIO()
|
152 |
plt.savefig(buf, format='png')
|
|
|
158 |
|
159 |
return img, gr.update(choices=highlight_dates)
|
160 |
except Exception as e:
|
161 |
+
return f"Error processing data: {e}", gr.update(choices=[])
|
162 |
|
163 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ (3์ด ๋ ์ด์์)
|
164 |
with gr.Blocks() as demo:
|
|
|
172 |
|
173 |
submit_btn = gr.Button("Submit")
|
174 |
|
175 |
+
# ์์ (์ด์ ๋ ์ด์์์ผ๋ก ๋ณต์)
|
176 |
examples = [["SK๋ฐ์ด์คํ"],
|
177 |
+
["๋์ค๋ฅ ์์ด 1์"],
|
178 |
+
["๋์ค๋ฅ ์ ์ฝ์ฃผ ์์ด 1์"],
|
179 |
+
["๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์"],
|
180 |
+
["์ฝ์คํผ ์์ด 1์"],
|
181 |
+
["์ฝ์ค๋ฅ ์์ด 1์"]]
|
182 |
gr.Examples(examples=examples, inputs=[input_value])
|
183 |
|
184 |
with gr.Column(): # ๊ทธ๋ํ๋ฅผ ์ถ๋ ฅํ ๋ ๋ฒ์งธ ์ด
|