Commit
e9fe77c
ยท
verified ยท
1 Parent(s): b8aa6cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -27
app.py CHANGED
@@ -1,11 +1,9 @@
1
- # ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ๋ฐ ์—…๋ฐ์ดํŠธ๋ฅผ ์ตœ์ƒ๋‹จ์— ๋ฐฐ์น˜
2
  import subprocess
3
 
4
  # ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ๋ฐ ์—…๋ฐ์ดํŠธ
5
  subprocess.run(["pip", "install", "--upgrade", "pip"])
6
  subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
7
 
8
- # ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ
9
  import yfinance as yf
10
  import os
11
  import matplotlib.font_manager as fm
@@ -47,41 +45,33 @@ API_KEY = "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" # ์—ฌ๊ธฐ์— P
47
 
48
  # ๋‰ด์Šค ์š”์•ฝ์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜ (๊ธฐ์กด ์ฝ”๋“œ ์œ ์ง€)
49
  def get_real_news_summary(company, date):
50
- # OpenAI ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
51
  client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
52
 
53
- # ๋‚ ์งœ ํ˜•์‹์„ ๋งž์ถฐ์ฃผ๊ธฐ ์œ„ํ•œ ์ฒ˜๋ฆฌ
54
  target_date = datetime.strptime(date, '%Y-%m-%d')
55
  start_date = (target_date - timedelta(days=1)).strftime('%Y-%m-%d')
56
  end_date = (target_date + timedelta(days=1)).strftime('%Y-%m-%d')
57
 
58
- # API ์š”์ฒญ์„ ์œ„ํ•œ ๋ฉ”์‹œ์ง€ ๊ตฌ์„ฑ - ํ•œ๊ตญ์–ด๋กœ๋งŒ ์‘๋‹ต์„ ๋ฐ›๋„๋ก ์ง€์‹œ
59
  messages = [
60
  {"role": "system", "content": "You are a helpful assistant that summarizes stock news strictly in Korean."},
61
  {"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."}
62
  ]
63
 
64
  try:
65
- # API ์š”์ฒญ
66
  response = client.chat.completions.create(
67
  model="llama-3.1-sonar-large-128k-online",
68
  messages=messages
69
  )
70
-
71
- # ์‘๋‹ต์—์„œ ์š”์•ฝ ์ถ”์ถœ
72
  summary = response.choices[0].message.content
73
 
74
  # ํ•œ๊ธ€, ์ˆซ์ž, ๊ณต๋ฐฑ, ํŠน์ˆ˜ ๊ธฐํ˜ธ๋งŒ ๋‚จ๊ธฐ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹
75
  korean_only_summary = re.sub(r'[^\w\s#.,!%()\-\[\]]', '', summary)
76
-
77
- # ##๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ถ€๋ถ„์„ **์œผ๋กœ ๊ฐ์‹ธ์„œ Bold ์ฒ˜๋ฆฌ
78
  formatted_summary = re.sub(r'##\s*(.+)', r'**\1**', korean_only_summary)
79
 
80
  return formatted_summary
81
  except Exception as e:
82
  return f"๋‰ด์Šค ์š”์•ฝ ์ค‘ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
83
 
84
- # ๋‰ด์Šค ์š”์•ฝ์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜
85
  def handle_click(company_name, date_clicked):
86
  return get_real_news_summary(company_name, date_clicked)
87
 
@@ -90,16 +80,11 @@ def update_news(input_value, selected_date):
90
  if selected_date == "" or selected_date is None:
91
  return "๋‚ ์งœ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”."
92
  else:
93
- # ์ข…๋ชฉ๋ช…์„ ๊ฐ€์ ธ์™€์„œ Perplexity๋กœ ๊ฒ€์ƒ‰
94
  ticker = get_dynamic_ticker(input_value)
95
- company_name = input_value if ticker == input_value else ticker_to_name.get(ticker, input_value)
96
  return handle_click(company_name, selected_date)
97
 
98
- # ์ข…๋ชฉ๋ช…๊ณผ ํ‹ฐ์ปค๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ
99
- ticker_to_name = {}
100
- name_to_ticker = {}
101
-
102
- # ๋ชจ๋“  ์˜ˆ์ œ๋ฅผ ๋™์ ์œผ๋กœ ๋งค์นญํ•˜๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
103
  def get_dynamic_ticker(input_value):
