Commit
8d3265b
ยท
verified ยท
1 Parent(s): 215dd90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -41
app.py CHANGED
@@ -1,40 +1,71 @@
1
- import subprocess
2
- import os
3
- import matplotlib.font_manager as fm
4
- import matplotlib.pyplot as plt
5
- import io
6
- from PIL import Image
7
- from datetime import datetime, timedelta
8
- import gradio as gr
9
- import yfinance as yf
10
- from openai import OpenAI
11
 
12
- # 1. ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ๋ฐ ์—…๋ฐ์ดํŠธ
13
- subprocess.run(["pip", "install", "--upgrade", "pip"])
14
- subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
15
-
16
- # 2. ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ์„ค์น˜ ๋ฐ ์ ์šฉ
17
- subprocess.run(["apt-get", "update"])
18
- subprocess.run(["apt-get", "install", "-y", "fonts-nanum"])
19
- subprocess.run(["fc-cache", "-fv"])
20
-
21
- # 3. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ (ํฐํŠธ ์„ค์น˜ ํ›„)
22
- import matplotlib.font_manager as fm
23
  import matplotlib.pyplot as plt
 
 
 
 
24
 
25
  # ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ๊ฒฝ๋กœ ์„ค์ • ๋ฐ ๊ฐ•์ œ ์ ์šฉ
26
  font_path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf'
27
 
28
  if os.path.exists(font_path):
29
  fm.fontManager.addfont(font_path)
30
- font_prop = fm.FontProperties(fname=font_path)
31
- plt.rcParams['font.family'] = font_prop.get_name()
32
- plt.rcParams['axes.unicode_minus'] = False # ๋งˆ์ด๋„ˆ์Šค ๋ถ€ํ˜ธ ๊นจ์ง ๋ฐฉ์ง€
33
  else:
