File size: 6,079 Bytes
97a16e9 bf83c95 97a16e9 bf83c95 97a16e9 bf83c95 |
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 |
# νμν λΌμ΄λΈλ¬λ¦¬ μ€μΉ λ° μ
λ°μ΄νΈ
!pip install --upgrade pip
!pip install --upgrade openai yfinance gradio matplotlib Pillow
# λͺ¨λ νμν ν¨ν€μ§κ° μ€μΉλ ν κ³μν΄μ μ€νλλλ‘ ν¨.
import requests
import gradio as gr
import yfinance as yf
import matplotlib.pyplot as plt
import io
from PIL import Image
from datetime import datetime, timedelta
from openai import OpenAI
import os
# 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")
# API μμ²μ μν λ©μμ§ κ΅¬μ±
messages = [
{"role": "system", "content": "You are a helpful assistant that summarizes stock news in Korean."},
{"role": "user", "content": f"Summarize the latest stock news for {company} on {date} in Korean. If there's no specific news for that date, provide the most recent relevant information."}
]
try:
# API μμ²
response = client.chat.completions.create(
model="llama-3.1-sonar-large-128k-online",
messages=messages
)
# μλ΅μμ μμ½ μΆμΆ
summary = response.choices[0].message.content
return summary
except Exception as e:
return f"λ΄μ€ μμ½ μ€ μλ¬κ° λ°μνμ΅λλ€: {str(e)}"
# λ΄μ€ μμ½μ κ°μ Έμ€λ ν¨μ
def handle_click(input_value, date_clicked):
return get_real_news_summary(input_value, date_clicked)
# Gradioμμ μ¬μ©ν ν¨μ (λ΄μ€ μμ½ ν¬ν¨)
def update_news(input_value, selected_date):
if selected_date == "" or selected_date is None:
return "λ μ§λ₯Ό μ νν΄μ£ΌμΈμ."
else:
return handle_click(input_value, selected_date)
# μ’
λͺ©λͺ
κ³Ό ν°μ»€λ₯Ό λ§€ννλ λμ
λ리
name_to_ticker = {
"μΌμ±μ μ": "005930.KS",
"SKλ°μ΄μ€ν": "326030.KS",
"Apple": "AAPL",
# νμν μ’
λͺ©λ€μ μΆκ°νμΈμ
}
# μ£Όκ° λ°μ΄ν°λ₯Ό κ°μ Έμ€κ³ 쑰건μ λ§λ λ μ§μ κ·Έλνλ₯Ό λ°ννλ ν¨μ
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μΆκ³Όμ μ°κ²°μ μ μ μΌλ‘ νμ
plt.title(f'{input_value} μ£Όκ° μΆμ΄ ({change_type} νμ)')
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.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.Textbox(label="λ΄μ€ μμ½")
# 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()
|