Spaces:
Sleeping
Sleeping
Commit
·
c365981
1
Parent(s):
da5015b
back
Browse files
app.py
CHANGED
|
@@ -1,8 +1,64 @@
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
-
import phoGPT
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# 3️⃣ Giao diện Gradio
|
| 8 |
demo = gr.ChatInterface(
|
|
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
| 6 |
+
|
| 7 |
+
# 1️⃣ Cấu hình và load model + tokenizer
|
| 8 |
+
model_path = "vinai/PhoGPT-4B-Chat"
|
| 9 |
+
|
| 10 |
+
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
|
| 11 |
+
config.init_device = "cpu"
|
| 12 |
+
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained("vinai/PhoGPT-4B-Chat", trust_remote_code=True)
|
| 14 |
+
model.eval()
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 19 |
+
# 2.1 — Gom system message và history vào messages list
|
| 20 |
+
messages = [{"role": "system", "content": system_message}]
|
| 21 |
+
for u, b in history:
|
| 22 |
+
if u:
|
| 23 |
+
messages.append({"role": "user", "content": u})
|
| 24 |
+
if b:
|
| 25 |
+
messages.append({"role": "assistant", "content": b})
|
| 26 |
+
messages.append({"role": "user", "content": message})
|
| 27 |
+
|
| 28 |
+
# 2.2 — Tạo prompt chuẩn
|
| 29 |
+
input_prompt = tokenizer.apply_chat_template(
|
| 30 |
+
messages,
|
| 31 |
+
tokenize=False,
|
| 32 |
+
add_generation_prompt=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# 2.3 — Tokenize và đưa lên device
|
| 36 |
+
# inputs = tokenizer(input_prompt, return_tensors="pt")
|
| 37 |
+
input_ids = tokenizer(input_prompt, return_tensors="pt")
|
| 38 |
+
# inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 39 |
+
|
| 40 |
+
# 2.4 — Sinh text
|
| 41 |
+
outputs = model.generate(
|
| 42 |
+
inputs=input_ids["input_ids"],
|
| 43 |
+
max_new_tokens=max_tokens,
|
| 44 |
+
temperature=temperature,
|
| 45 |
+
top_p=top_p,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 48 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 49 |
+
)
|
| 50 |
+
# print('!!!! OUTPUTS 1: ',outputs)
|
| 51 |
+
# 2.5 — Decode và tách phần assistant trả lời
|
| 52 |
+
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 53 |
+
print('!! OUTPUTS 2: ',response)
|
| 54 |
+
|
| 55 |
+
response = response.split("### Trả lời:")[1]
|
| 56 |
+
print('!!!! OUTPUTS 3: ',response)
|
| 57 |
+
return response
|
| 58 |
+
|
| 59 |
+
# 2.6 — Cập nhật history và trả về
|
| 60 |
+
# history.append((message, response))
|
| 61 |
+
# return history
|
| 62 |
|
| 63 |
# 3️⃣ Giao diện Gradio
|
| 64 |
demo = gr.ChatInterface(
|
git.txt
DELETED
|
@@ -1,4 +0,0 @@
|
|
| 1 |
-
git commit -am "Update space"; git push origin main
|
| 2 |
-
|
| 3 |
-
git add test.py
|
| 4 |
-
git rm test.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
phoGPT.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
|
| 2 |
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 3 |
|
| 4 |
# 1️⃣ Cấu hình và load model + tokenizer
|
| 5 |
model_path = "vinai/PhoGPT-4B-Chat"
|
|
|
|
| 1 |
+
# type: ignore
|
| 2 |
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
# 1️⃣ Cấu hình và load model + tokenizer
|
| 6 |
model_path = "vinai/PhoGPT-4B-Chat"
|