Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# 1.
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# 3. Định nghĩa hàm xử lý
|
| 13 |
def dich_teencode(text):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
|
|
|
| 17 |
return result[0]['generated_text']
|
| 18 |
|
| 19 |
# 4. Tạo giao diện
|
| 20 |
demo = gr.Interface(
|
| 21 |
fn=dich_teencode,
|
| 22 |
-
inputs=gr.Textbox(label="Nhập
|
| 23 |
-
outputs=gr.Textbox(label="Kết quả
|
| 24 |
-
title="
|
| 25 |
-
description=
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
# 5. Chạy ứng dụng
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
+
# 1. Định nghĩa đường dẫn model
|
| 5 |
+
# Dùng dấu chấm "." để báo cho code biết model nằm ngay tại thư mục này
|
| 6 |
+
model_path = "."
|
| 7 |
|
| 8 |
+
print("Đang tải model từ thư mục hiện tại...")
|
| 9 |
+
|
| 10 |
+
# 2. Load Tokenizer và Model thủ công để kiểm soát lỗi tốt hơn
|
| 11 |
+
try:
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 13 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
| 14 |
+
|
| 15 |
+
# Tạo pipeline từ model đã load
|
| 16 |
+
pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Lỗi khi load model: {e}")
|
| 19 |
+
# Fallback: Nếu không load được local, thử load từ ID gốc (phòng hờ)
|
| 20 |
+
pipe = pipeline("text2text-generation", model="Whelxi/bartpho-teencode")
|
| 21 |
|
| 22 |
# 3. Định nghĩa hàm xử lý
|
| 23 |
def dich_teencode(text):
|
| 24 |
+
if not text:
|
| 25 |
+
return ""
|
| 26 |
+
# max_length=128 là đủ cho các câu teencode thông thường
|
| 27 |
+
result = pipe(text, max_length=128)
|
| 28 |
return result[0]['generated_text']
|
| 29 |
|
| 30 |
# 4. Tạo giao diện
|
| 31 |
demo = gr.Interface(
|
| 32 |
fn=dich_teencode,
|
| 33 |
+
inputs=gr.Textbox(label="Nhập Teencode", placeholder="Ví dụ: k hum nay di hok khong?", lines=2),
|
| 34 |
+
outputs=gr.Textbox(label="Kết quả Tiếng Việt"),
|
| 35 |
+
title="Whelxi - Teencode Converter",
|
| 36 |
+
description="Chuyển đổi Teencode sang Tiếng Việt chuẩn sử dụng mô hình BARTpho-syllable đã được fine-tune.",
|
| 37 |
+
examples=[["k hum nay di hok khong?"], ["hqa m lam j?"]]
|
| 38 |
)
|
| 39 |
|
| 40 |
# 5. Chạy ứng dụng
|