Update app.py
Browse files
app.py
CHANGED
|
@@ -13,22 +13,56 @@ MODEL_NAME = "quickmt-en-vi"
|
|
| 13 |
|
| 14 |
def translate_text(text,progress=gr.Progress(track_tqdm=True)):
|
| 15 |
global t
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if not model_path.exists():
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
t = Translator(
|
| 25 |
str(model_path),
|
| 26 |
device="auto",
|
| 27 |
inter_threads=2,
|
| 28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
output = t([text], beam_size=1)
|
| 30 |
return output[0]
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
return text
|
| 33 |
|
| 34 |
# Tải model và tokenizer 1 LẦN DUY NHẤT
|
|
@@ -90,13 +124,13 @@ def chat_with_model(prompt, system_prompt, chatbot_display, internal_history,pro
|
|
| 90 |
# 5. Decode *chỉ* phần trả lời mới
|
| 91 |
response_text = tokenizer.decode(output_tokens[0][inputs.shape[-1]:], skip_special_tokens=True)
|
| 92 |
translated = translate_text(response_text)
|
| 93 |
-
print(
|
| 94 |
# 6. Cập nhật "bộ nhớ" (gr.State) với lượt nói MỚI
|
| 95 |
internal_history.append({"role": "user", "content": prompt})
|
| 96 |
-
internal_history.append({"role": "assistant", "content":
|
| 97 |
|
| 98 |
# 7. Cập nhật lịch sử hiển thị (gr.Chatbot)
|
| 99 |
-
chatbot_display.append([prompt,
|
| 100 |
|
| 101 |
# 8. Trả về cả hai để cập nhật UI
|
| 102 |
# (chuỗi rỗng "" để xóa nội dung trong ô prompt_box)
|
|
|
|
| 13 |
|
| 14 |
def translate_text(text,progress=gr.Progress(track_tqdm=True)):
|
| 15 |
global t
|
| 16 |
+
|
| 17 |
+
# 1. Thay đổi thư mục lưu trữ thành một thư mục có quyền ghi (ví dụ: trong thư mục hiện tại /code/ hoặc /app)
|
| 18 |
+
# Dùng Path("models") để lưu vào thư mục 'models' trong thư mục hiện tại của ứng dụng (nếu có quyền ghi)
|
| 19 |
+
# HOẶC, nếu bạn đang dùng HF Spaces, dùng Path("/tmp") có thể là một giải pháp tốt hơn
|
| 20 |
+
|
| 21 |
+
# Ở đây tôi đề xuất dùng thư mục 'models' trong thư mục hiện tại.
|
| 22 |
+
# Hãy đảm bảo rằng thư mục này có quyền ghi.
|
| 23 |
+
|
| 24 |
+
# Sửa đổi: Sử dụng Path("models") / MODEL_NAME
|
| 25 |
+
MODEL_STORAGE_DIR = Path("/tmp")
|
| 26 |
+
model_path = MODEL_STORAGE_DIR / MODEL_NAME
|
| 27 |
+
|
| 28 |
+
# Đảm bảo thư mục lưu trữ tồn tại trước khi tải
|
| 29 |
+
if not MODEL_STORAGE_DIR.exists():
|
| 30 |
+
MODEL_STORAGE_DIR.mkdir(parents=True, exist_ok=True)
|
| 31 |
+
|
| 32 |
if not model_path.exists():
|
| 33 |
+
print(f"Downloading model quickmt/{MODEL_NAME} to {model_path}...")
|
| 34 |
+
try:
|
| 35 |
+
hf_download(
|
| 36 |
+
model_name="quickmt/" + MODEL_NAME,
|
| 37 |
+
output_dir=model_path,
|
| 38 |
+
)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"❌ Lỗi khi tải model: {e}")
|
| 41 |
+
# Nếu tải thất bại, trả về văn bản gốc để tránh dừng chương trình
|
| 42 |
+
return text
|
| 43 |
+
|
| 44 |
try:
|
| 45 |
+
# Giả định biến 't' chưa được định nghĩa hoặc có lỗi liên quan đến việc kiểm tra model cũ
|
| 46 |
+
# Tôi sẽ đơn giản hóa logic kiểm tra model
|
| 47 |
+
global t # Đảm bảo biến t có thể được truy cập và sửa đổi.
|
| 48 |
+
|
| 49 |
+
# Nếu t chưa được load hoặc đường dẫn model khác
|
| 50 |
+
if 't' not in globals() or t is None or str(Path(t.model_path).name) != MODEL_NAME:
|
| 51 |
+
print(f"Loading model {MODEL_NAME} from {model_path}")
|
| 52 |
t = Translator(
|
| 53 |
str(model_path),
|
| 54 |
device="auto",
|
| 55 |
inter_threads=2,
|
| 56 |
)
|
| 57 |
+
|
| 58 |
+
if not text.strip():
|
| 59 |
+
return text
|
| 60 |
+
|
| 61 |
output = t([text], beam_size=1)
|
| 62 |
return output[0]
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"❌ Lỗi khi tải hoặc dịch: {e}")
|
| 66 |
return text
|
| 67 |
|
| 68 |
# Tải model và tokenizer 1 LẦN DUY NHẤT
|
|
|
|
| 124 |
# 5. Decode *chỉ* phần trả lời mới
|
| 125 |
response_text = tokenizer.decode(output_tokens[0][inputs.shape[-1]:], skip_special_tokens=True)
|
| 126 |
translated = translate_text(response_text)
|
| 127 |
+
print(response_text)
|
| 128 |
# 6. Cập nhật "bộ nhớ" (gr.State) với lượt nói MỚI
|
| 129 |
internal_history.append({"role": "user", "content": prompt})
|
| 130 |
+
internal_history.append({"role": "assistant", "content": translated})
|
| 131 |
|
| 132 |
# 7. Cập nhật lịch sử hiển thị (gr.Chatbot)
|
| 133 |
+
chatbot_display.append([prompt, translated])
|
| 134 |
|
| 135 |
# 8. Trả về cả hai để cập nhật UI
|
| 136 |
# (chuỗi rỗng "" để xóa nội dung trong ô prompt_box)
|