Commit
โข
a363720
1
Parent(s):
bc7fd9d
Update app.py
Browse files
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 |
-
|
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 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
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 =
|
135 |
-
info = stock.
|
|
|
|
|
|
|
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['
|
176 |
-
highest_price = recent['
|
177 |
-
lowest_price = recent['
|
178 |
return f"ํ๊ท ๊ฐ: {average_price:.2f}, ์ต๊ณ ๊ฐ: {highest_price:.2f}, ์ต์ ๊ฐ: {lowest_price:.2f}"
|
179 |
|
180 |
def get_news_summary(ticker):
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|