Spaces:
Sleeping
Sleeping
Commit
·
cf42bd0
1
Parent(s):
6c7392f
test xíu
Browse files
app.py
CHANGED
@@ -1,20 +1,15 @@
|
|
1 |
-
#
|
2 |
-
import torch
|
3 |
import gradio as gr
|
4 |
-
from transformers import
|
5 |
|
6 |
-
# 1️⃣
|
7 |
-
|
8 |
-
"
|
|
|
9 |
trust_remote_code=True
|
10 |
)
|
11 |
-
model = AutoModelForCausalLM.from_pretrained(
|
12 |
-
"vinai/PhoGPT-4B-Chat",
|
13 |
-
trust_remote_code=True
|
14 |
-
)
|
15 |
-
model.eval()
|
16 |
|
17 |
-
# 2️⃣ Hàm
|
18 |
def respond(message, history):
|
19 |
# Gom lịch sử chat + tin nhắn mới vào prompt
|
20 |
prompt = ""
|
@@ -22,20 +17,17 @@ def respond(message, history):
|
|
22 |
prompt += f"Bạn: {user_msg}\nBot: {bot_msg}\n"
|
23 |
prompt += f"Bạn: {message}\nBot:"
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
max_new_tokens=100, # khoảng 60–80 từ
|
30 |
temperature=0.7,
|
31 |
top_p=0.9,
|
32 |
-
do_sample=True
|
33 |
-
eos_token_id=tokenizer.eos_token_id,
|
34 |
-
pad_token_id=tokenizer.pad_token_id
|
35 |
)
|
|
|
36 |
|
37 |
-
#
|
38 |
-
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
answer = generated.replace(prompt, "").strip()
|
40 |
|
41 |
# Cập nhật lịch sử và trả về
|
@@ -45,10 +37,7 @@ def respond(message, history):
|
|
45 |
# 3️⃣ Giao diện Gradio
|
46 |
demo = gr.ChatInterface(
|
47 |
fn=respond,
|
48 |
-
|
49 |
-
additional_inputs=[
|
50 |
-
gr.Textbox("Bạn là trợ lý AI tiếng Việt thân thiện.", label="System message")
|
51 |
-
],
|
52 |
)
|
53 |
|
54 |
if __name__ == "__main__":
|
|
|
1 |
+
# app_phogpt4b_chat.py
|
|
|
2 |
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
|
5 |
+
# 1️⃣ Tạo pipeline với PhoGPT-4B-Chat
|
6 |
+
pipe = pipeline(
|
7 |
+
"text-generation",
|
8 |
+
model="vinai/PhoGPT-4B-Chat",
|
9 |
trust_remote_code=True
|
10 |
)
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# 2️⃣ Hàm chat handler
|
13 |
def respond(message, history):
|
14 |
# Gom lịch sử chat + tin nhắn mới vào prompt
|
15 |
prompt = ""
|
|
|
17 |
prompt += f"Bạn: {user_msg}\nBot: {bot_msg}\n"
|
18 |
prompt += f"Bạn: {message}\nBot:"
|
19 |
|
20 |
+
# Sinh văn bản
|
21 |
+
outputs = pipe(
|
22 |
+
prompt,
|
23 |
+
max_new_tokens=100, # khoảng 60–80 từ
|
|
|
24 |
temperature=0.7,
|
25 |
top_p=0.9,
|
26 |
+
do_sample=True
|
|
|
|
|
27 |
)
|
28 |
+
generated = outputs[0]["generated_text"]
|
29 |
|
30 |
+
# Tách phần Bot trả lời
|
|
|
31 |
answer = generated.replace(prompt, "").strip()
|
32 |
|
33 |
# Cập nhật lịch sử và trả về
|
|
|
37 |
# 3️⃣ Giao diện Gradio
|
38 |
demo = gr.ChatInterface(
|
39 |
fn=respond,
|
40 |
+
# Không thêm message_format hay type → dùng default tuple format
|
|
|
|
|
|
|
41 |
)
|
42 |
|
43 |
if __name__ == "__main__":
|