|
import gradio as gr |
|
import torch |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
model_name = "kakaocorp/kanana-nano-2.1b-instruct" |
|
|
|
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_name, |
|
torch_dtype=torch.float32, |
|
trust_remote_code=True, |
|
) |
|
|
|
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() |
|
|