File size: 1,787 Bytes
3c321fe e3fff45 3c321fe e3fff45 2692d93 e3fff45 2692d93 e3fff45 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "kakaocorp/kanana-nano-2.1b-instruct"
# ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ CPU ํ๊ฒฝ์์ ๋ก๋ํฉ๋๋ค.
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float32, # CPU์์๋ bfloat16 ์ง์์ด ์ ํ๋ ์ ์์ผ๋ฏ๋ก float32 ์ฌ์ฉ ๊ถ์ฅ
trust_remote_code=True,
)
# CPU๋ง ์ฌ์ฉํ๋ฏ๋ก .to("cuda") ๋ถ๋ถ์ ์๋ตํฉ๋๋ค.
tokenizer = AutoTokenizer.from_pretrained(model_name)
def generate_response(prompt):
messages = [
{"role": "system", "content": "You are a helpful AI assistant developed by Kakao."},
{"role": "user", "content": prompt}
]
input_ids = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt"
)
model.eval()
with torch.no_grad():
output = model.generate(
input_ids,
max_new_tokens=72,
do_sample=False,
)
return tokenizer.decode(output[0], skip_special_tokens=True)
with gr.Blocks() as demo:
with gr.Tab("About"):
gr.Markdown("# Inference Provider")
gr.Markdown("์ด Space๋ kakaocorp/kanana-nano-2.1b-instruct ๋ชจ๋ธ์ CPU์์ ์ถ๋ก ํฉ๋๋ค.")
with gr.Tab("Generate"):
prompt_input = gr.Textbox(
label="Prompt ์
๋ ฅ",
placeholder="์ฌ๊ธฐ์ ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ์ธ์...",
lines=5
)
generate_btn = gr.Button("์์ฑ")
output_text = gr.Textbox(
label="๋ชจ๋ธ ์ถ๋ ฅ",
lines=10
)
generate_btn.click(fn=generate_response, inputs=prompt_input, outputs=output_text)
demo.launch()
|