seawolf2357 commited on
Commit
a363720
โ€ข
1 Parent(s): bc7fd9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -34
app.py CHANGED
@@ -2,9 +2,10 @@ import discord
2
  import logging
3
  import os
4
  import asyncio
5
- import yfinance as yf
6
- from huggingface_hub import InferenceClient
7
  import subprocess
 
 
 
8
 
9
  # ๋กœ๊น… ์„ค์ •
10
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
@@ -95,13 +96,7 @@ async def generate_response(message):
95
  response = await asyncio.get_event_loop().run_in_executor(None, lambda: hf_client.chat_completion(
96
  messages=messages, max_tokens=1000, temperature=0.7, top_p=0.85))
97
 
98
- full_response = []
99
- for part in response:
100
- logging.debug(f'์ŠคํŠธ๋ฆผ์—์„œ ๋ฐ›์€ ๋ถ€๋ถ„: {part}')
101
- if part.choices and part.choices[0].delta and part.choices[0].delta.content:
102
- full_response.append(part.choices[0].delta.content)
103
-
104
- full_response_text = ''.join(full_response)
105
  logging.debug(f'๋ชจ๋ธ ์ „์ฒด ์‘๋‹ต: {full_response_text}')
106
 
107
  # ์–ด์‹œ์Šคํ„ดํŠธ ์‘๋‹ต์„ ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ์— ์ถ”๊ฐ€
@@ -114,34 +109,33 @@ async def generate_response(message):
114
 
115
  def get_ticker_from_name(name):
116
  try:
117
- search_results = yf.Ticker(name).info
118
- if 'symbol' in search_results:
119
- return search_results['symbol']
120
- else:
121
- # ์ข…๋ชฉ๋ช…์ด ์•„๋‹Œ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ํ‹ฐ์ปค ๊ฒ€์ƒ‰
122
- ticker_search = yf.utils.get_tickers()
123
- matches = [t for t in ticker_search if name.lower() in t.lower()]
124
- if matches:
125
- return matches[0] # ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ํ‹ฐ์ปค ๋ฐ˜ํ™˜
126
- else:
127
- return None
128
  except Exception as e:
129
  logging.error(f'Error retrieving ticker for {name}: {e}')
130
  return None
131
 
132
  def get_company_info(ticker):
133
  try:
134
- stock = yf.Ticker(ticker)
135
- info = stock.info
 
 
 
136
  history = stock.history(period="10y")
 
137
 
138
  summary = f"""
139
- **ํšŒ์‚ฌ ์ด๋ฆ„:** {info.get('longName', 'N/A')}
140
  **ํ‹ฐ์ปค:** {ticker}
141
- **์‚ฐ์—…:** {info.get('industry', 'N/A')}
142
- **์„นํ„ฐ:** {info.get('sector', 'N/A')}
143
- **์‹œ๊ฐ€ ์ด์•ก:** {info.get('marketCap', 'N/A')}
144
- **ํ˜„์žฌ๊ฐ€:** {info.get('previousClose', 'N/A')}
 
145
 
146
  **์ตœ๊ทผ 1์ผ ์ฐจํŠธ:**\n{get_chart_summary(history, period='1d')}
147
  **์ตœ๊ทผ 1์ฃผ ์ฐจํŠธ:**\n{get_chart_summary(history, period='1wk')}
@@ -160,7 +154,9 @@ def get_company_info(ticker):
160
  return f"ํ‹ฐ์ปค {ticker}์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
161
 
162
  def get_chart_summary(history, period):
163
- # ์ฐจํŠธ ์š”์•ฝ ์ƒ์„ฑ (์˜ˆ: ํ‰๊ท , ์ตœ๊ณ , ์ตœ์ € ๋“ฑ)
 
 
164
  if period == '1d':
165
  recent = history.tail(1)
166
  elif period == '1wk':
@@ -172,15 +168,24 @@ def get_chart_summary(history, period):
172
  else: # '10y'
173
  recent = history
174
 
175
- average_price = recent['Close'].mean()
176
- highest_price = recent['Close'].max()
177
- lowest_price = recent['Close'].min()
178
  return f"ํ‰๊ท ๊ฐ€: {average_price:.2f}, ์ตœ๊ณ ๊ฐ€: {highest_price:.2f}, ์ตœ์ €๊ฐ€: {lowest_price:.2f}"
179
 
180
  def get_news_summary(ticker):
181
- # ์ตœ์‹  ๋‰ด์Šค ์š”์•ฝ ์ƒ์„ฑ (Yahoo Finance API ๋“ฑ์„ ํ™œ์šฉํ•˜์—ฌ ๋‰ด์Šค ์š”์•ฝ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ)
182
- # ํ˜„์žฌ๋Š” ์˜ˆ์‹œ๋กœ ๊ฐ„๋‹จํ•œ ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
183
- return f"{ticker} ๊ด€๋ จ ์ตœ์‹  ๋‰ด์Šค: (๋‰ด์Šค ์„œ๋น„์Šค์™€์˜ ํ†ตํ•ฉ ํ•„์š”)"
 
 
 
 
 
 
 
 
 
184
 
185
  if __name__ == "__main__":
186
  discord_token = os.getenv('DISCORD_TOKEN')
 
2
  import logging
3
  import os
4
  import asyncio
 
 
5
  import subprocess
6
+ from huggingface_hub import InferenceClient
7
+ from yahooquery import Ticker
8
+ from datetime import datetime
9
 
10
  # ๋กœ๊น… ์„ค์ •
11
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', handlers=[logging.StreamHandler()])
 
96
  response = await asyncio.get_event_loop().run_in_executor(None, lambda: hf_client.chat_completion(
97
  messages=messages, max_tokens=1000, temperature=0.7, top_p=0.85))
98
 
99
+ full_response_text = ''.join(response)
 
 
 
 
 
 
100
  logging.debug(f'๋ชจ๋ธ ์ „์ฒด ์‘๋‹ต: {full_response_text}')
101
 
102
  # ์–ด์‹œ์Šคํ„ดํŠธ ์‘๋‹ต์„ ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ์— ์ถ”๊ฐ€
 
109
 
110
  def get_ticker_from_name(name):
111
  try:
112
+ ticker_search = Ticker(name)
113
+ search_results = ticker_search.summary_detail
114
+ if search_results and name in search_results:
115
+ return search_results[name]['symbol']
116
+ return None
 
 
 
 
 
 
117
  except Exception as e:
118
  logging.error(f'Error retrieving ticker for {name}: {e}')
119
  return None
120
 
121
  def get_company_info(ticker):
122
  try:
123
+ stock = Ticker(ticker)
124
+ info = stock.summary_detail
125
+ if not info:
126
+ return f"ํ‹ฐ์ปค {ticker}์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
127
+
128
  history = stock.history(period="10y")
129
+ now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
130
 
131
  summary = f"""
