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