Spaces:
Sleeping
Sleeping
| from hazm import stopwords_list, Lemmatizer, Normalizer | |
| import gradio as gr | |
| import numpy as np | |
| import pickle | |
| from huggingface_hub import hf_hub_download | |
| # Load model | |
| model_path = hf_hub_download( | |
| repo_id="sirunchained/bad-comment", | |
| filename="offensive_text_classifier.pkl" | |
| ) | |
| with open(model_path, "rb") as f: | |
| pickle_model = pickle.load(f) | |
| stopwords = stopwords_list() | |
| lemmatizer = Lemmatizer() | |
| normalizer = Normalizer() | |
| def clean_sentences(sentence: str) -> str: | |
| normalized = normalizer.normalize(sentence) | |
| return " ".join( | |
| lemmatizer.lemmatize(word) | |
| for word in normalized.split() | |
| if word not in stopwords | |
| ) | |
| def predict_with_model(comment: str) -> str: | |
| cleaned_comment = clean_sentences(comment) | |
| output_pred = pickle_model.predict([cleaned_comment]) | |
| output_proba = pickle_model.predict_proba([cleaned_comment]) | |
| label = "Offensive" if output_pred[0] == 1 else "Neutral" | |
| confidence = output_proba.max() | |
| return f"{confidence:.2f} : {label}" | |
| iface = gr.Interface( | |
| fn=predict_with_model, | |
| inputs=gr.Textbox(lines=2, placeholder="Enter a comment here..."), | |
| outputs="text", | |
| title="Persian Offensive Comment Classifier", | |
| description="Enter a Persian comment to classify it." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |