|
|
|
import subprocess |
|
|
|
subprocess.run(["pip", "install", "--upgrade", "pip"]) |
|
subprocess.run(["pip", "install", "--upgrade", "yfinance", "gradio", "matplotlib", "Pillow"]) |
|
|
|
|
|
import yfinance as yf |
|
import gradio as gr |
|
import matplotlib.pyplot as plt |
|
import matplotlib.font_manager as fm |
|
import numpy as np |
|
import pandas as pd |
|
import re |
|
import io |
|
from PIL import Image |
|
from datetime import datetime, timedelta |
|
|
|
|
|
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) |
|
plt.rcParams['font.family'] = 'NanumGothic' |
|
except Exception as e: |
|
print(f"ν°νΈ μ€μΉ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {e}") |
|
|
|
install_nanum_font() |
|
|
|
|
|
def get_real_news_summary(company, date): |
|
|
|
return f"{company}μ {date} μ£Όλ³ λ΄μ€ μμ½μ
λλ€." |
|
|
|
def handle_click(company_name, date_clicked): |
|
return get_real_news_summary(company_name, date_clicked) |
|
|
|
def update_news(input_value, selected_date): |
|
if not selected_date: |
|
return "λ μ§λ₯Ό μ νν΄μ£ΌμΈμ." |
|
else: |
|
ticker = name_to_ticker.get(input_value, input_value) |
|
company_name = ticker_to_name.get(ticker, input_value) |
|
return handle_click(company_name, selected_date) |
|
|
|
|
|
name_to_ticker = { |
|
"SKλ°μ΄μ€ν": "326030.KS", |
|
"λμ€λ₯ μμ΄ 1μ": "AAPL", |
|
"λμ€λ₯ λ°μ΄μ€ν
μμ΄ 1μ": "AMGN", |
|
"λμ€λ₯ ν¬μ€μΌμ΄ μμ΄ 1μ": "JNJ", |
|
"μ½μ€νΌ μμ΄ 1μ": "005930.KS", |
|
"μ½μ€λ₯ μμ΄ 1μ": "247540.KQ", |
|
} |
|
|
|
ticker_to_name = {v: k for k, v in name_to_ticker.items()} |
|
|
|
|
|
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") |
|
|
|
if stock_data.empty: |
|
return "μ£Όκ° λ°μ΄ν°λ₯Ό μ°Ύμ μ μμ΅λλ€.", gr.Dropdown.update(choices=[]) |
|
|
|
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", gr.Dropdown.update(choices=[]) |
|
|
|
dates = stock_data.index |
|
closing_prices = stock_data['Close'] |
|
|
|
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=8, color=color, ha='right') |
|
plt.axvline(x=index, color=color, linestyle='--', linewidth=1) |
|
|
|
company_name = ticker_to_name.get(ticker, input_value) |
|
plt.title(f'{company_name} μ£Όκ° μΆμ΄') |
|
plt.xlabel('λ μ§') |
|
plt.ylabel('μ’
κ°') |
|
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.Dropdown.update(choices=highlight_dates) |
|
except Exception as e: |
|
print(f"Error: {e}") |
|
return f"λ°μ΄ν° μ²λ¦¬ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {e}", gr.Dropdown.update(choices=[]) |
|
|
|
|
|
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(label="λ΄μ€ μμ½", value="") |
|
|
|
|
|
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] |
|
) |
|
|
|
demo.launch() |
|
|