Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,117 +10,77 @@ from transformers import (
|
|
| 10 |
)
|
| 11 |
import re
|
| 12 |
|
| 13 |
-
# --- THAY ĐỔI
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# -------------------------------------------------------------
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
print(f"Bắt đầu tải model lên thiết bị: {DEVICE}...")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
try:
|
| 26 |
-
tokenizer = tokenizer_class.from_pretrained(repo_id)
|
| 27 |
-
model = model_class.from_pretrained(repo_id).to(DEVICE)
|
| 28 |
-
model.eval()
|
| 29 |
-
print(f"Tải thành công model: {repo_id}")
|
| 30 |
-
return model, tokenizer
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"Lỗi khi tải model {repo_id}: {e}")
|
| 33 |
-
# Trả về None nếu có lỗi để xử lý ở giao diện
|
| 34 |
-
return None, None
|
| 35 |
|
| 36 |
# Tải các model
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# Hàm clean text, lấy từ notebook của bạn
|
| 41 |
def clean_text(text):
|
| 42 |
-
if not isinstance(text, str):
|
| 43 |
-
return ""
|
| 44 |
return re.sub(r'\s+', ' ', text).strip()
|
| 45 |
|
| 46 |
-
# Hàm xử lý việc sửa lỗi
|
| 47 |
def correct_grammar(sentence, model_choice):
|
| 48 |
-
if not sentence.strip():
|
| 49 |
-
return "Vui lòng nhập một câu."
|
| 50 |
-
|
| 51 |
-
model = None
|
| 52 |
-
tokenizer = None
|
| 53 |
-
prefix = ""
|
| 54 |
|
|
|
|
| 55 |
if model_choice == "BARTpho-syllable":
|
| 56 |
-
if model_bart
|
| 57 |
-
model = model_bart
|
| 58 |
-
tokenizer = tokenizer_bart
|
| 59 |
-
prefix = "Fix: "
|
| 60 |
else:
|
| 61 |
-
return "Lỗi: Model BART không khả dụng. Vui lòng
|
| 62 |
-
|
| 63 |
elif model_choice == "ViT5-base":
|
| 64 |
-
if model_vit5
|
| 65 |
-
model = model_vit5
|
| 66 |
-
tokenizer = tokenizer_vit5
|
| 67 |
-
prefix = "sửa lỗi: "
|
| 68 |
else:
|
| 69 |
-
return "Lỗi: Model ViT5 không khả dụng. Vui lòng
|
| 70 |
-
|
| 71 |
input_text = prefix + sentence
|
| 72 |
-
input_ids = tokenizer(
|
| 73 |
-
input_text,
|
| 74 |
-
return_tensors="pt",
|
| 75 |
-
max_length=256,
|
| 76 |
-
truncation=True,
|
| 77 |
-
padding=True
|
| 78 |
-
).input_ids.to(DEVICE)
|
| 79 |
|
| 80 |
with torch.no_grad():
|
| 81 |
-
outputs = model.generate(
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
num_beams=2,
|
| 85 |
-
early_stopping=True,
|
| 86 |
-
repetition_penalty=1.05,
|
| 87 |
-
no_repeat_ngram_size=2
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
corrected_sentence = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 91 |
-
return clean_text(corrected_sentence)
|
| 92 |
|
| 93 |
-
# Ví dụ cho giao diện Gradio
|
| 94 |
-
examples = [
|
| 95 |
-
["chương trỉnhnh được páht sóng vào lúc 19h", "ViT5-base"],
|
| 96 |
-
["công nghề thônngg tin đáng phát chiển rất nhanh", "ViT5-base"],
|
| 97 |
-
["Học hok tốt thì kho mak đc điểm cao.", "BARTpho-syllable"],
|
| 98 |
-
["dù rất mệt nhưng anh ấy vẫn cố hoàn thành công việc", "BARTpho-syllable"],
|
| 99 |
-
]
|
| 100 |
-
|
| 101 |
-
# Mô tả cho ứng dụng
|
| 102 |
description = """
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
**Cách sử dụng:**
|
| 109 |
-
1. Nhập câu tiếng Việt có lỗi vào ô bên dưới.
|
| 110 |
-
2. Chọn một trong hai model để thực hiện sửa lỗi.
|
| 111 |
-
3. Nhấn "Submit" và xem kết quả.
|
| 112 |
"""
|
|
|
|
| 113 |
|
| 114 |
-
# Tạo giao diện Gradio
|
| 115 |
demo = gr.Interface(
|
| 116 |
fn=correct_grammar,
|
| 117 |
inputs=[
|
| 118 |
gr.Textbox(lines=5, label="Nhập câu tiếng Việt bị lỗi"),
|
| 119 |
-
gr.Radio(
|
| 120 |
-
choices=["BARTpho-syllable", "ViT5-base"],
|
| 121 |
-
value="ViT5-base", # Model mặc định
|
| 122 |
-
label="Chọn Model"
|
| 123 |
-
)
|
| 124 |
],
|
| 125 |
outputs=gr.Textbox(label="Câu đã được sửa"),
|
| 126 |
title="Sửa lỗi Ngữ pháp Tiếng Việt",
|
|
@@ -128,6 +88,5 @@ demo = gr.Interface(
|
|
| 128 |
examples=examples
|
| 129 |
)
|
| 130 |
|
| 131 |
-
# Chạy ứng dụng
|
| 132 |
if __name__ == "__main__":
|
| 133 |
demo.launch()
|
|
|
|
| 10 |
)
|
| 11 |
import re
|
| 12 |
|
| 13 |
+
# --- THAY ĐỔI USERNAME CỦA BẠN VÀO ĐÂY ---
|
| 14 |
+
HF_USERNAME = "Tin113"
|
| 15 |
+
# -----------------------------------------
|
|
|
|
| 16 |
|
| 17 |
+
BART_MODEL_REPO = f"{HF_USERNAME}/bart_model"
|
| 18 |
+
VIT5_MODEL_REPO = f"{HF_USERNAME}/vit5_model"
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
print(f"Thiết bị sử dụng: {DEVICE}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Tải các model
|
| 24 |
+
try:
|
| 25 |
+
print(f"Đang tải model BART từ {BART_MODEL_REPO}...")
|
| 26 |
+
tokenizer_bart = AutoTokenizer.from_pretrained(BART_MODEL_REPO)
|
| 27 |
+
model_bart = AutoModelForSeq2SeqLM.from_pretrained(BART_MODEL_REPO).to(DEVICE)
|
| 28 |
+
model_bart.eval()
|
| 29 |
+
print("Tải model BART thành công.")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"Lỗi khi tải model BART: {e}")
|
| 32 |
+
model_bart, tokenizer_bart = None, None
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
print(f"Đang tải model ViT5 từ {VIT5_MODEL_REPO}...")
|
| 36 |
+
tokenizer_vit5 = T5Tokenizer.from_pretrained(VIT5_MODEL_REPO)
|
| 37 |
+
model_vit5 = T5ForConditionalGeneration.from_pretrained(VIT5_MODEL_REPO).to(DEVICE)
|
| 38 |
+
model_vit5.eval()
|
| 39 |
+
print("Tải model ViT5 thành công.")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Lỗi khi tải model ViT5: {e}")
|
| 42 |
+
model_vit5, tokenizer_vit5 = None, None
|
| 43 |
|
|
|
|
| 44 |
def clean_text(text):
|
| 45 |
+
if not isinstance(text, str): return ""
|
|
|
|
| 46 |
return re.sub(r'\s+', ' ', text).strip()
|
| 47 |
|
|
|
|
| 48 |
def correct_grammar(sentence, model_choice):
|
| 49 |
+
if not sentence.strip(): return "Vui lòng nhập một câu."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
model, tokenizer, prefix = None, None, ""
|
| 52 |
if model_choice == "BARTpho-syllable":
|
| 53 |
+
if model_bart:
|
| 54 |
+
model, tokenizer, prefix = model_bart, tokenizer_bart, "Fix: "
|
|
|
|
|
|
|
| 55 |
else:
|
| 56 |
+
return "Lỗi: Model BART không khả dụng. Vui lòng kiểm tra lại Space."
|
|
|
|
| 57 |
elif model_choice == "ViT5-base":
|
| 58 |
+
if model_vit5:
|
| 59 |
+
model, tokenizer, prefix = model_vit5, tokenizer_vit5, "sửa lỗi: "
|
|
|
|
|
|
|
| 60 |
else:
|
| 61 |
+
return "Lỗi: Model ViT5 không khả dụng. Vui lòng kiểm tra lại Space."
|
| 62 |
+
|
| 63 |
input_text = prefix + sentence
|
| 64 |
+
input_ids = tokenizer(input_text, return_tensors="pt", max_length=256, truncation=True, padding=True).input_ids.to(DEVICE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
with torch.no_grad():
|
| 67 |
+
outputs = model.generate(input_ids, max_length=276, num_beams=2, early_stopping=True, repetition_penalty=1.05, no_repeat_ngram_size=2)
|
| 68 |
+
|
| 69 |
+
return clean_text(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
description = """
|
| 72 |
+
Demo sửa lỗi ngữ pháp tiếng Việt sử dụng hai model: BARTpho-syllable và ViT5-base.
|
| 73 |
+
1. Nhập câu lỗi vào ô bên dưới.
|
| 74 |
+
2. Chọn model bạn muốn dùng.
|
| 75 |
+
3. Nhấn "Submit" để xem kết quả.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
"""
|
| 77 |
+
examples = [["chương trỉnhnh được páht sóng vào lúc 19h", "ViT5-base"], ["Học hok tốt thì kho mak đc điểm cao.", "BARTpho-syllable"]]
|
| 78 |
|
|
|
|
| 79 |
demo = gr.Interface(
|
| 80 |
fn=correct_grammar,
|
| 81 |
inputs=[
|
| 82 |
gr.Textbox(lines=5, label="Nhập câu tiếng Việt bị lỗi"),
|
| 83 |
+
gr.Radio(choices=["BARTpho-syllable", "ViT5-base"], value="ViT5-base", label="Chọn Model")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
],
|
| 85 |
outputs=gr.Textbox(label="Câu đã được sửa"),
|
| 86 |
title="Sửa lỗi Ngữ pháp Tiếng Việt",
|
|
|
|
| 88 |
examples=examples
|
| 89 |
)
|
| 90 |
|
|
|
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch()
|