Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import re
|
| 3 |
import unicodedata
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
import joblib
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
# —— 1. Preprocess functions —— #
|
| 9 |
def clean_html(raw_html: str) -> str:
|
| 10 |
"""Loại bỏ <img>, <math>, giữ text thuần."""
|
| 11 |
soup = BeautifulSoup(raw_html, "html.parser")
|
|
@@ -29,22 +27,15 @@ def normalize_text(text: str) -> str:
|
|
| 29 |
# xóa khoảng trắng thừa
|
| 30 |
return re.sub(r"\s+", " ", text).strip()
|
| 31 |
|
| 32 |
-
def preprocess(content_html: str) -> str
|
| 33 |
-
"""Pipeline: HTML → clean → normalize"""
|
| 34 |
text = clean_html(content_html)
|
| 35 |
text = normalize_text(text)
|
| 36 |
return text
|
| 37 |
|
| 38 |
-
# —— 2. Load vectorizer & model —— #
|
| 39 |
vect = joblib.load("vectorizer.joblib")
|
| 40 |
clf = joblib.load("nbc_model.joblib")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
def predict_kc(content_html: str) -> str:
|
| 44 |
-
"""
|
| 45 |
-
Nhận HTML content, trả về mã KC dự đoán.
|
| 46 |
-
Nếu bỏ trống hoặc không parse được, trả về thông báo.
|
| 47 |
-
"""
|
| 48 |
if not content_html or not isinstance(content_html, str):
|
| 49 |
return "Không có input hợp lệ."
|
| 50 |
text = preprocess(content_html)
|
|
@@ -54,18 +45,17 @@ def predict_kc(content_html: str) -> str:
|
|
| 54 |
pred = clf.predict(Xv)[0]
|
| 55 |
return pred
|
| 56 |
|
| 57 |
-
# —— 4. Xây dựng giao diện Gradio —— #
|
| 58 |
demo = gr.Interface(
|
| 59 |
fn=predict_kc,
|
| 60 |
inputs=gr.Textbox(
|
| 61 |
lines=6,
|
| 62 |
-
placeholder="Dán HTML Content (có thể kèm <p>, <img>, <math>) vào đây
|
| 63 |
),
|
| 64 |
outputs=gr.Label(num_top_classes=1, label="Mã KC dự đoán"),
|
| 65 |
title="Naive Bayes KC Predictor",
|
| 66 |
description="""
|
| 67 |
Nhập nội dung câu hỏi (HTML) và nhấn Submit để nhận về
|
| 68 |
-
mã kiến thức (KC) do
|
| 69 |
""",
|
| 70 |
allow_flagging="never",
|
| 71 |
)
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import unicodedata
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import joblib
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
| 7 |
def clean_html(raw_html: str) -> str:
|
| 8 |
"""Loại bỏ <img>, <math>, giữ text thuần."""
|
| 9 |
soup = BeautifulSoup(raw_html, "html.parser")
|
|
|
|
| 27 |
# xóa khoảng trắng thừa
|
| 28 |
return re.sub(r"\s+", " ", text).strip()
|
| 29 |
|
| 30 |
+
def preprocess(content_html: str) -> str
|
|
|
|
| 31 |
text = clean_html(content_html)
|
| 32 |
text = normalize_text(text)
|
| 33 |
return text
|
| 34 |
|
|
|
|
| 35 |
vect = joblib.load("vectorizer.joblib")
|
| 36 |
clf = joblib.load("nbc_model.joblib")
|
| 37 |
|
| 38 |
+
def predict_kc(content_html: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
if not content_html or not isinstance(content_html, str):
|
| 40 |
return "Không có input hợp lệ."
|
| 41 |
text = preprocess(content_html)
|
|
|
|
| 45 |
pred = clf.predict(Xv)[0]
|
| 46 |
return pred
|
| 47 |
|
|
|
|
| 48 |
demo = gr.Interface(
|
| 49 |
fn=predict_kc,
|
| 50 |
inputs=gr.Textbox(
|
| 51 |
lines=6,
|
| 52 |
+
placeholder="Dán HTML Content (có thể kèm <p>, <img>, <math>) vào đây"
|
| 53 |
),
|
| 54 |
outputs=gr.Label(num_top_classes=1, label="Mã KC dự đoán"),
|
| 55 |
title="Naive Bayes KC Predictor",
|
| 56 |
description="""
|
| 57 |
Nhập nội dung câu hỏi (HTML) và nhấn Submit để nhận về
|
| 58 |
+
mã kiến thức (KC) do mô hình Naive Bayes dự đoán.
|
| 59 |
""",
|
| 60 |
allow_flagging="never",
|
| 61 |
)
|