Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,8 @@ from transformers import GPT2Tokenizer
|
|
4 |
import yfinance as yf
|
5 |
import time
|
6 |
import logging
|
|
|
|
|
7 |
|
8 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
9 |
|
@@ -62,6 +64,7 @@ def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.
|
|
62 |
input_tokens = len(tokenizer.encode(prompt))
|
63 |
total_tokens_used += input_tokens
|
64 |
available_tokens = 32768 - total_tokens_used
|
|
|
65 |
if available_tokens <= 0:
|
66 |
yield f"Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}"
|
67 |
return
|
@@ -69,23 +72,40 @@ def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.
|
|
69 |
formatted_prompt = format_prompt(prompt, history)
|
70 |
output_accumulated = ""
|
71 |
try:
|
72 |
-
stream = client.text_generation(
|
73 |
-
|
74 |
-
temperature=temperature,
|
75 |
-
max_new_tokens=min(max_new_tokens, available_tokens),
|
76 |
-
top_p=top_p,
|
77 |
-
repetition_penalty=repetition_penalty,
|
78 |
-
do_sample=True,
|
79 |
-
seed=42,
|
80 |
-
stream=True
|
81 |
-
)
|
82 |
for response in stream:
|
83 |
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
84 |
output_accumulated += output_part
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
except Exception as e:
|
87 |
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
def validate_ticker(ticker):
|
90 |
logging.debug(f"Validating ticker: {ticker}")
|
91 |
stock = yf.Ticker(ticker)
|
|
|
4 |
import yfinance as yf
|
5 |
import time
|
6 |
import logging
|
7 |
+
from datetime import datetime
|
8 |
+
|
9 |
|
10 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
|
|
|
64 |
input_tokens = len(tokenizer.encode(prompt))
|
65 |
total_tokens_used += input_tokens
|
66 |
available_tokens = 32768 - total_tokens_used
|
67 |
+
|
68 |
if available_tokens <= 0:
|
69 |
yield f"Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}"
|
70 |
return
|
|
|
72 |
formatted_prompt = format_prompt(prompt, history)
|
73 |
output_accumulated = ""
|
74 |
try:
|
75 |
+
stream = client.text_generation(formatted_prompt, temperature=temperature, max_new_tokens=min(max_new_tokens, available_tokens),
|
76 |
+
top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, stream=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
for response in stream:
|
78 |
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
79 |
output_accumulated += output_part
|
80 |
+
|
81 |
+
# ๋ฐ์ดํฐ ๋ค์ด๋ก๋ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
82 |
+
if "ํฐ์ปค" in output_part:
|
83 |
+
ticker = extract_ticker(output_part) # ํฐ์ปค ์ถ์ถํ๋ ๋๋ฏธ ํจ์, ์ค์ ๋ก๋ ํฐ์ปค๋ฅผ ์ถ์ถํ๋ ๋ก์ง ํ์
|
84 |
+
download_result = download_stock_data(ticker)
|
85 |
+
output_accumulated += download_result
|
86 |
+
|
87 |
+
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}"
|
88 |
except Exception as e:
|
89 |
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
90 |
|
91 |
+
def download_stock_data(ticker):
|
92 |
+
try:
|
93 |
+
today = datetime.now()
|
94 |
+
start_date = today.replace(year=today.year - 10).strftime('%Y-%m-%d')
|
95 |
+
end_date = today.strftime('%Y-%m-%d')
|
96 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
97 |
+
if data.empty:
|
98 |
+
return f"Error: ๋ฐ์ดํฐ๋ฅผ ๋ค์ด๋ก๋ํ ์ ์์ต๋๋ค. ํฐ์ปค {ticker}๋ฅผ ํ์ธํ์ธ์."
|
99 |
+
else:
|
100 |
+
return f"Success: {ticker} ๋ฐ์ดํฐ ๋ค์ด๋ก๋ ์ฑ๊ณต."
|
101 |
+
except Exception as e:
|
102 |
+
return f"Error: ๋ฐ์ดํฐ ๋ค์ด๋ก๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. {str(e)}"
|
103 |
+
|
104 |
+
def extract_ticker(output_part):
|
105 |
+
# ์ด ํจ์๋ ์ฌ์ฉ์ ์ถ๋ ฅ์์ ํฐ์ปค๋ฅผ ์ถ์ถํ๋ ๋ก์ง์ ๊ตฌํํด์ผ ํจ
|
106 |
+
# ์ฌ๊ธฐ์๋ ์์๋ก 'AAPL'์ ๋ฐํํ๋๋ก ์ค์
|
107 |
+
return "AAPL"
|
108 |
+
|
109 |
def validate_ticker(ticker):
|
110 |
logging.debug(f"Validating ticker: {ticker}")
|
111 |
stock = yf.Ticker(ticker)
|