104
  if input_value == "๋‚˜์Šค๋‹ฅ ์‹œ์ด 1์œ„":
105
  return get_top_market_cap_stock("๋‚˜์Šค๋‹ฅ")
@@ -112,18 +97,15 @@ def get_dynamic_ticker(input_value):
112
  elif input_value == "์ฝ”์Šค๋‹ฅ ์‹œ์ด 1์œ„":
113
  return get_top_market_cap_stock("์ฝ”์Šค๋‹ฅ")
114
  else:
115
- # ์‚ฌ์ „์— ๋“ฑ๋ก๋œ ์ข…๋ชฉ๋ช… ๋ฐ˜ํ™˜
116
  return name_to_ticker.get(input_value, input_value)
117
 
118
- # ์‹œ๊ฐ€์ด์•ก ์ƒ์œ„ ์ข…๋ชฉ์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜
119
  def get_top_market_cap_stock(market, sector=None, industry=None):
120
- # yfinance๋Š” ์ข…๋ชฉ ๋ชฉ๋ก์„ ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ETF๋ฅผ ํ™œ์šฉ
121
  if market == "๋‚˜์Šค๋‹ฅ":
122
- etf_ticker = "QQQ" # ๋‚˜์Šค๋‹ฅ 100 ETF
123
  elif market == "์ฝ”์Šคํ”ผ":
124
- etf_ticker = "EWY" # ํ•œ๊ตญ ETF
125
  elif market == "์ฝ”์Šค๋‹ฅ":
126
- # ์ฝ”์Šค๋‹ฅ ์ข…๋ชฉ์€ ์ ‘๊ทผํ•˜๊ธฐ ์–ด๋ ค์šฐ๋ฏ€๋กœ ์‚ฌ์ „ ์ •์˜๋œ ๋ฆฌ์ŠคํŠธ ์‚ฌ์šฉ
127
  tickers = ["035420.KQ", "068270.KQ", "035720.KQ"]
128
  else:
129
  return None
@@ -158,10 +140,9 @@ def get_top_market_cap_stock(market, sector=None, industry=None):
158
  # ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์กฐ๊ฑด์— ๋งž๋Š” ๋‚ ์งœ์™€ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
159
  def display_stock_with_highlight(input_value, change_type, percent_change):
160
  try:
161
- # ์ž…๋ ฅ๊ฐ’์„ ํ‹ฐ์ปค๋กœ ๋ณ€ํ™˜
162
  ticker = get_dynamic_ticker(input_value)
163
  stock = yf.Ticker(ticker)
164
- stock_data = stock.history(period="5y") # ์ตœ๊ทผ 5๋…„ ๋ฐ์ดํ„ฐ๋กœ ์ œํ•œ
165
 
166
  if stock_data.empty:
167
  return "์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.", []
@@ -189,7 +170,6 @@ def display_stock_with_highlight(input_value, change_type, percent_change):
189
  plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
190
  plt.axvline(x=index, color=color, linestyle='--', linewidth=1)
191
 
192
- # ์ข…๋ชฉ๋ช… + '์ฃผ๊ฐ€ ์ถ”์ด'๋กœ ์ œ๋ชฉ ์„ค์ • (์ข…๏ฟฝ๏ฟฝ๏ฟฝ๋ช… ๊ธฐ๋ฐ˜)
193
  company_name = ticker_to_name.get(ticker, input_value)
194
  plt.title(f'{company_name} ์ฃผ๊ฐ€ ์ถ”์ด', fontproperties=font_prop)
195
  plt.xlabel('๋‚ ์งœ', fontproperties=font_prop)
 
 
1
  import subprocess
2
 
3
  # ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ ๋ฐ ์—…๋ฐ์ดํŠธ
4
  subprocess.run(["pip", "install", "--upgrade", "pip"])
5
  subprocess.run(["pip", "install", "--upgrade", "openai", "yfinance", "gradio", "matplotlib", "Pillow"])
6
 
 
7
  import yfinance as yf
8
  import os
9
  import matplotlib.font_manager as fm
 
45
 
46
  # ๋‰ด์Šค ์š”์•ฝ์„ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜ (๊ธฐ์กด ์ฝ”๋“œ ์œ ์ง€)
47
  def get_real_news_summary(company, date):
 