132
+ **ํšŒ์‚ฌ ์ด๋ฆ„:** {info[ticker].get('longName', 'N/A')}
133
  **ํ‹ฐ์ปค:** {ticker}
134
+ **์‚ฐ์—…:** {info[ticker].get('industry', 'N/A')}
135
+ **์„นํ„ฐ:** {info[ticker].get('sector', 'N/A')}
136
+ **์‹œ๊ฐ€ ์ด์•ก:** {info[ticker].get('marketCap', 'N/A')}
137
+ **ํ˜„์žฌ๊ฐ€:** {info[ticker].get('previousClose', 'N/A')}
138
+ **๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ:** {now}
139
 
140
  **์ตœ๊ทผ 1์ผ ์ฐจํŠธ:**\n{get_chart_summary(history, period='1d')}
141
  **์ตœ๊ทผ 1์ฃผ ์ฐจํŠธ:**\n{get_chart_summary(history, period='1wk')}
 
154
  return f"ํ‹ฐ์ปค {ticker}์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
155
 
156
  def get_chart_summary(history, period):
157
+ if history.empty:
158
+ return "์ฐจํŠธ ๋ฐ์ดํ„ฐ ์—†์Œ"
159
+
160
  if period == '1d':
161
  recent = history.tail(1)
162
  elif period == '1wk':
 
168
  else: # '10y'
169
  recent = history
170
 
171
+ average_price = recent['close'].mean()
172
+ highest_price = recent['close'].max()
173
+ lowest_price = recent['close'].min()
174
  return f"ํ‰๊ท ๊ฐ€: {average_price:.2f}, ์ตœ๊ณ ๊ฐ€: {highest_price:.2f}, ์ตœ์ €๊ฐ€: {lowest_price:.2f}"
175
 
176
  def get_news_summary(ticker):
177
+ try:
178
+ stock = Ticker(ticker)
179
+ news = stock.news()
180
+ if not news:
181
+ return "์ตœ์‹  ๋‰ด์Šค ์—†์Œ"
182
+
183
+ top_news = news[:3]
184
+ news_summary = "\n".join([f"{i+1}. [{article['title']}]({article['link']}) - {article['providerPublishTime']}" for i, article in enumerate(top_news)])
185
+ return news_summary
186
+ except Exception as e:
187
+ logging.error(f'Error retrieving news for {ticker}: {e}')
188
+ return "๋‰ด์Šค ์š”์•ฝ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค."
189
 
190
  if __name__ == "__main__":
191
  discord_token = os.getenv('DISCORD_TOKEN')