Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,7 +21,6 @@ system_instruction = """
|
|
21 |
# ๋์ ํ ํฐ ์ฌ์ฉ๋์ ์ถ์ ํ๋ ์ ์ญ ๋ณ์
|
22 |
total_tokens_used = 0
|
23 |
|
24 |
-
|
25 |
def fetch_ticker_info(ticker):
|
26 |
stock = yf.Ticker(ticker)
|
27 |
try:
|
@@ -43,7 +42,7 @@ def fetch_ticker_info(ticker):
|
|
43 |
return "\n".join([f"{key}: {value}" for key, value in result.items() if value is not None])
|
44 |
except ValueError:
|
45 |
return "์ ํจํ์ง ์์ ํฐ์ปค์
๋๋ค. ๋ค์ ์
๋ ฅํด์ฃผ์ธ์."
|
46 |
-
|
47 |
def format_prompt(message, history):
|
48 |
prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction)
|
49 |
for user_prompt, bot_response in history:
|
@@ -56,10 +55,8 @@ def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.
|
|
56 |
input_tokens = len(tokenizer.encode(prompt))
|
57 |
total_tokens_used += input_tokens
|
58 |
available_tokens = 32768 - total_tokens_used
|
59 |
-
|
60 |
if available_tokens <= 0:
|
61 |
-
|
62 |
-
return
|
63 |
|
64 |
formatted_prompt = format_prompt(prompt, history)
|
65 |
output_accumulated = ""
|
@@ -69,44 +66,53 @@ def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.
|
|
69 |
for response in stream:
|
70 |
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
71 |
output_accumulated += output_part
|
72 |
-
|
73 |
except Exception as e:
|
74 |
-
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
info = fetch_ticker_info(ticker)
|
99 |
-
return [("bot", f"ํฐ์ปค '{ticker}'์ ์ ๋ณด ์กฐํ ๊ฒฐ๊ณผ:\n\n{info}")]
|
100 |
-
|
101 |
-
submit_button.click(
|
102 |
-
fn=query_and_show,
|
103 |
-
inputs=ticker_input,
|
104 |
-
outputs=chatbot
|
105 |
-
)
|
106 |
-
|
107 |
-
gr.Markdown("### ์ฑํ
")
|
108 |
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
-
|
112 |
-
app.launch()
|
|
|
21 |
# ๋์ ํ ํฐ ์ฌ์ฉ๋์ ์ถ์ ํ๋ ์ ์ญ ๋ณ์
|
22 |
total_tokens_used = 0
|
23 |
|
|
|
24 |
def fetch_ticker_info(ticker):
|
25 |
stock = yf.Ticker(ticker)
|
26 |
try:
|
|
|
42 |
return "\n".join([f"{key}: {value}" for key, value in result.items() if value is not None])
|
43 |
except ValueError:
|
44 |
return "์ ํจํ์ง ์์ ํฐ์ปค์
๋๋ค. ๋ค์ ์
๋ ฅํด์ฃผ์ธ์."
|
45 |
+
|
46 |
def format_prompt(message, history):
|
47 |
prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction)
|
48 |
for user_prompt, bot_response in history:
|
|
|
55 |
input_tokens = len(tokenizer.encode(prompt))
|
56 |
total_tokens_used += input_tokens
|
57 |
available_tokens = 32768 - total_tokens_used
|
|
|
58 |
if available_tokens <= 0:
|
59 |
+
return f"Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}"
|
|
|
60 |
|
61 |
formatted_prompt = format_prompt(prompt, history)
|
62 |
output_accumulated = ""
|
|
|
66 |
for response in stream:
|
67 |
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
68 |
output_accumulated += output_part
|
69 |
+
return output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}"
|
70 |
except Exception as e:
|
71 |
+
return f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
72 |
|
73 |
+
def postprocess(history):
|
74 |
+
user_prompt = history[-1][0]
|
75 |
+
bot_response = history[-1][1]
|
76 |
+
|
77 |
+
# ํฐ์ปค ์ ๋ณด ์ถ๋ ฅ
|
78 |
+
if "ํฐ์ปค" in bot_response:
|
79 |
+
ticker = bot_response.split("ํฐ์ปค")[1].strip()
|
80 |
+
ticker_info = fetch_ticker_info(ticker)
|
81 |
+
bot_response += f"\n\nํฐ์ปค ์ ๋ณด:\n{ticker_info}"
|
82 |
+
|
83 |
+
return [(user_prompt, bot_response)]
|
84 |
|
85 |
+
mychatbot = gr.Chatbot(
|
86 |
+
avatar_images=["./user.png", "./botm.png"],
|
87 |
+
bubble_full_width=False,
|
88 |
+
show_label=False,
|
89 |
+
show_copy_button=True,
|
90 |
+
likeable=True
|
91 |
+
)
|
92 |
+
|
93 |
+
examples = [
|
94 |
+
["๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.", []], # history ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ ๊ณต
|
95 |
+
["๋ถ์ ๊ฒฐ๊ณผ ๋ณด๊ณ ์ ๋ค์ ์ถ๋ ฅํ ๊ฒ", []],
|
96 |
+
["์ถ์ฒ ์ข
๋ชฉ ์๋ ค์ค", []],
|
97 |
+
["๊ทธ ์ข
๋ชฉ ํฌ์ ์ ๋ง ์์ธกํด", []]
|
98 |
+
]
|
99 |
+
|
100 |
+
css = """
|
101 |
+
h1 {
|
102 |
+
font-size: 14px; /* ์ ๋ชฉ ๊ธ๊ผด ํฌ๊ธฐ๋ฅผ ์๊ฒ ์ค์ */
|
103 |
+
}
|
104 |
+
footer {visibility: hidden;}
|
105 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
demo = gr.ChatInterface(
|
108 |
+
fn=generate,
|
109 |
+
chatbot=mychatbot,
|
110 |
+
title="๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์,์ง์,์ํ,๊ฐ์์์ฐ,์ธํ ๋ฑ) ๋ถ์ LLM: BloombAI",
|
111 |
+
retry_btn=None,
|
112 |
+
undo_btn=None,
|
113 |
+
css=css,
|
114 |
+
examples=examples,
|
115 |
+
postprocess=postprocess
|
116 |
+
)
|
117 |
|
118 |
+
demo.queue().launch(show_api=False)
|
|