Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,10 @@ import yfinance as yf
|
|
11 |
import numpy as np
|
12 |
import re # ํ๊ธ, ์ซ์, ๊ธฐํธ๋ฅผ ๋จ๊ธฐ๊ธฐ ์ํ ์ ๊ท ํํ์์ ์ฌ์ฉ
|
13 |
import gradio as gr
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# 1. ๋๋๊ณ ๋ ํฐํธ ์ค์น ๋ฐ ์ ์ฉ
|
16 |
def install_nanum_font():
|
@@ -36,31 +40,31 @@ font_prop = fm.FontProperties(fname=font_path)
|
|
36 |
plt.rcParams['font.family'] = font_prop.get_name()
|
37 |
plt.rcParams['axes.unicode_minus'] = False # ๋ง์ด๋์ค ๋ถํธ ๊นจ์ง ๋ฐฉ์ง
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
# Perplexity AI API ์ค์
|
66 |
API_KEY = "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" # ์ฌ๊ธฐ์ Perplexity AI API ํค๋ฅผ ์
๋ ฅํ์ธ์.
|
@@ -106,26 +110,37 @@ def update_news(input_value, selected_date):
|
|
106 |
return "๋ ์ง๋ฅผ ์ ํํด์ฃผ์ธ์."
|
107 |
else:
|
108 |
# ์ข
๋ชฉ๋ช
์ ๊ฐ์ ธ์์ Perplexity๋ก ๊ฒ์
|
109 |
-
ticker = name_to_ticker.get(input_value, input_value)
|
110 |
company_name = input_value if ticker == input_value else list(name_to_ticker.keys())[list(name_to_ticker.values()).index(ticker)]
|
111 |
return handle_click(company_name, selected_date)
|
112 |
|
113 |
-
# ์ข
๋ชฉ๋ช
๊ณผ ํฐ์ปค๋ฅผ
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
# ์ฃผ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์กฐ๊ฑด์ ๋ง๋ ๋ ์ง์ ๊ทธ๋ํ๋ฅผ ๋ฐํํ๋ ํจ์
|
125 |
def display_stock_with_highlight(input_value, change_type, percent_change):
|
126 |
try:
|
127 |
# ์
๋ ฅ๊ฐ์ ํฐ์ปค๋ก ๋ณํ
|
128 |
-
ticker =
|
129 |
stock = yf.Ticker(ticker)
|
130 |
stock_data = stock.history(period="5y") # ์ต๊ทผ 5๋
๋ฐ์ดํฐ๋ก ์ ํ
|
131 |
|
@@ -160,7 +175,7 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
|
|
160 |
plt.title(f'{input_value} ์ฃผ๊ฐ ์ถ์ด', fontproperties=font_prop)
|
161 |
plt.xlabel('๋ ์ง', fontproperties=font_prop)
|
162 |
plt.ylabel('์ข
๊ฐ', fontproperties=font_prop)
|
163 |
-
plt.legend()
|
164 |
|
165 |
buf = io.BytesIO()
|
166 |
plt.savefig(buf, format='png')
|
|
|
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():
|
|
|
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 ํค๋ฅผ ์
๋ ฅํ์ธ์.
|
|
|
110 |
return "๋ ์ง๋ฅผ ์ ํํด์ฃผ์ธ์."
|
111 |
else:
|
112 |
# ์ข
๋ชฉ๋ช
์ ๊ฐ์ ธ์์ Perplexity๋ก ๊ฒ์
|
113 |
+
ticker = name_to_ticker.get(input_value, input_value)
|
114 |
company_name = input_value if ticker == input_value else list(name_to_ticker.keys())[list(name_to_ticker.values()).index(ticker)]
|
115 |
return handle_click(company_name, selected_date)
|
116 |
|
117 |
+
# ์ข
๋ชฉ๋ช
๊ณผ ํฐ์ปค๋ฅผ ๋งคํํ๋ ๋์
๋๋ฆฌ ํ์ฅ ๋ฐ ์กฐ๊ฑด๋ณ ์ข
๋ชฉ ๋งคํ
|
118 |
+
name_to_ticker = {
|
119 |
+
"์ผ์ฑ์ ์": "005930.KS",
|
120 |
+
"SK๋ฐ์ด์คํ": "326030.KS",
|
121 |
+
"Apple": "AAPL",
|
122 |
+
"Nvidia": "NVDA",
|
123 |
+
"Vertex": "VRTX",
|
124 |
+
"ํ๋์ฐจ": "005380.KS",
|
125 |
+
"์นด์นด์ค": "035720.KS",
|
126 |
+
"LGํํ": "051910.KS",
|
127 |
+
"์
ํธ๋ฆฌ์จ": "068270.KS",
|
128 |
+
"๋ค์ด๋ฒ": "035420.KS",
|
129 |
+
"์์ฝํ๋ก๋น์ ": "247540.KS",
|
130 |
+
"์ํ
์ค์ ": "196170.KQ",
|
131 |
+
# ์ต์ ์๊ฐ์ด์ก ์์ ์ข
๋ชฉ์ ๋ฐ์ํ์ฌ ์
๋ฐ์ดํธ
|
132 |
+
"๋์ค๋ฅ ์์ด 1์": "AAPL", # Apple
|
133 |
+
"๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์": "VRTX", # Vertex Pharmaceuticals
|
134 |
+
"๋์ค๋ฅ ์ ์ฝ์ฃผ ์์ด 1์": "VRTX", # Vertex Pharmaceuticals
|
135 |
+
"์ฝ์คํผ ์์ด 1์": "005930.KS", # ์ผ์ฑ์ ์
|
136 |
+
"์ฝ์ค๋ฅ ์์ด 1์": "196170.KQ", # ์ํ
์ค์
|
137 |
+
}
|
138 |
|
139 |
# ์ฃผ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์กฐ๊ฑด์ ๋ง๋ ๋ ์ง์ ๊ทธ๋ํ๋ฅผ ๋ฐํํ๋ ํจ์
|
140 |
def display_stock_with_highlight(input_value, change_type, percent_change):
|
141 |
try:
|
142 |
# ์
๋ ฅ๊ฐ์ ํฐ์ปค๋ก ๋ณํ
|
143 |
+
ticker = name_to_ticker.get(input_value, input_value)
|
144 |
stock = yf.Ticker(ticker)
|
145 |
stock_data = stock.history(period="5y") # ์ต๊ทผ 5๋
๋ฐ์ดํฐ๋ก ์ ํ
|
146 |
|
|
|
175 |
plt.title(f'{input_value} ์ฃผ๊ฐ ์ถ์ด', fontproperties=font_prop)
|
176 |
plt.xlabel('๋ ์ง', fontproperties=font_prop)
|
177 |
plt.ylabel('์ข
๊ฐ', fontproperties=font_prop)
|
178 |
+
plt.legend(prop=font_prop)
|
179 |
|
180 |
buf = io.BytesIO()
|
181 |
plt.savefig(buf, format='png')
|