File size: 1,608 Bytes
b30eda1
8391c09
a673a02
b30eda1
0789c58
 
 
 
 
 
 
 
 
 
 
 
 
 
b30eda1
a673a02
0789c58
80986a7
0789c58
80986a7
 
0789c58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8391c09
0789c58
 
 
 
 
 
 
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
57
58
59
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import torch

# CPU ๊ฐ•์ œ ์„ค์ •
device = "cpu"
torch.set_num_threads(4)  # CPU ์Šค๋ ˆ๋“œ ์ œํ•œ

# ๊ฒฝ๋Ÿ‰ํ™”๋œ ๋ชจ๋ธ ๋กœ๋“œ
model = AutoModelForCausalLM.from_pretrained(
    "naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B",
    torch_dtype=torch.float32,  # CPU๋Š” float32 ๊ถŒ์žฅ
    low_cpu_mem_usage=True
).to(device)

tokenizer = AutoTokenizer.from_pretrained(
    "naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B"
)

def predict(message, history):
    # ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ์„ ์œ„ํ•œ ๊ฐ„์†Œํ™”๋œ ์ฑ„ํŒ… ๊ตฌ์„ฑ
    chat = [
        {"role": "system", "content": "๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋‹ต๋ณ€ํ•ด์ฃผ์„ธ์š”."},
        {"role": "user", "content": message}
    ]
    
    # CPU ์ตœ์ ํ™” ์„ค์ •
    inputs = tokenizer.apply_chat_template(
        chat,
        return_tensors="pt",
        max_length=512,  # ๊ธธ์ด ์ œํ•œ
        truncation=True
    ).to(device)
    
    outputs = model.generate(
        inputs,
        max_new_tokens=200,  # ์งง์€ ์‘๋‹ต
        temperature=0.3,    # ์ฐฝ์˜์„ฑ ๊ฐ์†Œ
        do_sample=False      #็กฎๅฎšๆ€ง ์‘๋‹ต
    )
    
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# ๊ฒฝ๋Ÿ‰ํ™”๋œ ์ธํ„ฐํŽ˜์ด์Šค
demo = gr.ChatInterface(
    predict,
    title="CLOVA X (CPU ๋ชจ๋“œ)",
    description="CPU ์ „์šฉ ๊ฒฝ๋Ÿ‰ํ™” ๋ฒ„์ „",
    theme="soft",
    examples=["์•ˆ๋…•ํ•˜์„ธ์š”", "๋‚ ์”จ ์•Œ๋ ค์ค˜"]
)

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        favicon_path=None,
        prevent_thread_lock=True
    )