Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,165 @@
|
|
1 |
-
|
2 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import matplotlib.font_manager as fm
|
4 |
import matplotlib.pyplot as plt
|
|
|
|
|
5 |
import gradio as gr
|
6 |
import io
|
7 |
from PIL import Image
|
8 |
from datetime import datetime, timedelta
|
9 |
-
import
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Perplexity AI API ์ค์
|
16 |
API_KEY = "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" # ์ฌ๊ธฐ์ Perplexity AI API ํค๋ฅผ ์
๋ ฅํ์ธ์.
|
17 |
|
18 |
-
#
|
19 |
-
def
|
20 |
-
#
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
largest_market_cap = 0
|
23 |
-
|
24 |
|
25 |
-
for ticker in
|
26 |
stock = yf.Ticker(ticker)
|
27 |
-
|
28 |
-
market_cap =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
if market_cap > largest_market_cap:
|
31 |
largest_market_cap = market_cap
|
32 |
-
|
33 |
-
|
34 |
-
return leader_ticker
|
35 |
|
36 |
-
|
37 |
-
name_to_ticker = {
|
38 |
-
"๋์ค๋ฅ ์์ด 1์": "AAPL", # Apple
|
39 |
-
"๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์": "VRTX", # Vertex Pharmaceuticals
|
40 |
-
"๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์": get_nasdaq_healthcare_leader(), # yfinance๋ก ๋์ ์ผ๋ก ์ค์
|
41 |
-
"์ฝ์คํผ ์์ด 1์": "005930.KS", # ์ผ์ฑ์ ์
|
42 |
-
"์ฝ์ค๋ฅ ์์ด 1์": "196170.KQ", # ์ํ
์ค์
|
43 |
-
}
|
44 |
|
45 |
# ์ฃผ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์กฐ๊ฑด์ ๋ง๋ ๋ ์ง์ ๊ทธ๋ํ๋ฅผ ๋ฐํํ๋ ํจ์
|
46 |
def display_stock_with_highlight(input_value, change_type, percent_change):
|
47 |
try:
|
48 |
# ์
๋ ฅ๊ฐ์ ํฐ์ปค๋ก ๋ณํ
|
49 |
-
ticker =
|
50 |
stock = yf.Ticker(ticker)
|
51 |
stock_data = stock.history(period="5y") # ์ต๊ทผ 5๋
๋ฐ์ดํฐ๋ก ์ ํ
|
52 |
|
@@ -74,11 +187,13 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
|
|
74 |
|
75 |
for index, row in highlight_data.iterrows():
|
76 |
plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
|
77 |
-
plt.axvline(x=index, color=color, linestyle='--', linewidth=1)
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
plt.
|
|
|
|
|
82 |
plt.legend()
|
83 |
|
84 |
buf = io.BytesIO()
|
@@ -105,11 +220,11 @@ with gr.Blocks() as demo:
|
|
105 |
|
106 |
submit_btn = gr.Button("Submit")
|
107 |
|
108 |
-
# ์์ (
|
109 |
examples = [["SK๋ฐ์ด์คํ"],
|
110 |
["๋์ค๋ฅ ์์ด 1์"],
|
111 |
["๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์"],
|
112 |
-
["๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์"],
|
113 |
["์ฝ์คํผ ์์ด 1์"],
|
114 |
["์ฝ์ค๋ฅ ์์ด 1์"]]
|
115 |
gr.Examples(examples=examples, inputs=[input_value])
|
|
|
1 |
+
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ๋ฐ ์
๋ฐ์ดํธ๋ฅผ ์ต์๋จ์ ๋ฐฐ์น
|
2 |
import subprocess
|
3 |
+
|
4 |
+
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ๋ฐ ์
๋ฐ์ดํธ
|
5 |
+
subprocess.run(["pip", "install", "--upgrade", "pip"])
|
6 |
+
subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
|
7 |
+
|
8 |
+
# ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ
|
9 |
+
import yfinance as yf
|
10 |
+
import os
|
11 |
import matplotlib.font_manager as fm
|
12 |
import matplotlib.pyplot as plt
|
13 |
+
import numpy as np
|
14 |
+
import re # ํ๊ธ, ์ซ์, ๊ธฐํธ๋ฅผ ๋จ๊ธฐ๊ธฐ ์ํ ์ ๊ท ํํ์์ ์ฌ์ฉ
|
15 |
import gradio as gr
|
16 |
import io
|
17 |
from PIL import Image
|
18 |
from datetime import datetime, timedelta
|
19 |
+
from openai import OpenAI
|
20 |
|
21 |
+
# 1. ๋๋๊ณ ๋ ํฐํธ ์ค์น ๋ฐ ์ ์ฉ
|
22 |
+
def install_nanum_font():
|
23 |
+
try:
|
24 |
+
subprocess.run(["apt-get", "update"], check=True)
|
25 |
+
subprocess.run(["apt-get", "install", "-y", "fonts-nanum"], check=True)
|
26 |
+
subprocess.run(["fc-cache", "-fv"], check=True)
|
27 |
+
except Exception as e:
|
28 |
+
print(f"ํฐํธ ์ค์น ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}")
|
29 |
+
|
30 |
+
install_nanum_font()
|
31 |
+
|
32 |
+
# ๋๋๊ณ ๋ ํฐํธ ๊ฒฝ๋ก ์ค์ ๋ฐ ๊ฐ์ ์ ์ฉ
|
33 |
+
font_path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf'
|
34 |
+
|
35 |
+
if os.path.exists(font_path):
|
36 |
+
fm.fontManager.addfont(font_path)
|
37 |
+
else:
|
38 |
+
print("ํฐํธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
39 |
+
|
40 |
+
# ๋๋๊ณ ๋ ํฐํธ ๊ฐ์ ์ ์ฉ
|
41 |
+
font_prop = fm.FontProperties(fname=font_path)
|
42 |
+
plt.rcParams['font.family'] = font_prop.get_name()
|
43 |
+
plt.rcParams['axes.unicode_minus'] = False # ๋ง์ด๋์ค ๋ถํธ ๊นจ์ง ๋ฐฉ์ง
|
44 |
|
45 |
# Perplexity AI API ์ค์
|
46 |
API_KEY = "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" # ์ฌ๊ธฐ์ Perplexity AI API ํค๋ฅผ ์
๋ ฅํ์ธ์.
|
47 |
|
48 |
+
# ๋ด์ค ์์ฝ์ ๊ฐ์ ธ์ค๋ ํจ์ (๊ธฐ์กด ์ฝ๋ ์ ์ง)
|
49 |
+
def get_real_news_summary(company, date):
|
50 |
+
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
51 |
+
client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
|
52 |
+
|
53 |
+
# ๋ ์ง ํ์์ ๋ง์ถฐ์ฃผ๊ธฐ ์ํ ์ฒ๋ฆฌ
|
54 |
+
target_date = datetime.strptime(date, '%Y-%m-%d')
|
55 |
+
start_date = (target_date - timedelta(days=1)).strftime('%Y-%m-%d')
|
56 |
+
end_date = (target_date + timedelta(days=1)).strftime('%Y-%m-%d')
|
57 |
+
|
58 |
+
# API ์์ฒญ์ ์ํ ๋ฉ์์ง ๊ตฌ์ฑ - ํ๊ตญ์ด๋ก๋ง ์๋ต์ ๋ฐ๋๋ก ์ง์
|
59 |
+
messages = [
|
60 |
+
{"role": "system", "content": "You are a helpful assistant that summarizes stock news strictly in Korean."},
|
61 |
+
{"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."}
|
62 |
+
]
|
63 |
+
|
64 |
+
try:
|
65 |
+
# API ์์ฒญ
|
66 |
+
response = client.chat.completions.create(
|
67 |
+
model="llama-3.1-sonar-large-128k-online",
|
68 |
+
messages=messages
|
69 |
+
)
|
70 |
+
|
71 |
+
# ์๋ต์์ ์์ฝ ์ถ์ถ
|
72 |
+
summary = response.choices[0].message.content
|
73 |
+
|
74 |
+
# ํ๊ธ, ์ซ์, ๊ณต๋ฐฑ, ํน์ ๊ธฐํธ๋ง ๋จ๊ธฐ๋ ์ ๊ท ํํ์
|
75 |
+
korean_only_summary = re.sub(r'[^\w\s#.,!%()\-\[\]]', '', summary)
|
76 |
+
|
77 |
+
# ##๋ก ์์ํ๋ ๋ถ๋ถ์ **์ผ๋ก ๊ฐ์ธ์ Bold ์ฒ๋ฆฌ
|
78 |
+
formatted_summary = re.sub(r'##\s*(.+)', r'**\1**', korean_only_summary)
|
79 |
+
|
80 |
+
return formatted_summary
|
81 |
+
except Exception as e:
|
82 |
+
return f"๋ด์ค ์์ฝ ์ค ์๋ฌ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
83 |
+
|
84 |
+
# ๋ด์ค ์์ฝ์ ๊ฐ์ ธ์ค๋ ํจ์
|
85 |
+
def handle_click(company_name, date_clicked):
|
86 |
+
return get_real_news_summary(company_name, date_clicked)
|
87 |
+
|
88 |
+
# Gradio์์ ์ฌ์ฉํ ํจ์ (๋ด์ค ์์ฝ ํฌํจ)
|
89 |
+
def update_news(input_value, selected_date):
|
90 |
+
if selected_date == "" or selected_date is None:
|
91 |
+
return "๋ ์ง๋ฅผ ์ ํํด์ฃผ์ธ์."
|
92 |
+
else:
|
93 |
+
# ์ข
๋ชฉ๋ช
์ ๊ฐ์ ธ์์ Perplexity๋ก ๊ฒ์
|
94 |
+
ticker = get_dynamic_ticker(input_value)
|
95 |
+
company_name = input_value if ticker == input_value else ticker_to_name.get(ticker, input_value)
|
96 |
+
return handle_click(company_name, selected_date)
|
97 |
+
|
98 |
+
# ์ข
๋ชฉ๋ช
๊ณผ ํฐ์ปค๋ฅผ ๋งคํํ๋ ๋์
๋๋ฆฌ
|
99 |
+
ticker_to_name = {}
|
100 |
+
name_to_ticker = {}
|
101 |
+
|
102 |
+
# ๋ชจ๋ ์์ ๋ฅผ ๋์ ์ผ๋ก ๋งค์นญํ๊ธฐ ์ํ ํจ์
|
103 |
+
def get_dynamic_ticker(input_value):
|
104 |
+
if input_value == "๋์ค๋ฅ ์์ด 1์":
|
105 |
+
return get_top_market_cap_stock("๋์ค๋ฅ")
|
106 |
+
elif input_value == "๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์":
|
107 |
+
return get_top_market_cap_stock("๋์ค๋ฅ", industry="Biotechnology")
|
108 |
+
elif input_value == "๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์":
|
109 |
+
return get_top_market_cap_stock("๋์ค๋ฅ", sector="Healthcare")
|
110 |
+
elif input_value == "์ฝ์คํผ ๏ฟฝ๏ฟฝ์ด 1์":
|
111 |
+
return get_top_market_cap_stock("์ฝ์คํผ")
|
112 |
+
elif input_value == "์ฝ์ค๋ฅ ์์ด 1์":
|
113 |
+
return get_top_market_cap_stock("์ฝ์ค๋ฅ")
|
114 |
+
else:
|
115 |
+
# ์ฌ์ ์ ๋ฑ๋ก๋ ์ข
๋ชฉ๋ช
๋ฐํ
|
116 |
+
return name_to_ticker.get(input_value, input_value)
|
117 |
+
|
118 |
+
# ์๊ฐ์ด์ก ์์ ์ข
๋ชฉ์ ๊ฐ์ ธ์ค๋ ํจ์
|
119 |
+
def get_top_market_cap_stock(market, sector=None, industry=None):
|
120 |
+
# yfinance๋ ์ข
๋ชฉ ๋ชฉ๋ก์ ์ ๊ณตํ์ง ์์ผ๋ฏ๋ก, ETF๋ฅผ ํ์ฉ
|
121 |
+
if market == "๋์ค๋ฅ":
|
122 |
+
etf_ticker = "QQQ" # ๋์ค๋ฅ 100 ETF
|
123 |
+
elif market == "์ฝ์คํผ":
|
124 |
+
etf_ticker = "EWY" # ํ๊ตญ ETF
|
125 |
+
elif market == "์ฝ์ค๋ฅ":
|
126 |
+
# ์ฝ์ค๋ฅ ์ข
๋ชฉ์ ์ ๊ทผํ๊ธฐ ์ด๋ ค์ฐ๋ฏ๋ก ์ฌ์ ์ ์๋ ๋ฆฌ์คํธ ์ฌ์ฉ
|
127 |
+
tickers = ["035420.KQ", "068270.KQ", "035720.KQ"]
|
128 |
+
else:
|
129 |
+
return None
|
130 |
+
|
131 |
+
if market in ["๋์ค๋ฅ", "์ฝ์คํผ"]:
|
132 |
+
etf = yf.Ticker(etf_ticker)
|
133 |
+
holdings = etf.info.get('holdings', [])
|
134 |
+
tickers = [holding['symbol'] for holding in holdings]
|
135 |
+
|
136 |
largest_market_cap = 0
|
137 |
+
top_ticker = None
|
138 |
|
139 |
+
for ticker in tickers:
|
140 |
stock = yf.Ticker(ticker)
|
141 |
+
stock_info = stock.info
|
142 |
+
market_cap = stock_info.get('marketCap', 0)
|
143 |
+
stock_sector = stock_info.get('sector', None)
|
144 |
+
stock_industry = stock_info.get('industry', None)
|
145 |
+
|
146 |
+
if sector and stock_sector != sector:
|
147 |
+
continue
|
148 |
+
if industry and stock_industry != industry:
|
149 |
+
continue
|
150 |
|
151 |
if market_cap > largest_market_cap:
|
152 |
largest_market_cap = market_cap
|
153 |
+
top_ticker = ticker
|
154 |
+
ticker_to_name[ticker] = stock_info.get('shortName', ticker)
|
|
|
155 |
|
156 |
+
return top_ticker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
# ์ฃผ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์กฐ๊ฑด์ ๋ง๋ ๋ ์ง์ ๊ทธ๋ํ๋ฅผ ๋ฐํํ๋ ํจ์
|
159 |
def display_stock_with_highlight(input_value, change_type, percent_change):
|
160 |
try:
|
161 |
# ์
๋ ฅ๊ฐ์ ํฐ์ปค๋ก ๋ณํ
|
162 |
+
ticker = get_dynamic_ticker(input_value)
|
163 |
stock = yf.Ticker(ticker)
|
164 |
stock_data = stock.history(period="5y") # ์ต๊ทผ 5๋
๋ฐ์ดํฐ๋ก ์ ํ
|
165 |
|
|
|
187 |
|
188 |
for index, row in highlight_data.iterrows():
|
189 |
plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
|
190 |
+
plt.axvline(x=index, color=color, linestyle='--', linewidth=1)
|
191 |
|
192 |
+
# ์ข
๋ชฉ๋ช
+ '์ฃผ๊ฐ ์ถ์ด'๋ก ์ ๋ชฉ ์ค์ (์ข
๋ชฉ๋ช
๊ธฐ๋ฐ)
|
193 |
+
company_name = ticker_to_name.get(ticker, input_value)
|
194 |
+
plt.title(f'{company_name} ์ฃผ๊ฐ ์ถ์ด', fontproperties=font_prop)
|
195 |
+
plt.xlabel('๋ ์ง', fontproperties=font_prop)
|
196 |
+
plt.ylabel('์ข
๊ฐ', fontproperties=font_prop)
|
197 |
plt.legend()
|
198 |
|
199 |
buf = io.BytesIO()
|
|
|
220 |
|
221 |
submit_btn = gr.Button("Submit")
|
222 |
|
223 |
+
# ์์ (๋์ ์ผ๋ก ๋งค์นญ)
|
224 |
examples = [["SK๋ฐ์ด์คํ"],
|
225 |
["๋์ค๋ฅ ์์ด 1์"],
|
226 |
["๋์ค๋ฅ ๋ฐ์ด์คํ
์์ด 1์"],
|
227 |
+
["๋์ค๋ฅ ํฌ์ค์ผ์ด ์์ด 1์"],
|
228 |
["์ฝ์คํผ ์์ด 1์"],
|
229 |
["์ฝ์ค๋ฅ ์์ด 1์"]]
|
230 |
gr.Examples(examples=examples, inputs=[input_value])
|