Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,56 @@
|
|
1 |
-
import os
|
2 |
-
import
|
3 |
-
|
4 |
-
from
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from llama_cpp import Llama
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
# Cấu hình tải model từ Hugging Face
|
7 |
+
REPO_ID = "vinai/PhoGPT-4B-Chat-gguf"
|
8 |
+
FILENAME = "phogpt-4b-chat-q4_K_M.gguf"
|
9 |
+
HF_TOKEN = os.getenv("HF_AUTH_TOKEN")
|
10 |
+
|
11 |
+
# Tải và cache model tự động
|
12 |
+
model_path = hf_hub_download(
|
13 |
+
repo_id=REPO_ID,
|
14 |
+
filename=FILENAME,
|
15 |
+
token=HF_TOKEN,
|
16 |
+
)
|
17 |
+
|
18 |
+
# Khởi tạo Llama với model đã tải
|
19 |
+
llm = Llama(
|
20 |
+
model_path=model_path,
|
21 |
+
n_ctx=2048,
|
22 |
+
n_threads=os.cpu_count(),
|
23 |
+
verbose=False,
|
24 |
+
)
|
25 |
+
|
26 |
+
def chat_fn(message, history=None):
|
27 |
+
# Thiết lập lịch sử cuộc trò chuyện
|
28 |
+
history = history or []
|
29 |
+
# Chuẩn bị messages cho chat completion
|
30 |
+
system_prompt = {"role": "system", "content": "Bạn là trợ lý AI tiếng Việt hữu ích."}
|
31 |
+
messages = [system_prompt]
|
32 |
+
for user_msg, bot_msg in history:
|
33 |
+
messages.append({"role": "user", "content": user_msg})
|
34 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
35 |
+
messages.append({"role": "user", "content": message})
|
36 |
+
|
37 |
+
# Tạo phản hồi
|
38 |
+
response = llm.create_chat_completion(
|
39 |
+
messages=messages,
|
40 |
+
max_tokens=512,
|
41 |
+
temperature=0.7,
|
42 |
+
stop=["<|user|>", "<|assistant|>"],
|
43 |
+
)
|
44 |
+
reply = response["choices"][0]["message"]["content"].strip()
|
45 |
+
# Cập nhật lịch sử
|
46 |
+
history.append((message, reply))
|
47 |
+
return reply, history
|
48 |
+
|
49 |
+
# Khởi chạy giao diện Gradio
|
50 |
+
gr.ChatInterface(
|
51 |
+
fn=chat_fn,
|
52 |
+
chatbot=gr.Chatbot(height=450),
|
53 |
+
title="🤖 PhoGPT-4B Chatbot (Tiếng Việt)",
|
54 |
+
description="Chatbot tiếng Việt sử dụng PhoGPT-4B-Chat-gguf",
|
55 |
+
theme="soft",
|
56 |
+
).launch()
|