File size: 9,860 Bytes
c05ccec
 
 
 
 
 
56c0ae0
 
 
 
 
c05ccec
56c0ae0
 
 
 
c05ccec
56c0ae0
0170c23
7628d13
 
c05ccec
 
 
7628d13
 
 
 
 
 
 
c05ccec
7628d13
 
 
 
c05ccec
7628d13
 
 
 
 
c05ccec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7628d13
c05ccec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7628d13
c05ccec
 
 
 
 
7628d13
 
 
c05ccec
7628d13
c05ccec
7628d13
 
c05ccec
7628d13
 
 
 
c05ccec
7628d13
 
 
 
 
 
08f8b7f
7628d13
 
 
 
 
 
 
c05ccec
7628d13
 
08f8b7f
7628d13
 
 
 
c05ccec
7628d13
 
c05ccec
7628d13
 
c05ccec
7628d13
 
c05ccec
7628d13
 
c05ccec
7628d13
 
 
 
 
 
 
 
 
c05ccec
7628d13
c05ccec
 
7628d13
 
 
 
 
 
c05ccec
 
7628d13
c05ccec
7628d13
 
 
 
 
 
 
 
 
 
 
 
 
 
c05ccec
7628d13
c05ccec
56c0ae0
c05ccec
97a16e9
467f517
97a16e9
56c0ae0
c05ccec
56c0ae0
 
 
c05ccec
56c0ae0
 
08f8b7f
56c0ae0
 
08f8b7f
56c0ae0
 
 
 
 
c05ccec
56c0ae0
 
 
c05ccec
 
 
56c0ae0
c05ccec
56c0ae0
 
 
 
 
 
c05ccec
56c0ae0
 
 
 
 
 
c05ccec
56c0ae0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import subprocess

# ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ๋ฐ ์—…๋ฐ์ดํŠธ
subprocess.run(["pip", "install", "--upgrade", "pip"])
subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])

import os
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
import yfinance as yf
import numpy as np
import re  # ํ•œ๊ธ€, ์ˆซ์ž, ๊ธฐํ˜ธ๋ฅผ ๋‚จ๊ธฐ๊ธฐ ์œ„ํ•œ ์ •๊ทœ ํ‘œํ˜„์‹์— ์‚ฌ์šฉ
import gradio as gr
import io
from PIL import Image
from datetime import datetime, timedelta
from openai import OpenAI

# 1. ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ์„ค์น˜ ๋ฐ ์ ์šฉ
def install_nanum_font():
    try:
        subprocess.run(["apt-get", "update"], check=True)
        subprocess.run(["apt-get", "install", "-y", "fonts-nanum"], check=True)
        subprocess.run(["fc-cache", "-fv"], check=True)
    except Exception as e:
        print(f"ํฐํŠธ ์„ค์น˜ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {e}")

install_nanum_font()

# ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ๊ฒฝ๋กœ ์„ค์ • ๋ฐ ๊ฐ•์ œ ์ ์šฉ
font_path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf'

if os.path.exists(font_path):
    fm.fontManager.addfont(font_path)
else:
    print("ํฐํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")

# ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ๊ฐ•์ œ ์ ์šฉ
font_prop = fm.FontProperties(fname=font_path)
plt.rcParams['font.family'] = font_prop.get_name()
plt.rcParams['axes.unicode_minus'] = False  # ๋งˆ์ด๋„ˆ์Šค ๋ถ€ํ˜ธ ๊นจ์ง ๋ฐฉ์ง€

# 2. yfinance๋กœ ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ (์‚ผ์„ฑ์ „์ž ์˜ˆ์‹œ)
ticker = '005930.KS'  # ์‚ผ์„ฑ์ „์ž ํ‹ฐ์ปค
stock = yf.Ticker(ticker)
stock_data = stock.history(period="max")

# ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ ํ™•์ธ
print(stock_data.head())  # ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์ผ๋ถ€ ํ™•์ธ

# 3. ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” (pandas ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ numpy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ฒ˜๋ฆฌ)
plt.figure(figsize=(10, 6))

# ์ธ๋ฑ์Šค์™€ ์ข…๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ numpy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
dates = stock_data.index.to_numpy()
closing_prices = stock_data['Close'].to_numpy()

# ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”
plt.plot(dates, closing_prices, label='์‚ผ์„ฑ์ „์ž ์ข…๊ฐ€')

# ๊ทธ๋ž˜ํ”„ ์ œ๋ชฉ, ์ถ• ๋ผ๋ฒจ์„ ํ•œ๊ธ€๋กœ ์„ค์ •
plt.title('์‚ผ์„ฑ์ „์ž ์ฃผ๊ฐ€ ์ถ”์ด', fontproperties=font_prop)
plt.xlabel('๋‚ ์งœ', fontproperties=font_prop)
plt.ylabel('์ข…๊ฐ€', fontproperties=font_prop)
plt.legend(prop=font_prop)

# B ์ฝ”๋“œ

# Perplexity AI API ์„ค์ •
API_KEY = "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"  # ์—ฌ๊ธฐ์— Perplexity AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.

# ๋‰ด์Šค ์š”์•ฝ์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜
def get_real_news_summary(company, date):
    # OpenAI ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
    client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
    
    # ๋‚ ์งœ ํ˜•์‹์„ ๋งž์ถฐ์ฃผ๊ธฐ ์œ„ํ•œ ์ฒ˜๋ฆฌ
    target_date = datetime.strptime(date, '%Y-%m-%d')
    start_date = (target_date - timedelta(days=1)).strftime('%Y-%m-%d')
    end_date = (target_date + timedelta(days=1)).strftime('%Y-%m-%d')

    # API ์š”์ฒญ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€ ๊ตฌ์„ฑ - ํ•œ๊ตญ์–ด๋กœ๋งŒ ์‘๋‹ต์„ ๋ฐ›๋„๋ก ์ง€์‹œ
    messages = [
        {"role": "system", "content": "You are a helpful assistant that summarizes stock news strictly in Korean."},
        {"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."}
    ]

    try:
        # API ์š”์ฒญ
        response = client.chat.completions.create(
            model="llama-3.1-sonar-large-128k-online",
            messages=messages
        )

        # ์‘๋‹ต์—์„œ ์š”์•ฝ ์ถ”์ถœ
        summary = response.choices[0].message.content

        # ํ•œ๊ธ€, ์ˆซ์ž, ๊ณต๋ฐฑ, ํŠน์ˆ˜ ๊ธฐํ˜ธ๋งŒ ๋‚จ๊ธฐ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹
        korean_only_summary = re.sub(r'[^\w\s#.,!%()\-\[\]]', '', summary)

        # ##๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ถ€๋ถ„์„ **์œผ๋กœ ๊ฐ์‹ธ์„œ Bold ์ฒ˜๋ฆฌ
        formatted_summary = re.sub(r'##\s*(.+)', r'**\1**', korean_only_summary)

        return formatted_summary
    except Exception as e:
        return f"๋‰ด์Šค ์š”์•ฝ ์ค‘ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"

# ๋‰ด์Šค ์š”์•ฝ์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜
def handle_click(company_name, date_clicked):
    return get_real_news_summary(company_name, date_clicked)

# Gradio์—์„œ ์‚ฌ์šฉํ•  ํ•จ์ˆ˜ (๋‰ด์Šค ์š”์•ฝ ํฌํ•จ)
def update_news(input_value, selected_date):
    if selected_date == "" or selected_date is None:
        return "๋‚ ์งœ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”."
    else:
        # ์ข…๋ชฉ๋ช…์„ ๊ฐ€์ ธ์™€์„œ Perplexity๋กœ ๊ฒ€์ƒ‰
        ticker = name_to_ticker.get(input_value, input_value)
        company_name = input_value if ticker == input_value else list(name_to_ticker.keys())[list(name_to_ticker.values()).index(ticker)]
        return handle_click(company_name, selected_date)

