ThongCoding commited on
Commit
c26f4f8
·
verified ·
1 Parent(s): da4e0ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -36
app.py CHANGED
@@ -1,36 +1,56 @@
1
- import os
2
- import subprocess
3
- import gradio as gr
4
- from llama_cpp import Llama
5
-
6
- MODEL_URL = "https://drive.google.com/uc?id=1aYzH-tUYl8LT-0KdO1NK9mA1w37CkM7s&export=download"
7
- MODEL_PATH = "model/phogpt-4b-chat-q4_K_M.gguf"
8
-
9
- # Tải model nếu chưa có
10
- def download_model():
11
- if not os.path.exists(MODEL_PATH):
12
- os.makedirs("model", exist_ok=True)
13
- print("⬇️ Tải model từ Google Drive...")
14
- subprocess.run([
15
- "wget", "-O", MODEL_PATH, MODEL_URL
16
- ])
17
- print("✅ Tải xong!")
18
-
19
- download_model()
20
-
21
- # Load model
22
- llm = Llama(
23
- model_path=MODEL_PATH,
24
- n_ctx=2048,
25
- n_threads=4,
26
- n_gpu_layers=35,
27
- verbose=True
28
- )
29
-
30
- def chat_fn(message, history):
31
- prompt = f"<|user|>\n{message}\n<|assistant|>\n"
32
- response = llm(prompt, max_tokens=512, stop=["<|user|>"], echo=False)
33
- output = response["choices"][0]["text"].strip()
34
- return output
35
-
36
- gr.ChatInterface(chat_fn, title="🧠 PhoGPT-4B Chatbot (Tiếng Việt)").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()