Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,38 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
|
| 4 |
+
def load_model(checkpoint_path):
|
| 5 |
+
# Load tokenizer và model từ Hugging Face Hub
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint_path)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint_path)
|
| 8 |
+
return model, tokenizer
|
| 9 |
|
| 10 |
+
def correct_spelling(text):
|
| 11 |
+
# Thêm tiền tố nếu model yêu cầu (tuỳ vào cách model được huấn luyện)
|
| 12 |
+
input_text = text # Thay đổi nếu cần
|
| 13 |
+
|
| 14 |
+
# Tokenize và generate
|
| 15 |
+
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 16 |
+
outputs = model.generate(
|
| 17 |
+
**inputs,
|
| 18 |
+
max_length=512,
|
| 19 |
+
num_beams=5,
|
| 20 |
+
early_stopping=True
|
| 21 |
+
)
|
| 22 |
+
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
+
return corrected_text
|
| 24 |
+
|
| 25 |
+
# Đường dẫn đến model của bạn trên Hugging Face Hub
|
| 26 |
+
checkpoint_path = "Diezu/Batpho_v2" # Thay bằng tên repository của bạn
|
| 27 |
+
model, tokenizer = load_model(checkpoint_path)
|
| 28 |
+
|
| 29 |
+
# Tạo giao diện Gradio
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=correct_spelling,
|
| 32 |
+
inputs=gr.Textbox(placeholder="Nhập văn bản có lỗi chính tả..."),
|
| 33 |
+
outputs=gr.Textbox(label="Văn bản đã sửa"),
|
| 34 |
+
title="Demo Sửa Lỗi Chính Tả",
|
| 35 |
+
description="Sử dụng mô hình của bạn để sửa lỗi chính tả tiếng Việt."
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|