Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,137 +41,68 @@ The user provided the additional info about how they would like you to respond:
|
|
41 |
total_tokens_used = 0
|
42 |
|
43 |
def format_prompt(message, history):
|
44 |
-
prompt = "<s>[SYSTEM] {} [/SYSTEM]"
|
45 |
for user_prompt, bot_response in history:
|
46 |
prompt += f"[INST] {user_prompt} [/INST]{bot_response}</s> "
|
47 |
prompt += f"[INST] {message} [/INST]"
|
48 |
return prompt
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
def get_stock_data(ticker):
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
|
56 |
global total_tokens_used
|
57 |
-
input_tokens = len(tokenizer.encode(prompt))
|
58 |
-
total_tokens_used += input_tokens
|
59 |
-
available_tokens = 32768 - total_tokens_used
|
60 |
-
if available_tokens <= 0:
|
61 |
-
yield f"Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}"
|
62 |
-
return
|
63 |
-
|
64 |
-
formatted_prompt = format_prompt(prompt, history)
|
65 |
-
output_accumulated = ""
|
66 |
try:
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
yield output_accumulated
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
repetition_penalty=repetition_penalty,
|
82 |
-
do_sample=True,
|
83 |
-
seed=42,
|
84 |
-
stream=True
|
85 |
-
)
|
86 |
-
for response in stream:
|
87 |
-
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
88 |
-
output_accumulated += output_part
|
89 |
-
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}\nStock Data: {stock_data}"
|
90 |
else:
|
91 |
-
|
92 |
-
ticker = prompt.upper()
|
93 |
-
if ticker in ['AAPL', 'MSFT', 'AMZN', 'GOOGL', 'TSLA']:
|
94 |
-
stock_info = get_stock_info_by_ticker(ticker)
|
95 |
-
response_msg = f"{stock_info['name']}์(๋) {stock_info['description']} ์ฃผ๋ ฅ์ผ๋ก ์์ฐํ๋ ๊ธฐ์
์
๋๋ค. {stock_info['name']}์ ํฐ์ปค๋ {stock_info['ticker']}์
๋๋ค. ์ํ์๋ ์ข
๋ชฉ์ด ๋ง๋๊ฐ์?"
|
96 |
-
output_accumulated += response_msg
|
97 |
-
yield output_accumulated
|
98 |
-
|
99 |
-
# ์ถ๊ฐ์ ์ธ ๋ถ์ ์์ฒญ์ด ์๋ค๋ฉด, yfinance๋ก ๋ฐ์ดํฐ ์์ง ๋ฐ ๋ถ์
|
100 |
-
stock_data = get_stock_data(stock_info['ticker']) # ํฐ์ปค๋ฅผ ์ด์ฉํด ์ฃผ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
|
101 |
-
stream = client.text_generation(
|
102 |
-
formatted_prompt,
|
103 |
-
temperature=temperature,
|
104 |
-
max_new_tokens=min(max_new_tokens, available_tokens),
|
105 |
-
top_p=top_p,
|
106 |
-
repetition_penalty=repetition_penalty,
|
107 |
-
do_sample=True,
|
108 |
-
seed=42,
|
109 |
-
stream=True
|
110 |
-
)
|
111 |
-
for response in stream:
|
112 |
-
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
113 |
-
output_accumulated += output_part
|
114 |
-
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}\nStock Data: {stock_data}"
|
115 |
-
else:
|
116 |
-
yield f"์
๋ ฅํ์ '{prompt}'์(๋) ์ง์๋๋ ์ข
๋ชฉ๋ช
๋๋ ํฐ์ปค๊ฐ ์๋๋๋ค. ํ์ฌ ์ง์๋๋ ์ข
๋ชฉ์ ์ ํ(AAPL), ๋ง์ดํฌ๋ก์ํํธ(MSFT), ์๋ง์กด(AMZN), ์ํ๋ฒณ(GOOGL), ํ
์ฌ๋ผ(TSLA) ๋ฑ์
๋๋ค. ์ ํํ ์ข
๋ชฉ๋ช
๋๋ ํฐ์ปค๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
117 |
except Exception as e:
|
118 |
-
|
119 |
|
120 |
-
# ํฐ์ปค๋ฅผ ํ ๋๋ก ์ข
๋ชฉ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ํจ์
|
121 |
-
def get_stock_info_by_ticker(ticker):
|
122 |
-
stock_info = {
|
123 |
-
"AAPL": {'ticker': 'AAPL', 'name': '์ ํ', 'description': '์์ดํฐ์'},
|
124 |
-
"MSFT": {'ticker': 'MSFT', 'name': '๋ง์ดํฌ๋ก์ํํธ', 'description': '์๋์ฐ ์ด์์ฒด์ ์ ์คํผ์ค ์ํํธ์จ์ด๋ฅผ'},
|
125 |
-
"AMZN": {'ticker': 'AMZN', 'name': '์๋ง์กด', 'description': '์ ์์๊ฑฐ๋ ๋ฐ ํด๋ผ์ฐ๋ ์๋น์ค๋ฅผ'},
|
126 |
-
"GOOGL": {'ticker': 'GOOGL', 'name': '์ํ๋ฒณ', 'description': '๊ฒ์ ์์ง ๋ฐ ์จ๋ผ์ธ ๊ด๊ณ ๋ฅผ'},
|
127 |
-
"TSLA": {'ticker': 'TSLA', 'name': 'ํ
์ฌ๋ผ', 'description': '์ ๊ธฐ์๋์ฐจ์ ์๋์ง ์ ์ฅ์ฅ์น๋ฅผ'},
|
128 |
-
}
|
129 |
-
return stock_info.get(ticker, {'ticker': None, 'name': None, 'description': ''})
|
130 |
-
|
131 |
-
# ์ข
๋ชฉ๋ช
์ ํ ๋๋ก ํฐ์ปค์ ๊ธฐ์
์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ํจ์
|
132 |
-
def get_stock_info(name):
|
133 |
-
stock_info = {
|
134 |
-
"apple": {'ticker': 'AAPL', 'name': '์ ํ', 'description': '์์ดํฐ์'},
|
135 |
-
"microsoft": {'ticker': 'MSFT', 'name': '๋ง์ดํฌ๋ก์ํํธ', 'description': '์๋์ฐ ์ด์์ฒด์ ์ ์คํผ์ค ์ํํธ์จ์ด๋ฅผ'},
|
136 |
-
"amazon": {'ticker': 'AMZN', 'name': '์๋ง์กด', 'description': '์ ์์๊ฑฐ๋ ๋ฐ ํด๋ผ์ฐ๋ ์๋น์ค๋ฅผ'},
|
137 |
-
"google": {'ticker': 'GOOGL', 'name': '์ํ๋ฒณ (๊ตฌ๊ธ)', 'description': '๊ฒ์ ์์ง ๋ฐ ์จ๋ผ์ธ ๊ด๊ณ ๋ฅผ'},
|
138 |
-
"tesla": {'ticker': 'TSLA', 'name': 'ํ
์ฌ๋ผ', 'description': '์ ๊ธฐ์๋์ฐจ์ ์๋์ง ์ ์ฅ์ฅ์น๋ฅผ'},
|
139 |
-
# ์ถ๊ฐ์ ์ธ ์ข
๋ชฉ์ ๋ํ ์ ๋ณด๋ฅผ ์ด๊ณณ์ ๊ตฌํํ ์ ์์ต๋๋ค.
|
140 |
-
}
|
141 |
-
return stock_info.get(name.lower(), {'ticker': None, 'name': name, 'description': ''})
|
142 |
-
|
143 |
mychatbot = gr.Chatbot(
|
144 |
avatar_images=["./user.png", "./botm.png"],
|
145 |
bubble_full_width=False,
|
146 |
show_label=False,
|
147 |
show_copy_button=True,
|
148 |
likeable=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
)
|
150 |
|
151 |
-
examples = [
|
152 |
-
["๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.", []],
|
153 |
-
["์ข์ ์ข
๋ชฉ(ํฐ์ปค) ์ถ์ฒํด์ค", []],
|
154 |
-
["์์ฝ ๊ฒฐ๋ก ์ ์ ์ํด", []],
|
155 |
-
["ํฌํธํด๋ฆฌ์ค ๋ถ์ํด์ค", []]
|
156 |
-
]
|
157 |
-
|
158 |
-
css = """
|
159 |
-
h1 {
|
160 |
-
font-size: 14px;
|
161 |
-
}
|
162 |
-
footer {
|
163 |
-
visibility: hidden;
|
164 |
-
}
|
165 |
-
"""
|
166 |
-
|
167 |
demo = gr.ChatInterface(
|
168 |
fn=generate,
|
169 |
chatbot=mychatbot,
|
170 |
title="๊ธ๋ก๋ฒ ์์ฐ ๋ถ์ ๋ฐ ์์ธก LLM: BloombAI",
|
171 |
-
|
172 |
-
undo_btn=None,
|
173 |
-
css=css,
|
174 |
-
examples=examples
|
175 |
)
|
176 |
|
177 |
-
demo.
|
|
|
41 |
total_tokens_used = 0
|
42 |
|
43 |
def format_prompt(message, history):
|
44 |
+
prompt = f"<s>[SYSTEM] {system_instruction} [/SYSTEM]"
|
45 |
for user_prompt, bot_response in history:
|
46 |
prompt += f"[INST] {user_prompt} [/INST]{bot_response}</s> "
|
47 |
prompt += f"[INST] {message} [/INST]"
|
48 |
return prompt
|
49 |
|
50 |
+
stock_info = {
|
51 |
+
"AAPL": {'name': '์ ํ', 'description': '์์ดํฐ์ ์ฃผ๋ ฅ์ผ๋ก ์์ฐํ๋'},
|
52 |
+
"MSFT": {'name': '๋ง์ดํฌ๋ก์ํํธ', 'description': '์๋์ฐ ์ด์์ฒด์ ์ ์คํผ์ค ์ํํธ์จ์ด๋ฅผ'},
|
53 |
+
"AMZN": {'name': '์๋ง์กด', 'description': '์ ์์๊ฑฐ๋ ๋ฐ ํด๋ผ์ฐ๋ ์๋น์ค๋ฅผ'},
|
54 |
+
"GOOGL": {'name': '์ํ๋ฒณ (๊ตฌ๊ธ)', 'description': '๊ฒ์ ์์ง ๋ฐ ์จ๋ผ์ธ ๊ด๊ณ ๋ฅผ'},
|
55 |
+
"TSLA": {'name': 'ํ
์ฌ๋ผ', 'description': '์ ๊ธฐ์๋์ฐจ์ ์๋์ง ์ ์ฅ์ฅ์น๋ฅผ'}
|
56 |
+
}
|
57 |
+
|
58 |
def get_stock_data(ticker):
|
59 |
+
try:
|
60 |
+
stock = yf.Ticker(ticker)
|
61 |
+
hist = stock.history(period="5d")
|
62 |
+
return hist
|
63 |
+
except Exception as e:
|
64 |
+
return f"๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
65 |
|
66 |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
|
67 |
global total_tokens_used
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
try:
|
69 |
+
input_tokens = len(tokenizer.encode(prompt))
|
70 |
+
total_tokens_used += input_tokens
|
71 |
+
available_tokens = 32768 - total_tokens_used
|
72 |
+
if available_tokens <= 0:
|
73 |
+
return "Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค."
|
|
|
74 |
|
75 |
+
formatted_prompt = format_prompt(prompt, history)
|
76 |
+
ticker = prompt.upper()
|
77 |
+
stock_info_detail = stock_info.get(ticker, None)
|
78 |
+
if stock_info_detail:
|
79 |
+
response_msg = f"{stock_info_detail['name']}์(๋) {stock_info_detail['description']} ์ฃผ๋ ฅ์ผ๋ก ์์ฐํ๋ ๊ธฐ์
์
๋๋ค. ํฐ์ปค๋ {ticker}์
๋๋ค. ์ํ์๋ ์ข
๋ชฉ์ด ๋ง๋๊ฐ์?"
|
80 |
+
stock_data = get_stock_data(ticker)
|
81 |
+
return f"{response_msg}\n\n---\nTotal tokens used: {total_tokens_used}\nStock Data: {stock_data}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
else:
|
83 |
+
return f"์
๋ ฅํ์ '{prompt}'์(๋) ์ง์๋๋ ์ข
๋ชฉ๋ช
๋๋ ํฐ์ปค๊ฐ ์๋๋๋ค. ์ง์๋๋ ํฐ์ปค: {', '.join(stock_info.keys())}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
except Exception as e:
|
85 |
+
return f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
mychatbot = gr.Chatbot(
|
88 |
avatar_images=["./user.png", "./botm.png"],
|
89 |
bubble_full_width=False,
|
90 |
show_label=False,
|
91 |
show_copy_button=True,
|
92 |
likeable=True,
|
93 |
+
examples=[
|
94 |
+
["๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.", []],
|
95 |
+
["์ข์ ์ข
๋ชฉ(ํฐ์ปค) ์ถ์ฒํด์ค", []],
|
96 |
+
["์์ฝ ๊ฒฐ๋ก ์ ์ ์ํด", []],
|
97 |
+
["ํฌํธํด๋ฆฌ์ค ๋ถ์ํด์ค", []]
|
98 |
+
]
|
99 |
)
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
demo = gr.ChatInterface(
|
102 |
fn=generate,
|
103 |
chatbot=mychatbot,
|
104 |
title="๊ธ๋ก๋ฒ ์์ฐ ๋ถ์ ๋ฐ ์์ธก LLM: BloombAI",
|
105 |
+
css="h1 { font-size: 14px; } footer { visibility: hidden; }"
|
|
|
|
|
|
|
106 |
)
|
107 |
|
108 |
+
demo.launch(show_api=False)
|