Spaces:
Sleeping
Sleeping
๊น์คํ commited on
Commit ยท
d52e44d
1
Parent(s): 3e87bd8
gpu
Browse files
app.py
CHANGED
|
@@ -4,27 +4,34 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
|
| 4 |
from peft import PeftModel
|
| 5 |
|
| 6 |
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 7 |
-
|
| 8 |
-
# 1) ๋ฒ ์ด์ค ๋ชจ๋ธ ๋ก๋ (ํ์ตํ ๋์ ๋์ผํ ์ค์ )
|
| 9 |
-
bnb_config = BitsAndBytesConfig(
|
| 10 |
-
load_in_4bit=True,
|
| 11 |
-
bnb_4bit_quant_type="nf4",
|
| 12 |
-
bnb_4bit_compute_dtype=torch.float16,
|
| 13 |
-
bnb_4bit_use_double_quant=True,
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
-
model_name,
|
| 18 |
-
quantization_config=bnb_config,
|
| 19 |
-
device_map="auto",
|
| 20 |
-
)
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@spaces.GPU
|
| 27 |
-
def generate(prompt,
|
|
|
|
| 28 |
model.eval()
|
| 29 |
messages = [{"role": "user", "content": prompt}]
|
| 30 |
inputs = tokenizer.apply_chat_template(
|
|
@@ -46,6 +53,6 @@ def generate(prompt, model, max_new_tokens=200):
|
|
| 46 |
import gradio as gr
|
| 47 |
|
| 48 |
def chat(message, history):
|
| 49 |
-
return generate(message
|
| 50 |
|
| 51 |
gr.ChatInterface(chat).launch() # Colab์์๋ share=True ๋ก ์์ ๊ณต๊ฐ ๋งํฌ ์์ฑ
|
|
|
|
| 4 |
from peft import PeftModel
|
| 5 |
|
| 6 |
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
|
| 9 |
+
model = None
|
| 10 |
+
|
| 11 |
+
def load_model():
|
| 12 |
+
# ZeroGPU์์๋ ์ค์ GPU๊ฐ @spaces.GPU ํจ์ ํธ์ถ ์์ ์๋ง ํ ๋น๋๋ฏ๋ก
|
| 13 |
+
# bitsandbytes 4bit ๋ก๋ฉ๋ ์ด ํจ์ ์์์ ์ฒ์ ํธ์ถ๋ ๋ ์ํํด์ผ ํ๋ค.
|
| 14 |
+
global model
|
| 15 |
+
if model is not None:
|
| 16 |
+
return model
|
| 17 |
+
|
| 18 |
+
bnb_config = BitsAndBytesConfig(
|
| 19 |
+
load_in_4bit=True,
|
| 20 |
+
bnb_4bit_quant_type="nf4",
|
| 21 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 22 |
+
bnb_4bit_use_double_quant=True,
|
| 23 |
+
)
|
| 24 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 25 |
+
model_name,
|
| 26 |
+
quantization_config=bnb_config,
|
| 27 |
+
device_map="auto",
|
| 28 |
+
)
|
| 29 |
+
model = PeftModel.from_pretrained(base_model, "JunHwi/Joseon-Qwen")
|
| 30 |
+
return model
|
| 31 |
|
| 32 |
@spaces.GPU
|
| 33 |
+
def generate(prompt, max_new_tokens=200):
|
| 34 |
+
model = load_model()
|
| 35 |
model.eval()
|
| 36 |
messages = [{"role": "user", "content": prompt}]
|
| 37 |
inputs = tokenizer.apply_chat_template(
|
|
|
|
| 53 |
import gradio as gr
|
| 54 |
|
| 55 |
def chat(message, history):
|
| 56 |
+
return generate(message)
|
| 57 |
|
| 58 |
gr.ChatInterface(chat).launch() # Colab์์๋ share=True ๋ก ์์ ๊ณต๊ฐ ๋งํฌ ์์ฑ
|