# ์ข…๋ชฉ๋ช…๊ณผ ํ‹ฐ์ปค๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ ํ™•์žฅ ๋ฐ ์กฐ๊ฑด๋ณ„ ์ข…๋ชฉ ๋งคํ•‘
name_to_ticker = {
    "์‚ผ์„ฑ์ „์ž": "005930.KS",
    "SK๋ฐ”์ด์˜คํŒœ": "326030.KS",
    "Apple": "AAPL",
    "Nvidia": "NVDA",
    "Vertex": "VRTX",
    "Eli Lilly": "LLY",  # ์ถ”๊ฐ€๋œ ํ•ญ๋ชฉ
    "ํ˜„๋Œ€์ฐจ": "005380.KS",
    "์นด์นด์˜ค": "035720.KS",
    "LGํ™”ํ•™": "051910.KS",
    "์…€ํŠธ๋ฆฌ์˜จ": "068270.KS",
    "๋„ค์ด๋ฒ„": "035420.KS",
    "์—์ฝ”ํ”„๋กœ๋น„์— ": "247540.KS",
    "์•Œํ…Œ์˜ค์  ": "196170.KQ",
    # ์ตœ์‹  ์‹œ๊ฐ€์ด์•ก ์ƒ์œ„ ์ข…๋ชฉ์„ ๋ฐ˜์˜ํ•˜์—ฌ ์—…๋ฐ์ดํŠธ
    "๋‚˜์Šค๋‹ฅ ์‹œ์ด 1์œ„": "AAPL",  # Apple
    "๋‚˜์Šค๋‹ฅ ๋ฐ”์ด์˜คํ… ์‹œ์ด 1์œ„": "VRTX",  # Vertex Pharmaceuticals
    "๋‚˜์Šค๋‹ฅ ํ—ฌ์Šค์ผ€์–ด ์‹œ์ด 1์œ„": "LLY",  # Eli Lilly๋กœ ๋ณ€๊ฒฝ
    "์ฝ”์Šคํ”ผ ์‹œ์ด 1์œ„": "005930.KS",  # ์‚ผ์„ฑ์ „์ž
    "์ฝ”์Šค๋‹ฅ ์‹œ์ด 1์œ„": "196170.KQ",  # ์•Œํ…Œ์˜ค์  
}

# ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์กฐ๊ฑด์— ๋งž๋Š” ๋‚ ์งœ์™€ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
def display_stock_with_highlight(input_value, change_type, percent_change):
    try:
        # ์ž…๋ ฅ๊ฐ’์„ ํ‹ฐ์ปค๋กœ ๋ณ€ํ™˜
        ticker = name_to_ticker.get(input_value, input_value)
        stock = yf.Ticker(ticker)
        stock_data = stock.history(period="5y")  # ์ตœ๊ทผ 5๋…„ ๋ฐ์ดํ„ฐ๋กœ ์ œํ•œ

        if stock_data.empty:
            return "์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.", []

        stock_data['Change'] = stock_data['Close'].pct_change() * 100

        percent_change = float(percent_change)

        if change_type == "์ƒ์Šน":
            highlight_data = stock_data[stock_data['Change'] >= percent_change]
            color = "darkorange"
        elif change_type == "ํ•˜๋ฝ":
            highlight_data = stock_data[stock_data['Change'] <= -percent_change]
            color = "purple"
        else:
            return "Invalid change type", []

        dates = stock_data.index.to_numpy()
        closing_prices = stock_data['Close'].to_numpy()

        plt.figure(figsize=(10, 6))
        plt.plot(dates, closing_prices, color='gray', label=input_value)
        plt.scatter(highlight_data.index, highlight_data['Close'], color=color, label=f'{change_type} ํฌ์ธํŠธ')

        for index, row in highlight_data.iterrows():
            plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
            plt.axvline(x=index, color=color, linestyle='--', linewidth=1)  # x์ถ•๊ณผ์˜ ์—ฐ๊ฒฐ์„  ์ ์„ ์œผ๋กœ ํ‘œ์‹œ

        # ์ข…๋ชฉ๋ช… + '์ฃผ๊ฐ€ ์ถ”์ด'๋กœ ์ œ๋ชฉ ์„ค์ • (์ข…๋ชฉ๋ช… ๊ธฐ๋ฐ˜)
        company_name = list(name_to_ticker.keys())[list(name_to_ticker.values()).index(ticker)]
        plt.title(f'{company_name} ์ฃผ๊ฐ€ ์ถ”์ด', fontproperties=font_prop)
        plt.xlabel('๋‚ ์งœ', fontproperties=font_prop)
        plt.ylabel('์ข…๊ฐ€', fontproperties=font_prop)
        plt.legend()

        buf = io.BytesIO()
        plt.savefig(buf, format='png')
        plt.close()
        buf.seek(0)
        img = Image.open(buf)

        highlight_dates = highlight_data.index.strftime('%Y-%m-%d').tolist()

        return img, gr.update(choices=highlight_dates)
    except Exception as e:
        return f"Error processing data: {e}", gr.update(choices=[])

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ (3์—ด ๋ ˆ์ด์•„์›ƒ)
with gr.Blocks() as demo:
    gr.Markdown("## ์ฃผ๊ฐ€ ๊ทธ๋ž˜ํ”„์™€ ๋‰ด์Šค ์š”์•ฝ")

    with gr.Row():
        with gr.Column():  # ์ž…๋ ฅ๊ฐ’์„ ๋‹ด์„ ์ฒซ ๋ฒˆ์งธ ์—ด
            input_value = gr.Textbox(label="์ข…๋ชฉ๋ช… ๋˜๋Š” ํ‹ฐ์ปค ์ž…๋ ฅ", placeholder="์˜ˆ: SK๋ฐ”์ด์˜คํŒœ, AAPL")
            change_type = gr.Dropdown(choices=["์ƒ์Šน", "ํ•˜๋ฝ"], label="์ƒ์Šน ๋˜๋Š” ํ•˜๋ฝ ์„ ํƒ", value="์ƒ์Šน")
            percent_change = gr.Textbox(label="๋ณ€๋™ ํผ์„ผํŠธ (%)", placeholder="์˜ˆ: 10", value="10")

            submit_btn = gr.Button("Submit")

            # ์˜ˆ์ œ ์—…๋ฐ์ดํŠธ
            examples = [["SK๋ฐ”์ด์˜คํŒœ"],
                        ["๋‚˜์Šค๋‹ฅ ์‹œ์ด 1์œ„"],
                        ["๋‚˜์Šค๋‹ฅ ํ—ฌ์Šค์ผ€์–ด ์‹œ์ด 1์œ„"],  # ์ˆ˜์ •๋œ ํ•ญ๋ชฉ
                        ["๋‚˜์Šค๋‹ฅ ๋ฐ”์ด์˜คํ… ์‹œ์ด 1์œ„"],
                        ["์ฝ”์Šคํ”ผ ์‹œ์ด 1์œ„"],
                        ["์ฝ”์Šค๋‹ฅ ์‹œ์ด 1์œ„"]]
            gr.Examples(examples=examples, inputs=[input_value])

        with gr.Column():  # ๊ทธ๋ž˜ํ”„๋ฅผ ์ถœ๋ ฅํ•  ๋‘ ๋ฒˆ์งธ ์—ด
            plot = gr.Image(label="์ฃผ๊ฐ€ ๊ทธ๋ž˜ํ”„")
            date_dropdown = gr.Dropdown(label="์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๋‚ ์งœ ์„ ํƒ", choices=[])

        with gr.Column():  # ๋‰ด์Šค ์š”์•ฝ์„ ์ถœ๋ ฅํ•  ์„ธ ๋ฒˆ์งธ ์—ด
            # news_output์„ gr.Markdown์œผ๋กœ ๋ณ€๊ฒฝ
            news_output = gr.Markdown(label="๋‰ด์Šค ์š”์•ฝ", value="")  # ๋นˆ ์นธ์œผ๋กœ ๊ธฐ๋ณธ ํ‘œ์‹œ

    # Submit ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๊ทธ๋ž˜ํ”„ ๋ฐ ๋‚ ์งœ ๋“œ๋กญ๋‹ค์šด ์—…๋ฐ์ดํŠธ
    submit_btn.click(
        fn=display_stock_with_highlight,
        inputs=[input_value, change_type, percent_change],
        outputs=[plot, date_dropdown]
    )

    # ๋‚ ์งœ ์„ ํƒ ์‹œ ๋‰ด์Šค ์š”์•ฝ ์—…๋ฐ์ดํŠธ
    date_dropdown.change(
        fn=update_news,
        inputs=[input_value, date_dropdown],
        outputs=[news_output]
    )

# Gradio ์‹คํ–‰
demo.launch()