File size: 2,204 Bytes
f50254b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import streamlit as st
from sentence_transformers import SentenceTransformer
import joblib
import numpy as np
# ---------------------------------------------------
# Load SBERT + Classifier + LabelEncoder
# ---------------------------------------------------
@st.cache_resource
def load_models():
# Must match training model exactly
embedder = SentenceTransformer("sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2")
# Load classifier
classifier = joblib.load("classifier.pkl")
# Load label encoder
label_encoder = joblib.load("label_encoder.pkl")
return embedder, classifier, label_encoder
embedder, classifier, label_encoder = load_models()
# ---------------------------------------------------
# Streamlit UI
# ---------------------------------------------------
st.set_page_config(page_title="Trademark Class Predictor", page_icon="π")
st.title("π NICE Class Predictor (3, 5, Both)")
description = st.text_area("Enter product description:", height=150)
if st.button("Predict Class"):
if description.strip() == "":
st.warning("β οΈ Please enter a valid description.")
else:
# Embed the input
emb = embedder.encode([description])
# Predict (numeric)
pred_num = classifier.predict(emb)[0]
# Convert to readable class label
pred_label = label_encoder.inverse_transform([pred_num])[0]
# Show result
st.subheader("Prediction:")
if pred_label == "3_only":
st.success("π§΄ Class 3 β Cosmetics & Cleaning Preparations")
elif pred_label == "5_only":
st.success("π Class 5 β Pharmaceutical & Medical Products")
elif pred_label == "both":
st.success("π Both β Mixed Class 3 + Class 5")
else:
st.error("Unknown label.")
# Confidence scores
if hasattr(classifier, "predict_proba"):
proba = classifier.predict_proba(emb)[0]
st.write("### Confidence Scores:")
for cls, p in zip(label_encoder.classes_, proba):
st.write(f"- **{cls}**: `{p:.3f}`")
|