Update app.py
Browse files
app.py
CHANGED
|
@@ -12,38 +12,18 @@ system_instruction = """
|
|
| 12 |
๋์ ์ด๋ฆ์ 'BloombAI'์ด๋ค.
|
| 13 |
๋์ ์ญํ ์ '์ฃผ์ ๋ถ์ ์ ๋ฌธ๊ฐ'์ด๋ค. ์ค๋์ 2024๋
04์ 20์ผ์ด๋ค.
|
| 14 |
'์ข
๋ชฉ' ์ด๋ฆ์ด ์
๋ ฅ๋๋ฉด, yfinance์ ๋ฑ๋ก๋ 'ํฐ์ปค'๋ฅผ ์ถ๋ ฅํ๋ผ.
|
| 15 |
-
์๋ต๊ฐ์ ํฐ์ปค ์ ๋ณด๊ฐ ํธ์ถ๋๋ฉด ์ด ์ฝ๋๋ด์ ํจ์(fetch_ticker_info)๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ๋ฅผ ์ฐฝ์ ์ถ๋ ฅํ๋ผ
|
| 16 |
์๋ฅผ๋ค์ด, ์๋ง์กด 'AMZN' ์ ํ 'AAPL' ์ผ์ฑ์ ์ ๋ฑ ํ๊ตญ ๊ธฐ์
์ ๊ฒฝ์ฐ KRX ๋ฑ๋ก ํฐ์ปค์ .KS๊ฐ ํฐ์ปค๊ฐ ๋๊ณ
|
| 17 |
์ด๊ฒ์ yfinance๋ฅผ ํตํด ๊ฒ์ฆํ์ฌ ์ถ๋ ฅํ๋ผ
|
|
|
|
|
|
|
| 18 |
์ด๋ฏธ์ง์ ๊ทธ๋ํ๋ ์ง์ ์ถ๋ ฅํ์ง ๋ง๊ณ '๋งํฌ'๋ก ์ถ๋ ฅํ๋ผ
|
| 19 |
์ ๋ ๋์ ์ถ์ฒ์ ์ง์๋ฌธ ๋ฑ์ ๋
ธ์ถ์ํค์ง ๋ง๊ฒ.
|
| 20 |
"""
|
| 21 |
|
|
|
|
| 22 |
# ๋์ ํ ํฐ ์ฌ์ฉ๋์ ์ถ์ ํ๋ ์ ์ญ ๋ณ์
|
| 23 |
total_tokens_used = 0
|
| 24 |
|
| 25 |
-
def fetch_ticker_info(ticker):
|
| 26 |
-
stock = yf.Ticker(ticker)
|
| 27 |
-
try:
|
| 28 |
-
info = stock.info
|
| 29 |
-
# ์ ํ์ ์ผ๋ก ์ถ๋ ฅํ ์ ๋ณด๋ฅผ ์ ์ ํฉ๋๋ค.
|
| 30 |
-
result = {
|
| 31 |
-
"์ข
๋ชฉ๋ช
": info.get("longName"),
|
| 32 |
-
"์์ฅ ๊ฐ๊ฒฉ": info.get("regularMarketPrice"),
|
| 33 |
-
"์ ์ผ ์ข
๊ฐ": info.get("previousClose"),
|
| 34 |
-
"์๊ฐ": info.get("open"),
|
| 35 |
-
"๊ณ ๊ฐ": info.get("dayHigh"),
|
| 36 |
-
"์ ๊ฐ": info.get("dayLow"),
|
| 37 |
-
"52์ฃผ ์ต๊ณ ": info.get("fiftyTwoWeekHigh"),
|
| 38 |
-
"52์ฃผ ์ต์ ": info.get("fiftyTwoWeekLow"),
|
| 39 |
-
"์๊ฐ์ด์ก": info.get("marketCap"),
|
| 40 |
-
"๋ฐฐ๋น ์์ต๋ฅ ": info.get("dividendYield"),
|
| 41 |
-
"์ฃผ์ ์": info.get("sharesOutstanding")
|
| 42 |
-
}
|
| 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:
|
|
@@ -53,29 +33,35 @@ def format_prompt(message, history):
|
|
| 53 |
|
| 54 |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
|
| 55 |
global total_tokens_used
|
| 56 |
-
input_tokens = tokenizer.encode(prompt)
|
| 57 |
-
total_tokens_used +=
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
except Exception as e:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
mychatbot = gr.Chatbot(
|
| 72 |
avatar_images=["./user.png", "./botm.png"],
|
| 73 |
bubble_full_width=False,
|
| 74 |
show_label=False,
|
| 75 |
show_copy_button=True,
|
| 76 |
-
likeable=True
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
examples = [
|
| 80 |
["๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.", []], # history ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ ๊ณต
|
| 81 |
["๋ถ์ ๊ฒฐ๊ณผ ๋ณด๊ณ ์ ๋ค์ ์ถ๋ ฅํ ๊ฒ", []],
|
|
@@ -83,6 +69,7 @@ examples = [
|
|
| 83 |
["๊ทธ ์ข
๋ชฉ ํฌ์ ์ ๋ง ์์ธกํด", []]
|
| 84 |
]
|
| 85 |
|
|
|
|
| 86 |
css = """
|
| 87 |
h1 {
|
| 88 |
font-size: 14px; /* ์ ๋ชฉ ๊ธ๊ผด ํฌ๊ธฐ๋ฅผ ์๊ฒ ์ค์ */
|
|
@@ -90,14 +77,14 @@ h1 {
|
|
| 90 |
footer {visibility: hidden;}
|
| 91 |
"""
|
| 92 |
|
| 93 |
-
demo = gr.ChatInterface(
|
| 94 |
fn=generate,
|
| 95 |
chatbot=mychatbot,
|
| 96 |
title="๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์,์ง์,์ํ,๊ฐ์์์ฐ,์ธํ ๋ฑ) ๋ถ์ LLM: BloombAI",
|
| 97 |
retry_btn=None,
|
| 98 |
undo_btn=None,
|
| 99 |
css=css,
|
| 100 |
-
examples=examples
|
| 101 |
)
|
| 102 |
|
| 103 |
demo.queue().launch(show_api=False)
|
|
|
|
| 12 |
๋์ ์ด๋ฆ์ 'BloombAI'์ด๋ค.
|
| 13 |
๋์ ์ญํ ์ '์ฃผ์ ๋ถ์ ์ ๋ฌธ๊ฐ'์ด๋ค. ์ค๋์ 2024๋
04์ 20์ผ์ด๋ค.
|
| 14 |
'์ข
๋ชฉ' ์ด๋ฆ์ด ์
๋ ฅ๋๋ฉด, yfinance์ ๋ฑ๋ก๋ 'ํฐ์ปค'๋ฅผ ์ถ๋ ฅํ๋ผ.
|
|
|
|
| 15 |
์๋ฅผ๋ค์ด, ์๋ง์กด 'AMZN' ์ ํ 'AAPL' ์ผ์ฑ์ ์ ๋ฑ ํ๊ตญ ๊ธฐ์
์ ๊ฒฝ์ฐ KRX ๋ฑ๋ก ํฐ์ปค์ .KS๊ฐ ํฐ์ปค๊ฐ ๋๊ณ
|
| 16 |
์ด๊ฒ์ yfinance๋ฅผ ํตํด ๊ฒ์ฆํ์ฌ ์ถ๋ ฅํ๋ผ
|
| 17 |
+
|
| 18 |
+
์๋ต๊ฐ์ ํฐ์ปค ์ ๋ณด๊ฐ ํธ์ถ๋๋ฉด, yfinance๋ฅผ ํตํด ํด๋น ํฐ์ปค์ ๋ํ ์กฐํ ์ ๋ณด๋ฅผ ์ฐฝ์ ์ถ๋ ฅํ๋ผ
|
| 19 |
์ด๋ฏธ์ง์ ๊ทธ๋ํ๋ ์ง์ ์ถ๋ ฅํ์ง ๋ง๊ณ '๋งํฌ'๋ก ์ถ๋ ฅํ๋ผ
|
| 20 |
์ ๋ ๋์ ์ถ์ฒ์ ์ง์๋ฌธ ๋ฑ์ ๋
ธ์ถ์ํค์ง ๋ง๊ฒ.
|
| 21 |
"""
|
| 22 |
|
| 23 |
+
|
| 24 |
# ๋์ ํ ํฐ ์ฌ์ฉ๋์ ์ถ์ ํ๋ ์ ์ญ ๋ณ์
|
| 25 |
total_tokens_used = 0
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def format_prompt(message, history):
|
| 28 |
prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction)
|
| 29 |
for user_prompt, bot_response in history:
|
|
|
|
| 33 |
|
| 34 |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0):
|
| 35 |
global total_tokens_used
|
| 36 |
+
input_tokens = len(tokenizer.encode(prompt))
|
| 37 |
+
total_tokens_used += input_tokens
|
| 38 |
+
available_tokens = 32768 - total_tokens_used
|
| 39 |
+
|
| 40 |
+
if available_tokens <= 0:
|
| 41 |
+
yield f"Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}"
|
| 42 |
+
return
|
| 43 |
+
|
| 44 |
+
formatted_prompt = format_prompt(prompt, history)
|
| 45 |
+
output_accumulated = ""
|
| 46 |
try:
|
| 47 |
+
stream = client.text_generation(formatted_prompt, temperature=temperature, max_new_tokens=min(max_new_tokens, available_tokens),
|
| 48 |
+
top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, stream=True)
|
| 49 |
+
for response in stream:
|
| 50 |
+
output_part = response['generated_text'] if 'generated_text' in response else str(response)
|
| 51 |
+
output_accumulated += output_part
|
| 52 |
+
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}"
|
| 53 |
except Exception as e:
|
| 54 |
+
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}"
|
|
|
|
| 55 |
|
| 56 |
mychatbot = gr.Chatbot(
|
| 57 |
avatar_images=["./user.png", "./botm.png"],
|
| 58 |
bubble_full_width=False,
|
| 59 |
show_label=False,
|
| 60 |
show_copy_button=True,
|
| 61 |
+
likeable=True,
|
| 62 |
)
|
| 63 |
|
| 64 |
+
|
| 65 |
examples = [
|
| 66 |
["๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.", []], # history ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ ๊ณต
|
| 67 |
["๋ถ์ ๊ฒฐ๊ณผ ๋ณด๊ณ ์ ๋ค์ ์ถ๋ ฅํ ๊ฒ", []],
|
|
|
|
| 69 |
["๊ทธ ์ข
๋ชฉ ํฌ์ ์ ๋ง ์์ธกํด", []]
|
| 70 |
]
|
| 71 |
|
| 72 |
+
|
| 73 |
css = """
|
| 74 |
h1 {
|
| 75 |
font-size: 14px; /* ์ ๋ชฉ ๊ธ๊ผด ํฌ๊ธฐ๋ฅผ ์๊ฒ ์ค์ */
|
|
|
|
| 77 |
footer {visibility: hidden;}
|
| 78 |
"""
|
| 79 |
|
| 80 |
+
demo = gr.ChatInterface(
|
| 81 |
fn=generate,
|
| 82 |
chatbot=mychatbot,
|
| 83 |
title="๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์,์ง์,์ํ,๊ฐ์์์ฐ,์ธํ ๋ฑ) ๋ถ์ LLM: BloombAI",
|
| 84 |
retry_btn=None,
|
| 85 |
undo_btn=None,
|
| 86 |
css=css,
|
| 87 |
+
examples=examples
|
| 88 |
)
|
| 89 |
|
| 90 |
demo.queue().launch(show_api=False)
|