48
  client = OpenAI(api_key=API_KEY, base_url="https://api.perplexity.ai")
49
 
50
+ # ๋‚ ์งœ ํ˜•์‹ ์กฐ์ •
51
  target_date = datetime.strptime(date, '%Y-%m-%d')
52
  start_date = (target_date - timedelta(days=1)).strftime('%Y-%m-%d')
53
  end_date = (target_date + timedelta(days=1)).strftime('%Y-%m-%d')
54
 
 
55
  messages = [
56
  {"role": "system", "content": "You are a helpful assistant that summarizes stock news strictly in Korean."},
57
  {"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."}
58
  ]
59
 
60
  try:
 
61
  response = client.chat.completions.create(
62
  model="llama-3.1-sonar-large-128k-online",
63
  messages=messages
64
  )
 
 
65
  summary = response.choices[0].message.content
66
 
67
  # ํ•œ๊ธ€, ์ˆซ์ž, ๊ณต๋ฐฑ, ํŠน์ˆ˜ ๊ธฐํ˜ธ๋งŒ ๋‚จ๊ธฐ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹
68
  korean_only_summary = re.sub(r'[^\w\s#.,!%()\-\[\]]', '', summary)
 
 
69
  formatted_summary = re.sub(r'##\s*(.+)', r'**\1**', korean_only_summary)
70
 
71
  return formatted_summary
72
  except Exception as e:
73
  return f"๋‰ด์Šค ์š”์•ฝ ์ค‘ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
74
 
 
75
  def handle_click(company_name, date_clicked):
76
  return get_real_news_summary(company_name, date_clicked)
77
 
 
80
  if selected_date == "" or selected_date is None:
81
  return "๋‚ ์งœ๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”."
82
  else:
 
83
  ticker = get_dynamic_ticker(input_value)
84
+ company_name = ticker_to_name.get(ticker, input_value)
85
  return handle_click(company_name, selected_date)
86
 
87
+ # ์ข…๋ชฉ ๋™์  ๋งค์นญ ํ•จ์ˆ˜
 
 
 
 
88
  def get_dynamic_ticker(input_value):
89
  if input_value == "๋‚˜์Šค๋‹ฅ ์‹œ์ด 1์œ„":
90
  return get_top_market_cap_stock("๋‚˜์Šค๋‹ฅ")
 
97
  elif input_value == "์ฝ”์Šค๋‹ฅ ์‹œ์ด 1์œ„":
98
  return get_top_market_cap_stock("์ฝ”์Šค๋‹ฅ")
99
  else:
 
100
  return name_to_ticker.get(input_value, input_value)
101
 
102
+ # ์‹œ๊ฐ€์ด์•ก ์ƒ์œ„ ์ข…๋ชฉ ๊ฐ€์ ธ์˜ค๊ธฐ
103
  def get_top_market_cap_stock(market, sector=None, industry=None):
 
104
  if market == "๋‚˜์Šค๋‹ฅ":
105
+ etf_ticker = "QQQ"
106
  elif market == "์ฝ”์Šคํ”ผ":
107
+ etf_ticker = "EWY"
108
  elif market == "์ฝ”์Šค๋‹ฅ":
 
109
  tickers = ["035420.KQ", "068270.KQ", "035720.KQ"]
110
  else:
111
  return None
 
140
  # ์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  ์กฐ๊ฑด์— ๋งž๋Š” ๋‚ ์งœ์™€ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
141
  def display_stock_with_highlight(input_value, change_type, percent_change):
142
  try:
 
143
  ticker = get_dynamic_ticker(input_value)
144
  stock = yf.Ticker(ticker)
145
+ stock_data = stock.history(period="5y")
146
 
147
  if stock_data.empty:
148
  return "์ฃผ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.", []
 
170
  plt.text(index, row['Close'], index.strftime('%Y-%m-%d'), fontsize=10, fontweight='bold', color=color, ha='right')
171
  plt.axvline(x=index, color=color, linestyle='--', linewidth=1)
172
 
 
173
  company_name = ticker_to_name.get(ticker, input_value)
174
  plt.title(f'{company_name} ์ฃผ๊ฐ€ ์ถ”์ด', fontproperties=font_prop)
175
  plt.xlabel('๋‚ ์งœ', fontproperties=font_prop)