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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -33
app.py CHANGED
@@ -1,41 +1,40 @@
1
  import subprocess
2
- import sys
3
  import os
4
  import matplotlib.font_manager as fm
5
  import matplotlib.pyplot as plt
6
  import io
7
  from PIL import Image
8
- from datetime import datetime
9
  import gradio as gr
10
  import yfinance as yf
11
  from openai import OpenAI
12
- import requests
13
-
14
- # ํŒจํ‚ค์ง€ ์„ค์น˜ (ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ๋ฐ ์—…๋ฐ์ดํŠธ)
15
- def install_packages():
16
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])
17
- subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
18
-
19
- # ํŒจํ‚ค์ง€ ์„ค์น˜
20
- install_packages()
21
-
22
- # ๋‚˜๋ˆ”๊ณ ๋”• ํฐํŠธ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์„ค์ •
23
- def download_and_setup_font():
24
- font_url = 'https://github.com/naver/nanumfont/blob/master/ttf/NanumGothic.ttf?raw=true'
25
- font_path = os.path.join(os.path.expanduser("~"), 'NanumGothic.ttf')
26
- response = requests.get(font_url)
27
- with open(font_path, 'wb') as f:
28
- f.write(response.content)
 
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
-
34
- # ํฐํŠธ ์„ค์ •
35
- download_and_setup_font()
36
 
37
  # Perplexity AI API ์„ค์ •
38
- API_KEY = "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" # ์—ฌ๊ธฐ์— Perplexity AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.
39
 
40
  def get_real_news_summary(company, date):
41
  # OpenAI ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
@@ -43,8 +42,8 @@ def get_real_news_summary(company, date):
43
 
44
  # API ์š”์ฒญ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€ ๊ตฌ์„ฑ
45
  messages = [
46
- {"role": "system", "content": "You are a helpful assistant that summarizes stock news in Korean."},
47
- {"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."}
48
  ]
49
 
50
  try:
@@ -100,7 +99,7 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
100
  highlight_data = stock_data[stock_data['Change'] <= -percent_change]
101
  color = "purple"
102
  else:
103
- return "Invalid change type", []
104
 
105
  dates = stock_data.index.to_numpy()
106
  closing_prices = stock_data['Close'].to_numpy()
@@ -110,13 +109,14 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
110
  plt.scatter(highlight_data.index, highlight_data['Close'], color=color, label=f'{change_type} ํฌ์ธํŠธ')
111
 
112
  for index, row in highlight_data.iterrows():
113
- plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
114
- plt.axvline(x=index, color=color, linestyle='--', linewidth=1) # x์ถ•๊ณผ์˜ ์—ฐ๊ฒฐ์„  ์ ์„ ์œผ๋กœ ํ‘œ์‹œ
115
 
116
- plt.title(f'{input_value} ์ฃผ๊ฐ€ ์ถ”์ด ({change_type} ํ‘œ์‹œ)')
117
- plt.xlabel('๋‚ ์งœ')
118
- plt.ylabel('์ข…๊ฐ€')
119
- plt.legend()
 
120
 
121
  buf = io.BytesIO()
122
  plt.savefig(buf, format='png')
@@ -128,7 +128,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"Error processing data: {e}", gr.update(choices=[])
132
 
133
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ (3์—ด ๋ ˆ์ด์•„์›ƒ)
134
  with gr.Blocks() as demo:
 
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
 
43
  # API ์š”์ฒญ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€ ๊ตฌ์„ฑ
44
  messages = [
45
+ {"role": "system", "content": "๋‹น์‹ ์€ ํ•œ๊ตญ์–ด๋กœ ์ฃผ์‹ ๋‰ด์Šค๋ฅผ ์š”์•ฝํ•ด์ฃผ๋Š” ๋„์›€์ด ๋˜๋Š” ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค."},
46
+ {"role": "user", "content": f"{date}์— ๋Œ€ํ•œ {company}์˜ ์ตœ์‹  ์ฃผ์‹ ๋‰ด์Šค๋ฅผ ํ•œ๊ตญ์–ด๋กœ ์š”์•ฝํ•ด์ค˜. ํ•ด๋‹น ๋‚ ์งœ์— ํŠน์ • ๋‰ด์Šค๊ฐ€ ์—†๋‹ค๋ฉด, ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด์ค˜."}
47
  ]
48
 
49
  try:
 
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
  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
 
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: