greatgod commited on
Commit
e3fff45
ยท
verified ยท
1 Parent(s): 2692d93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -6
app.py CHANGED
@@ -1,10 +1,55 @@
1
  import gradio as gr
 
 
2
 
3
- with gr.Blocks(fill_height=True) as demo:
4
- with gr.Sidebar():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  gr.Markdown("# Inference Provider")
6
- gr.Markdown("This Space showcases the kakaocorp/kanana-nano-2.1b-instruct model, served by the hf-inference API. Sign in with your Hugging Face account to use this API.")
7
- button = gr.LoginButton("Sign in")
8
- gr.load("models/kakaocorp/kanana-nano-2.1b-instruct", accept_token=button, provider="hf-inference")
9
 
10
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()