r0mi commited on
Commit
7c327a1
·
1 Parent(s): a8e5e13

Add application file

Browse files
Files changed (2) hide show
  1. .huggingface.yaml +0 -0
  2. app.py +58 -0
.huggingface.yaml ADDED
File without changes
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import time
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+
6
+ # 모델 ID 설정
7
+ model_id = 'kakaocorp/kanana-nano-2.1b-instruct'
8
+
9
+ # 모델 및 토크나이저 로드 (모델은 한 번만 로드)
10
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float32, # CPU 전용
14
+ )
15
+
16
+ # 텍스트 생성 함수
17
+ def generate_text(prompt):
18
+ try:
19
+ start_time = time.time()
20
+
21
+ messages = [{"role": "user", "content": prompt}]
22
+ input_ids = tokenizer.apply_chat_template(
23
+ messages,
24
+ add_generation_prompt=True,
25
+ return_tensors="pt"
26
+ ).to(model.device)
27
+
28
+ # 종료 토큰 (일반적으로 "<|endoftext|>" 하나로 충분)
29
+ eos_token_id = tokenizer.eos_token_id or tokenizer.convert_tokens_to_ids("<|endoftext|>")
30
+
31
+ outputs = model.generate(
32
+ input_ids,
33
+ max_new_tokens=512,
34
+ eos_token_id=eos_token_id,
35
+ do_sample=True,
36
+ temperature=0.4,
37
+ top_p=0.9,
38
+ top_k=50, # 안정성 향상
39
+ )
40
+
41
+ result = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
42
+ elapsed = round(time.time() - start_time, 3)
43
+ return f"{result}\n\n[응답 시간]: {elapsed}초"
44
+
45
+ except Exception as e:
46
+ return f"[오류 발생]: {str(e)}"
47
+
48
+ # Gradio UI 정의
49
+ iface = gr.Interface(
50
+ fn=generate_text,
51
+ inputs=gr.Textbox(lines=4, placeholder="프롬프트를 입력하세요...", label="입력"),
52
+ outputs=gr.Textbox(label="모델 응답"),
53
+ title="kanana-nano-2.1b-instruct 데모",
54
+ description="카카오브레인의 경량화 LLM: kakaocorp/kanana-nano-2.1b-instruct 기반 텍스트 생성",
55
+ )
56
+
57
+ # 실행
58
+ iface.launch()