34
  print("ํฐํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  # Perplexity AI API ์„ค์ •
37
- API_KEY = "์—ฌ๊ธฐ์—_์‹ค์ œ_API_ํ‚ค๋ฅผ_์ž…๋ ฅํ•˜์„ธ์š”" # ์—ฌ๊ธฐ์— Perplexity AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
38
 
39
  def get_real_news_summary(company, date):
40
  # OpenAI ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
@@ -42,8 +73,8 @@ def get_real_news_summary(company, date):
42
 
43
  # API ์š”์ฒญ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€ ๊ตฌ์„ฑ
44
  messages = [
45
- {"role": "system", "content": "๋‹น์‹ ์€ ํ•œ๊ตญ์–ด๋กœ ์ฃผ์‹ ๋‰ด์Šค๋ฅผ ์š”์•ฝํ•ด์ฃผ๋Š” ๋„์›€์ด ๋˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."},
46
- {"role": "user", "content": f"{date}์— ๋Œ€ํ•œ {company}์˜ ์ตœ์‹  ์ฃผ์‹ ๋‰ด์Šค๋ฅผ ํ•œ๊ตญ์–ด๋กœ ์š”์•ฝํ•ด์ค˜. ํ•ด๋‹น ๋‚ ์งœ์— ํŠน์ • ๋‰ด์Šค๊ฐ€ ์—†๋‹ค๋ฉด, ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด์ค˜."}
47
  ]
48
 
49
  try:
@@ -99,7 +130,7 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
99
  highlight_data = stock_data[stock_data['Change'] <= -percent_change]
100
  color = "purple"
101
  else:
102
- return "์ž˜๋ชป๋œ ๋ณ€๋™ ์œ ํ˜•์ž…๋‹ˆ๋‹ค.", []
103
 
104
  dates = stock_data.index.to_numpy()
105
  closing_prices = stock_data['Close'].to_numpy()
@@ -109,14 +140,13 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
109
  plt.scatter(highlight_data.index, highlight_data['Close'], color=color, label=f'{change_type} ํฌ์ธํŠธ')
110
 
111
  for index, row in highlight_data.iterrows():
112
- plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right', fontproperties=font_prop)
113
- plt.axvline(x=index, color=color, linestyle='--', linewidth=1)
114
 
115
- # ๊ทธ๋ž˜ํ”„ ์ œ๋ชฉ, ์ถ• ๋ผ๋ฒจ์„ ํ•œ๊ธ€๋กœ ์„ค์ •
116
- plt.title(f'{input_value} ์ฃผ๊ฐ€ ์ถ”์ด ({change_type} ํ‘œ์‹œ)', fontproperties=font_prop)
117
- plt.xlabel('๋‚ ์งœ', fontproperties=font_prop)
118
- plt.ylabel('์ข…๊ฐ€', fontproperties=font_prop)
119
- plt.legend(prop=font_prop)
120
 
121
  buf = io.BytesIO()
122
  plt.savefig(buf, format='png')
@@ -128,7 +158,7 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
128
 
129
  return img, gr.update(choices=highlight_dates)
130
  except Exception as e:
131
- return f"๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ค‘ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {e}", gr.update(choices=[])
132
 
133
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ (3์—ด ๋ ˆ์ด์•„์›ƒ)
134
  with gr.Blocks() as demo:
@@ -142,12 +172,13 @@ with gr.Blocks() as demo:
142
 
143
  submit_btn = gr.Button("Submit")
144
 
145
- # ์˜ˆ์ œ
146
  examples = [["SK๋ฐ”์ด์˜คํŒœ"],
147
- ["Apple"],
148
- ["์‚ผ์„ฑ์ „์ž"],
149
- ["005930.KS"],
150
- ["AAPL"]]
 
151
  gr.Examples(examples=examples, inputs=[input_value])
152
 
153
  with gr.Column(): # ๊ทธ๋ž˜ํ”„๋ฅผ ์ถœ๋ ฅํ•  ๋‘ ๋ฒˆ์งธ ์—ด
 
1
+ # A ์ฝ”๋“œ
2
+ # 1. ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ์„ค์น˜ ๋ฐ ์ ์šฉ
3
+ !apt-get install -y fonts-nanum
4
+ !fc-cache -fv
 
 
 
 
 
 
5
 
 
 
 
 
 
 
 
 
 
 
 
6
  import matplotlib.pyplot as plt
7
+ import matplotlib.font_manager as fm
8
+ import yfinance as yf
9
+ import numpy as np
10
+ import os
11
 
12
  # ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ๊ฒฝ๋กœ ์„ค์ • ๋ฐ ๊ฐ•์ œ ์ ์šฉ
13
  font_path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf'
14
 
15
  if os.path.exists(font_path):
16
  fm.fontManager.addfont(font_path)
 
 
 
17
  else:
18
  print("ํฐํŠธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
19
 
20
+ # ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ๊ฐ•์ œ ์ ์šฉ
21
+ font_prop = fm.FontProperties(fname=font_path)
22
+ plt.rcParams['font.family'] = font_prop.get_name()
23
+ plt.rcParams['axes.unicode_minus'] = False # ๋งˆ์ด๋„ˆ์Šค ๋ถ€ํ˜ธ ๊นจ์ง ๋ฐฉ์ง€
24
+
25
+ # 2. yfinance๋กœ ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ (์‚ผ์„ฑ์ „์ž ์˜ˆ์‹œ)
26
+ ticker = '005930.KS' # ์‚ผ์„ฑ์ „์ž ํ‹ฐ์ปค
27
+ stock = yf.Ticker(ticker)
28
+ stock_data = stock.history(period="max")
29
+
30
+ # ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ ํ™•์ธ
31
+ print(stock_data.head()) # ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์ผ๋ถ€ ํ™•์ธ
32
+
33
+ # 3. ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™” (pandas ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ numpy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ฒ˜๋ฆฌ)
34
+ plt.figure(figsize=(10, 6))
35
+
36
+ # ์ธ๋ฑ์Šค์™€ ์ข…๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ numpy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
37
+ dates = stock_data.index.to_numpy()
38
+ closing_prices = stock_data['Close'].to_numpy()
39
+
40
+ # ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ ์‹œ๊ฐํ™”
41
+ plt.plot(dates, closing_prices, label='์‚ผ์„ฑ์ „์ž ์ข…๊ฐ€')
42
+
43
+ # ๊ทธ๋ž˜ํ”„ ์ œ๋ชฉ, ์ถ• ๋ผ๋ฒจ์„ ํ•œ๊ธ€๋กœ ์„ค์ •
44
+ plt.title('์‚ผ์„ฑ์ „์ž ์ฃผ๊ฐ€ ์ถ”์ด', fontproperties=font_prop)
45
+ plt.xlabel('๋‚ ์งœ', fontproperties=font_prop)
46
+ plt.ylabel('์ข…๊ฐ€', fontproperties=font_prop)
47
+ plt.legend(prop=font_prop)
48
+
49
+ # B ์ฝ”๋“œ
50
+ import subprocess
51
+
52
+ # ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ๋ฐ ์—…๋ฐ์ดํŠธ
53
+ subprocess.run(["pip", "install", "--upgrade", "pip"])
54
+ subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
55
+
56
+ # ์ดํ›„ ์ฝ”๋“œ ์‹คํ–‰
57
+ import requests
58
+ import gradio as gr
59
+ import yfinance as yf
60
+ import matplotlib.pyplot as plt
61
+ import io
62
+ from PIL import Image
63
+ from datetime import datetime, timedelta
64
+ from openai import OpenAI
65
+ import os
66
+
67
  # Perplexity AI API ์„ค์ •
68
+ API_KEY = "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" # ์—ฌ๊ธฐ์— Perplexity AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
69
 
70
  def get_real_news_summary(company, date):
71
  # OpenAI ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
 
73
 
74
  # API ์š”์ฒญ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€ ๊ตฌ์„ฑ
75
  messages = [
76
+ {"role": "system", "content": "You are a helpful assistant that summarizes stock news in Korean."},
77
+ {"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."}
78
  ]
79
 
80
  try:
 
130
  highlight_data = stock_data[stock_data['Change'] <= -percent_change]
131
  color = "purple"
132
  else:
133
+ return "Invalid change type", []
134
 
135
  dates = stock_data.index.to_numpy()
136
  closing_prices = stock_data['Close'].to_numpy()
 
140
  plt.scatter(highlight_data.index, highlight_data['Close'], color=color, label=f'{change_type} ํฌ์ธํŠธ')
141
 
142
  for index, row in highlight_data.iterrows():
143
+ plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
144
+ plt.axvline(x=index, color=color, linestyle='--', linewidth=1) # x์ถ•๊ณผ์˜ ์—ฐ๊ฒฐ์„  ์ ์„ ์œผ๋กœ ํ‘œ์‹œ
145
 
146
+ plt.title(f'{input_value} ์ฃผ๊ฐ€ ์ถ”์ด ({change_type} ํ‘œ์‹œ)')
147
+ plt.xlabel('๋‚ ์งœ')
148
+ plt.ylabel('์ข…๊ฐ€')
149
+ plt.legend()
 
150
 
151
  buf = io.BytesIO()
152
  plt.savefig(buf, format='png')
 
158
 
159
  return img, gr.update(choices=highlight_dates)
160
  except Exception as e:
161
+ return f"Error processing data: {e}", gr.update(choices=[])
162
 
163
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ (3์—ด ๋ ˆ์ด์•„์›ƒ)
164
  with gr.Blocks() as demo:
 
172
 
173
  submit_btn = gr.Button("Submit")
174
 
175
+ # ์˜ˆ์ œ (์ด์ „ ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๋ณต์›)
176
  examples = [["SK๋ฐ”์ด์˜คํŒœ"],
177
+ ["๋‚˜์Šค๋‹ฅ ์‹œ์ด 1์œ„"],
178
+ ["๋‚˜์Šค๋‹ฅ ์ œ์•ฝ์ฃผ ์‹œ์ด 1์œ„"],
179
+ ["๋‚˜์Šค๋‹ฅ ๋ฐ”์ด์˜คํ… ์‹œ์ด 1์œ„"],
180
+ ["์ฝ”์Šคํ”ผ ์‹œ์ด 1์œ„"],
181
+ ["์ฝ”์Šค๋‹ฅ ์‹œ์ด 1์œ„"]]
182
  gr.Examples(examples=examples, inputs=[input_value])
183
 
184
  with gr.Column(): # ๊ทธ๋ž˜ํ”„๋ฅผ ์ถœ๋ ฅํ•  ๋‘ ๋ฒˆ์งธ ์—ด