Spaces:
Sleeping
Sleeping
Commit
·
3e5f6ae
1
Parent(s):
90486e8
met roi do
Browse files
app.py
CHANGED
|
@@ -7,22 +7,12 @@ from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
|
| 7 |
model_path = "vinai/PhoGPT-4B-Chat"
|
| 8 |
|
| 9 |
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
|
| 10 |
-
config.init_device = "cpu"
|
| 11 |
-
# Nếu có FlashAttention, bật thêm:
|
| 12 |
-
# config.attn_config['attn_impl'] = 'flash'
|
| 13 |
|
| 14 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
-
model_path,
|
| 16 |
-
config=config,
|
| 17 |
-
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 18 |
-
trust_remote_code=True,
|
| 19 |
-
)
|
| 20 |
model.eval()
|
| 21 |
-
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 23 |
|
| 24 |
-
# 2️⃣ Hàm chat theo template “### Câu hỏi / ### Trả lời”
|
| 25 |
-
PROMPT_TEMPLATE = "### Câu hỏi: {instruction}\n### Trả lời:"
|
| 26 |
|
| 27 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 28 |
# 2.1 — Gom system message và history vào messages list
|
|
@@ -35,19 +25,20 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
| 35 |
messages.append({"role": "user", "content": message})
|
| 36 |
|
| 37 |
# 2.2 — Tạo prompt chuẩn
|
| 38 |
-
|
| 39 |
messages,
|
| 40 |
tokenize=False,
|
| 41 |
add_generation_prompt=True
|
| 42 |
)
|
| 43 |
|
| 44 |
# 2.3 — Tokenize và đưa lên device
|
| 45 |
-
inputs = tokenizer(
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
# 2.4 — Sinh text
|
| 49 |
outputs = model.generate(
|
| 50 |
-
|
| 51 |
max_new_tokens=max_tokens,
|
| 52 |
temperature=temperature,
|
| 53 |
top_p=top_p,
|
|
@@ -55,16 +46,19 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
| 55 |
eos_token_id=tokenizer.eos_token_id,
|
| 56 |
pad_token_id=tokenizer.pad_token_id,
|
| 57 |
)
|
| 58 |
-
|
| 59 |
# 2.5 — Decode và tách phần assistant trả lời
|
| 60 |
-
|
| 61 |
-
response
|
| 62 |
-
|
| 63 |
|
| 64 |
# 2.6 — Cập nhật history và trả về
|
| 65 |
# history.append((message, response))
|
| 66 |
# return history
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
# 3️⃣ Giao diện Gradio
|
| 69 |
demo = gr.ChatInterface(
|
| 70 |
respond,
|
|
|
|
| 7 |
model_path = "vinai/PhoGPT-4B-Chat"
|
| 8 |
|
| 9 |
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
|
| 10 |
+
config.init_device = "cpu"
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
model.eval()
|
|
|
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 18 |
# 2.1 — Gom system message và history vào messages list
|
|
|
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
# 2.2 — Tạo prompt chuẩn
|
| 28 |
+
input_prompt = tokenizer.apply_chat_template(
|
| 29 |
messages,
|
| 30 |
tokenize=False,
|
| 31 |
add_generation_prompt=True
|
| 32 |
)
|
| 33 |
|
| 34 |
# 2.3 — Tokenize và đưa lên device
|
| 35 |
+
# inputs = tokenizer(input_prompt, return_tensors="pt")
|
| 36 |
+
input_ids = tokenizer(input_prompt, return_tensors="pt")
|
| 37 |
+
# inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 38 |
|
| 39 |
# 2.4 — Sinh text
|
| 40 |
outputs = model.generate(
|
| 41 |
+
inputs=input_ids["input_ids"],
|
| 42 |
max_new_tokens=max_tokens,
|
| 43 |
temperature=temperature,
|
| 44 |
top_p=top_p,
|
|
|
|
| 46 |
eos_token_id=tokenizer.eos_token_id,
|
| 47 |
pad_token_id=tokenizer.pad_token_id,
|
| 48 |
)
|
| 49 |
+
print('!!!! OUTPUTS: ',outputs)
|
| 50 |
# 2.5 — Decode và tách phần assistant trả lời
|
| 51 |
+
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 52 |
+
response = response.split("### Trả lời:")[1]
|
| 53 |
+
yield response
|
| 54 |
|
| 55 |
# 2.6 — Cập nhật history và trả về
|
| 56 |
# history.append((message, response))
|
| 57 |
# return history
|
| 58 |
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
# 3️⃣ Giao diện Gradio
|
| 63 |
demo = gr.ChatInterface(
|
| 64 |
respond,
|