Spaces:
Sleeping
Sleeping
| import os | |
| import joblib | |
| import pandas as pd | |
| import streamlit as st | |
| st.set_page_config( | |
| page_title="Toxic Comment Classifier", | |
| page_icon="☣️", | |
| layout="centered" | |
| ) | |
| BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| MODEL_PATH = os.path.join(BASE_DIR, "toxic_model.pkl") | |
| VECTORIZER_PATH = os.path.join(BASE_DIR, "toxic_vectorizer.pkl") | |
| COLUMNS_PATH = os.path.join(BASE_DIR, "toxic_columns.pkl") | |
| def load_artifacts(): | |
| model = joblib.load(MODEL_PATH) | |
| vectorizer = joblib.load(VECTORIZER_PATH) | |
| label_columns = joblib.load(COLUMNS_PATH) | |
| return model, vectorizer, label_columns | |
| model, vectorizer, label_columns = load_artifacts() | |
| st.title("☣️ Toxic Comment Classifier") | |
| st.write( | |
| "Enter a comment and predict whether it belongs to one or more toxicity categories." | |
| ) | |
| comment = st.text_area( | |
| "Comment Text", | |
| placeholder="Type or paste a comment here..." | |
| ) | |
| if st.button("Predict"): | |
| if not comment.strip(): | |
| st.warning("Please enter a comment.") | |
| else: | |
| text_vec = vectorizer.transform([comment]) | |
| pred = model.predict(text_vec)[0] | |
| result_df = pd.DataFrame({ | |
| "Label": label_columns, | |
| "Prediction": pred | |
| }) | |
| st.subheader("Prediction Results") | |
| st.dataframe(result_df, use_container_width=True) | |
| positive_labels = result_df.loc[result_df["Prediction"] == 1, "Label"].tolist() | |
| if positive_labels: | |
| st.error("Detected labels: " + ", ".join(positive_labels)) | |
| else: | |
| st.success("No toxic label detected.") | |
| st.subheader("Quick Summary") | |
| st.write( | |
| { | |
| "toxic_labels_count": int(result_df["Prediction"].sum()), | |
| "labels_detected": positive_labels | |
| } | |
| ) |