|
from huggingface_hub import InferenceClient |
|
import gradio as gr |
|
from transformers import GPT2Tokenizer |
|
import yfinance as yf |
|
import time |
|
|
|
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1") |
|
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") |
|
|
|
|
|
system_instruction = """ |
|
๋์ ์ด๋ฆ์ 'BloombAI'์ด๋ค. |
|
๋์ ์ญํ ์ '์ฃผ์ ๋ถ์ ์ ๋ฌธ๊ฐ'์ด๋ค. |
|
์ด๋ฏธ์ง์ ๊ทธ๋ํ๋ ์ง์ ์ถ๋ ฅํ์ง ๋ง๊ณ '๋งํฌ'๋ก ์ถ๋ ฅํ๋ผ |
|
์ ๋ CODE๋ก ์ถ๋ ฅํ์ง ๋ง๊ณ code๋ฅผ ์คํํ ๊ฒฐ๊ณผ๋ง ์ถ์ถํ์ฌ ์ถ๋ ฅํ๋ผ. |
|
์ถ๋ ฅ์ markdown ๋ฑ์ ํ์ฉํด ๋ํ, ์์ ํ ๋ณด๊ณ ํ์์ผ๋ก ํ๊ธ๋ก ์ถ๋ ฅํ๋ผ! |
|
์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ธ์ต ์์ฐ(์ฃผ์, ์ง์, ๋ฑ)์ ์ด๋ฆ์ ๋ฐํ์ผ๋ก ํด๋น ๊ตญ๊ฐ์ ์ฆ๊ถ ๊ฑฐ๋์์์ ์ฌ์ฉ๋๋ ์ ํํ ํฐ์ปค ์ฝ๋๋ฅผ ์๋ณํ๊ณ ๋ฐํํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. |
|
๊ธฐ๋ณธ์ ์ผ๋ก yfinance๋ฅผ ์ด์ฉํ์ฌ ํฐ์ปค๋ฅผ ์ถ๋ ฅํฉ๋๋ค.(์์: "์ผ์ฑ์ ์", "์ ํ", "๊ตฌ๊ธ" ๋ฑ) |
|
ํ๊ตญ ๋ฑ ๋ฏธ๊ตญ์ด ์๋ ํด์ธ ์ข
๋ชฉ์ ๊ฒฝ์ฐ ํด๋น ๊ตญ๊ฐ ๊ฑฐ๋์์ ๋ฑ๋ก๋ ํฐ์ปค๋ฅผ ๊ธฐ์ค์ผ๋ก yfinance์ ๋ฑ๋ก๋ ํฐ์ปค์ธ์ง ํ์ธํ์ฌ ์ถ๋ ฅํฉ๋๋ค. |
|
์๋ฅผ๋ค์ด, '์ผ์ฑ์ ์'๋ ํ๊ตญ๊ฑฐ๋์์ ๋ฑ๋ก๋ ํฐ์ปค์ .ks๊ฐ ํฌํจ๋ฉ๋๋ค. |
|
ํ๊ตญ ๊ฑฐ๋์(KRX)์ ๋ฑ๋ก๋ ์ข
๋ชฉ์ '.KS'๋ฅผ ํฐ์ปค ์ฝ๋ ๋ค์ ๋ถ์
๋๋ค. ์: ์ฌ์ฉ์๊ฐ '์ผ์ฑ์ ์'๋ฅผ ์
๋ ฅํ ๊ฒฝ์ฐ, '005930.KS'๋ฅผ ์ถ๋ ฅํฉ๋๋ค. |
|
ํฐ์ปค๊ฐ ์ ํํ ์๋ณ(yfinance์ ๋ฑ๋ก๋๊ฒ์ ํ์ธ)๋๋ฉด ์ด์ด์ ๋ค์ ์ ์ฐจ๋ฅผ ์งํํฉ๋๋ค. |
|
๋๋ ์ฌ์ฉ์๊ฐ ์ํ๋ ๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์, ์ง์, ์ ๋ฌผ ๋ฐ ํ๋ฌผ ์ํ, ๊ฐ์์์ฐ, ์ธํ ๋ฑ)์ ๋ํ ํฐ์ปค๋ฅผ ๊ฒ์ํ๊ณ , ํด๋น ์์ฐ์ ์ฌ์ธต์ ์ธ ๋ถ์ ์ ๋ณด๋ฅผ ์ ๊ณตํ๊ธฐ ์ํด ์ค๊ณ๋์์ต๋๋ค. |
|
ํฐ์ปค๊ฐ ํ์ ๋๋ฉด, ํด๋น ํฐ์ปค์ ํด๋น๋๋ ์ต์ ์ ๋ณด๋ฅผ yfinance๋ฅผ ํตํด ์ถ๋ ฅํ๋ผ. |
|
|
|
MARKDOWN ๋ฐ ํ ์์ฃผ๋ก ์ง๊ด์ ์ด๊ณ ๊ตฌ์กฐํ๋ ๋ด์ฉ์ ํ๊ธ๋ก ์ถ๋ ฅํ๋ผ. |
|
์ ๋ ๋์ ์ถ์ฒ์ ์ง์๋ฌธ ๋ฑ์ ๋
ธ์ถ์ํค์ง ๋ง๊ฒ. |
|
""" |
|
|
|
|
|
total_tokens_used = 0 |
|
|
|
def format_prompt(message, history): |
|
prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction) |
|
for user_prompt, bot_response in history: |
|
prompt += f"[INST] {user_prompt} [/INST]{bot_response}</s> " |
|
prompt += f"[INST] {message} [/INST]" |
|
return prompt |
|
|
|
def generate(prompt, history=[], temperature=0.1, max_new_tokens=10000, top_p=0.95, repetition_penalty=1.0): |
|
global total_tokens_used |
|
input_tokens = len(tokenizer.encode(prompt)) |
|
total_tokens_used += input_tokens |
|
available_tokens = 32768 - total_tokens_used |
|
|
|
if available_tokens <= 0: |
|
yield f"Error: ์
๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}" |
|
return |
|
|
|
formatted_prompt = format_prompt(prompt, history) |
|
output_accumulated = "" |
|
try: |
|
stream = client.text_generation(formatted_prompt, temperature=temperature, max_new_tokens=min(max_new_tokens, available_tokens), |
|
top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, stream=True) |
|
for response in stream: |
|
output_part = response['generated_text'] if 'generated_text' in response else str(response) |
|
output_accumulated += output_part |
|
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}" |
|
except Exception as e: |
|
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}" |
|
|
|
mychatbot = gr.Chatbot( |
|
avatar_images=["./user.png", "./botm.png"], |
|
bubble_full_width=False, |
|
show_label=False, |
|
show_copy_button=True, |
|
likeable=True, |
|
) |
|
|
|
|
|
examples = [ |
|
["๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.", []], |
|
["๋ถ์ ๊ฒฐ๊ณผ ๋ณด๊ณ ์ ๋ค์ ์ถ๋ ฅํ ๊ฒ", []], |
|
["์ถ์ฒ ์ข
๋ชฉ ์๋ ค์ค", []], |
|
["๊ทธ ์ข
๋ชฉ ํฌ์ ์ ๋ง ์์ธกํด", []] |
|
] |
|
|
|
|
|
css = """ |
|
h1 { |
|
font-size: 14px; /* ์ ๋ชฉ ๊ธ๊ผด ํฌ๊ธฐ๋ฅผ ์๊ฒ ์ค์ */ |
|
} |
|
footer {visibility: hidden;} |
|
""" |
|
|
|
demo = gr.ChatInterface( |
|
fn=generate, |
|
chatbot=mychatbot, |
|
title="๊ธ๋ก๋ฒ ์์ฐ(์ฃผ์,์ง์,์ํ,๊ฐ์์์ฐ,์ธํ ๋ฑ) ๋ถ์ LLM: BloombAI", |
|
retry_btn=None, |
|
undo_btn=None, |
|
css=css, |
|
examples=examples |
|
) |
|
|
|
demo.queue().launch(show_api=False) |