Whelxi commited on
Commit
5b28970
·
verified ·
1 Parent(s): 52df517

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -14
app.py CHANGED
@@ -1,28 +1,40 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # 1. Khai báo ID của model từ Hugging Face
5
- MODEL_ID = "Whelxi/bartpho-teencode"
 
6
 
7
- # 2. Tải model về Space
8
- # Dùng task "text2text-generation" vì Bartpho là model sinh văn bản
9
- print("Đang tải model... Vui lòng đợi trong giây lát.")
10
- pipe = pipeline("text2text-generation", model=MODEL_ID)
 
 
 
 
 
 
 
 
 
11
 
12
  # 3. Định nghĩa hàm xử lý
13
  def dich_teencode(text):
14
- # Model sinh văn bản, max_length giới hạn độ dài câu trả lời
15
- result = pipe(text, max_length=100)
16
- # Lấy kết quả text ra từ list trả về
 
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 văn bản (Teencode/Tiếng Việt)", placeholder="Ví dụ: k hum nay di hok khong?"),
23
- outputs=gr.Textbox(label="Kết quả chuyển đổi"),
24
- title="Demo Whelxi/bartpho-teencode",
25
- description=f"Model được load trực tiếp từ {MODEL_ID}. Nhập thử teencode để xem model dịch sang tiếng Việt chuẩn (hoặc ngược lại tùy vào cách model được fine-tune)."
 
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 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 đủ 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 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