vijjj1 commited on
Commit
6deb85e
·
verified ·
1 Parent(s): 5c69dbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -11,7 +11,7 @@ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
11
  # ===== Hàm dự đoán có progress bar =====
12
  def predict_with_progress(text, progress=gr.Progress(track_tqdm=True)):
13
  if not text.strip():
14
- return "❌ Vui lòng nhập bình luận", 0.0, "gray"
15
 
16
  progress(0.1, desc="Đang xử lý văn bản...")
17
  time.sleep(0.3)
@@ -23,14 +23,13 @@ def predict_with_progress(text, progress=gr.Progress(track_tqdm=True)):
23
  outputs = model(**inputs)
24
  probs = torch.softmax(outputs.logits, dim=1).tolist()[0]
25
 
26
- label = "Toxic" if probs[1] > probs[0] else "Non-toxic"
27
  confidence = max(probs)
28
 
29
  progress(0.9, desc="Hoàn tất!")
30
  time.sleep(0.2)
31
 
32
- color = "red" if label == "Toxic" else "green"
33
- return f"🔹 **Kết quả:** {label}", confidence, color
34
 
35
 
36
  # ===== Giao diện Gradio =====
@@ -38,32 +37,27 @@ with gr.Blocks(title="🛡️ Toxic Comment Detector") as demo:
38
  gr.Markdown("## 🧠 Phân loại bình luận độc hại (Toxic Comment Detector)")
39
  gr.Markdown(
40
  "Nhập một đoạn bình luận bằng tiếng Việt để kiểm tra mức độ độc hại.\n\n"
41
- "Mô hình sử dụng: `vijjj1/toxic-comment-phobert`"
42
  )
43
 
44
- with gr.Row():
45
- txt_input = gr.Textbox(
46
- label="Nhập bình luận cần kiểm tra",
47
- placeholder="Ví dụ: 'Mày ngu như bò vậy!'",
48
- lines=3
49
- )
50
 
51
  btn = gr.Button("🚀 Phân tích bình luận", variant="primary")
52
- output_label = gr.Markdown(label="Kết quả")
53
- prob_bar = gr.Progress(label="Độ tin cậy", value=0)
54
 
55
- with gr.Row():
56
- conf_bar = gr.Slider(
57
- minimum=0, maximum=1, value=0, step=0.01, label="Độ tin cậy (Confidence)", interactive=False
58
- )
59
 
60
- # Khi bấm nút
61
  btn.click(
62
  fn=predict_with_progress,
63
- inputs=[txt_input],
64
- outputs=[output_label, conf_bar, None]
65
  )
66
 
67
- # Chạy local hoặc deploy HF Spaces
68
  if __name__ == "__main__":
69
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
11
  # ===== Hàm dự đoán có progress bar =====
12
  def predict_with_progress(text, progress=gr.Progress(track_tqdm=True)):
13
  if not text.strip():
14
+ return "❌ Vui lòng nhập bình luận", 0.0
15
 
16
  progress(0.1, desc="Đang xử lý văn bản...")
17
  time.sleep(0.3)
 
23
  outputs = model(**inputs)
24
  probs = torch.softmax(outputs.logits, dim=1).tolist()[0]
25
 
26
+ label = "🧨 Toxic" if probs[1] > probs[0] else "Non-toxic"
27
  confidence = max(probs)
28
 
29
  progress(0.9, desc="Hoàn tất!")
30
  time.sleep(0.2)
31
 
32
+ return f"**Kết quả:** {label}", confidence
 
33
 
34
 
35
  # ===== Giao diện Gradio =====
 
37
  gr.Markdown("## 🧠 Phân loại bình luận độc hại (Toxic Comment Detector)")
38
  gr.Markdown(
39
  "Nhập một đoạn bình luận bằng tiếng Việt để kiểm tra mức độ độc hại.\n\n"
40
+ "Mô hình: **vijjj1/toxic-comment-phobert**"
41
  )
42
 
43
+ txt_input = gr.Textbox(
44
+ label="Nhập bình luận cần kiểm tra",
45
+ placeholder=" dụ: 'Mày ngu như bò vậy!'",
46
+ lines=3
47
+ )
 
48
 
49
  btn = gr.Button("🚀 Phân tích bình luận", variant="primary")
 
 
50
 
51
+ output_label = gr.Markdown(label="Kết quả")
52
+ conf_bar = gr.Slider(
53
+ minimum=0, maximum=1, value=0, step=0.01, label="Độ tin cậy (Confidence)", interactive=False
54
+ )
55
 
 
56
  btn.click(
57
  fn=predict_with_progress,
58
+ inputs=txt_input,
59
+ outputs=[output_label, conf_bar]
60
  )
61
 
 
62
  if __name__ == "__main__":
63
  demo.launch(server_name="0.0.0.0", server_port=7860)