Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|
| 12 |
+
for img in soup.find_all("img"):
|
| 13 |
+
img.decompose()
|
| 14 |
+
for math_tag in soup.find_all("math"):
|
| 15 |
+
math_tag.decompose()
|
| 16 |
+
return soup.get_text(separator=" ", strip=True)
|
| 17 |
+
|
| 18 |
+
def normalize_text(text: str) -> str:
|
| 19 |
+
"""Lowercase, giữ unicode letters & digits, thay ký tự khác thành space."""
|
| 20 |
+
text = text.lower()
|
| 21 |
+
chars = []
|
| 22 |
+
for ch in text:
|
| 23 |
+
cat = unicodedata.category(ch)
|
| 24 |
+
if cat.startswith("L") or ch.isdigit() or ch.isspace():
|
| 25 |
+
chars.append(ch)
|
| 26 |
+
else:
|
| 27 |
+
chars.append(" ")
|
| 28 |
+
text = "".join(chars)
|
| 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 |
+
# —— 3. Inference function —— #
|
| 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)
|
| 51 |
+
if not text:
|
| 52 |
+
return "Nội dung không chứa ký tự để dự đoán."
|
| 53 |
+
Xv = vect.transform([text])
|
| 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 model Naive Bayes dự đoán.
|
| 69 |
+
""",
|
| 70 |
+
allow_flagging="never",
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
demo.launch()
|