Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,58 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def predict(message, history):
|
|
|
|
| 10 |
chat = [
|
| 11 |
-
{"role": "system", "content": "
|
| 12 |
-
*[{"role": "user" if i%2==0 else "assistant", "content": h} for i,h in enumerate(sum(history, ()))],
|
| 13 |
{"role": "user", "content": message}
|
| 14 |
]
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# CPU ๊ฐ์ ์ค์
|
| 6 |
+
device = "cpu"
|
| 7 |
+
torch.set_num_threads(4) # CPU ์ค๋ ๋ ์ ํ
|
| 8 |
+
|
| 9 |
+
# ๊ฒฝ๋ํ๋ ๋ชจ๋ธ ๋ก๋
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
"naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B",
|
| 12 |
+
torch_dtype=torch.float32, # CPU๋ float32 ๊ถ์ฅ
|
| 13 |
+
low_cpu_mem_usage=True
|
| 14 |
+
).to(device)
|
| 15 |
+
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 17 |
+
"naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B"
|
| 18 |
+
)
|
| 19 |
|
| 20 |
def predict(message, history):
|
| 21 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ์ ์ํ ๊ฐ์ํ๋ ์ฑํ
๊ตฌ์ฑ
|
| 22 |
chat = [
|
| 23 |
+
{"role": "system", "content": "๊ฐ๊ฒฐํ๊ฒ ๋ต๋ณํด์ฃผ์ธ์."},
|
|
|
|
| 24 |
{"role": "user", "content": message}
|
| 25 |
]
|
| 26 |
+
|
| 27 |
+
# CPU ์ต์ ํ ์ค์
|
| 28 |
+
inputs = tokenizer.apply_chat_template(
|
| 29 |
+
chat,
|
| 30 |
+
return_tensors="pt",
|
| 31 |
+
max_length=512, # ๊ธธ์ด ์ ํ
|
| 32 |
+
truncation=True
|
| 33 |
+
).to(device)
|
| 34 |
+
|
| 35 |
+
outputs = model.generate(
|
| 36 |
+
inputs,
|
| 37 |
+
max_new_tokens=200, # ์งง์ ์๋ต
|
| 38 |
+
temperature=0.3, # ์ฐฝ์์ฑ ๊ฐ์
|
| 39 |
+
do_sample=False #็กฎๅฎๆง ์๋ต
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 43 |
+
|
| 44 |
+
# ๊ฒฝ๋ํ๋ ์ธํฐํ์ด์ค
|
| 45 |
+
demo = gr.ChatInterface(
|
| 46 |
+
predict,
|
| 47 |
+
title="CLOVA X (CPU ๋ชจ๋)",
|
| 48 |
+
description="CPU ์ ์ฉ ๊ฒฝ๋ํ ๋ฒ์ ",
|
| 49 |
+
theme="soft",
|
| 50 |
+
examples=["์๋
ํ์ธ์", "๋ ์จ ์๋ ค์ค"]
|
| 51 |
+
)
|
| 52 |
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch(
|
| 55 |
+
server_name="0.0.0.0",
|
| 56 |
+
server_port=7860,
|
| 57 |
+
favicon_path=None,
|
| 58 |
+
prevent_thread_lock=True
|
| 59 |
+
)
|