Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,40 @@
|
|
1 |
import subprocess
|
2 |
-
import sys
|
3 |
import os
|
4 |
import matplotlib.font_manager as fm
|
5 |
import matplotlib.pyplot as plt
|
6 |
import io
|
7 |
from PIL import Image
|
8 |
-
from datetime import datetime
|
9 |
import gradio as gr
|
10 |
import yfinance as yf
|
11 |
from openai import OpenAI
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
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 |
-
|
34 |
-
|
35 |
-
download_and_setup_font()
|
36 |
|
37 |
# Perplexity AI API ์ค์
|
38 |
-
API_KEY = "
|
39 |
|
40 |
def get_real_news_summary(company, date):
|
41 |
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
@@ -43,8 +42,8 @@ def get_real_news_summary(company, date):
|
|
43 |
|
44 |
# API ์์ฒญ์ ์ํ ๋ฉ์์ง ๊ตฌ์ฑ
|
45 |
messages = [
|
46 |
-
{"role": "system", "content": "
|
47 |
-
{"role": "user", "content": f"
|
48 |
]
|
49 |
|
50 |
try:
|
@@ -100,7 +99,7 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
|
|
100 |
highlight_data = stock_data[stock_data['Change'] <= -percent_change]
|
101 |
color = "purple"
|
102 |
else:
|
103 |
-
return "
|
104 |
|
105 |
dates = stock_data.index.to_numpy()
|
106 |
closing_prices = stock_data['Close'].to_numpy()
|
@@ -110,13 +109,14 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
|
|
110 |
plt.scatter(highlight_data.index, highlight_data['Close'], color=color, label=f'{change_type} ํฌ์ธํธ')
|
111 |
|
112 |
for index, row in highlight_data.iterrows():
|
113 |
-
plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
|
114 |
-
plt.axvline(x=index, color=color, linestyle='--', linewidth=1)
|
115 |
|
116 |
-
|
117 |
-
plt.
|
118 |
-
plt.
|
119 |
-
plt.
|
|
|
120 |
|
121 |
buf = io.BytesIO()
|
122 |
plt.savefig(buf, format='png')
|
@@ -128,7 +128,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:
|
|
|
1 |
import subprocess
|
|
|
2 |
import os
|
3 |
import matplotlib.font_manager as fm
|
4 |
import matplotlib.pyplot as plt
|
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 = "์ฌ๊ธฐ์_์ค์ _API_ํค๋ฅผ_์
๋ ฅํ์ธ์" # ์ฌ๊ธฐ์ Perplexity AI API ํค๋ฅผ ์
๋ ฅํ์ธ์.
|
38 |
|
39 |
def get_real_news_summary(company, date):
|
40 |
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
|
|
42 |
|
43 |
# API ์์ฒญ์ ์ํ ๋ฉ์์ง ๊ตฌ์ฑ
|
44 |
messages = [
|
45 |
+
{"role": "system", "content": "๋น์ ์ ํ๊ตญ์ด๋ก ์ฃผ์ ๋ด์ค๋ฅผ ์์ฝํด์ฃผ๋ ๋์์ด ๋๋ ์ด์์คํดํธ์
๋๋ค."},
|
46 |
+
{"role": "user", "content": f"{date}์ ๋ํ {company}์ ์ต์ ์ฃผ์ ๋ด์ค๋ฅผ ํ๊ตญ์ด๋ก ์์ฝํด์ค. ํด๋น ๋ ์ง์ ํน์ ๋ด์ค๊ฐ ์๋ค๋ฉด, ๊ฐ์ฅ ์ต๊ทผ์ ๊ด๋ จ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ค."}
|
47 |
]
|
48 |
|
49 |
try:
|
|
|
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 |
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', fontproperties=font_prop)
|
113 |
+
plt.axvline(x=index, color=color, linestyle='--', linewidth=1)
|
114 |
|
115 |
+
# ๊ทธ๋ํ ์ ๋ชฉ, ์ถ ๋ผ๋ฒจ์ ํ๊ธ๋ก ์ค์
|
116 |
+
plt.title(f'{input_value} ์ฃผ๊ฐ ์ถ์ด ({change_type} ํ์)', fontproperties=font_prop)
|
117 |
+
plt.xlabel('๋ ์ง', fontproperties=font_prop)
|
118 |
+
plt.ylabel('์ข
๊ฐ', fontproperties=font_prop)
|
119 |
+
plt.legend(prop=font_prop)
|
120 |
|
121 |
buf = io.BytesIO()
|
122 |
plt.savefig(buf, format='png')
|
|
|
128 |
|
129 |
return img, gr.update(choices=highlight_dates)
|
130 |
except Exception as e:
|
131 |
+
return f"๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ค ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}", gr.update(choices=[])
|
132 |
|
133 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ (3์ด ๋ ์ด์์)
|
134 |
with gr.Blocks() as demo:
|