Spaces:
Sleeping
Sleeping
| # ===================================================== | |
| # π Multi-lingual Sentiment Analyzer (English + Persian) | |
| # ===================================================== | |
| import gradio as gr | |
| import joblib | |
| import numpy as np | |
| import shap | |
| import os | |
| # ----------------------------------------------------- | |
| # β Load Models and Vectorizers | |
| # ----------------------------------------------------- | |
| english_model = joblib.load("best_model_english") | |
| english_vectorizer = joblib.load("tfidf_vectorizer_english") | |
| persian_model = joblib.load("best_model_persian") | |
| persian_vectorizer = joblib.load("tfidf_vectorizer_persian") | |
| # ----------------------------------------------------- | |
| # β Label mapping | |
| # ----------------------------------------------------- | |
| label_map = {0: "Negative", 1: "Neutral", 2: "Positive"} | |
| # ----------------------------------------------------- | |
| # β Prediction Function | |
| # ----------------------------------------------------- | |
| def predict_sentiment(text, lang): | |
| if not text.strip(): | |
| return "β οΈ Please enter some text.", "", "", "" | |
| # Select appropriate model and vectorizer | |
| if lang == "English": | |
| vectorizer = eng_vectorizer | |
| model = eng_model | |
| else: | |
| vectorizer = per_vectorizer | |
| model = per_model | |
| # Vectorize text | |
| X = vectorizer.transform([text]) | |
| pred = model.predict(X)[0] | |
| probs = model.predict_proba(X)[0] | |
| conf = np.max(probs) | |
| sentiment = label_map.get(pred, "Unknown") | |
| # SHAP explanation | |
| try: | |
| explainer = shap.LinearExplainer(model, X, feature_dependence="independent") | |
| shap_values = explainer.shap_values(X) | |
| shap_html = shap.plots.text(explainer, X, display=False) | |
| except Exception: | |
| shap_html = "<p>β οΈ No explanation available for this input.</p>" | |
| return f"Prediction: {sentiment}", f"Confidence: {conf:.3f}", shap_html, "" | |
| # ----------------------------------------------------- | |
| # β Build Gradio Interface | |
| # ----------------------------------------------------- | |
| with gr.Blocks(theme=gr.themes.Soft()) as app: | |
| gr.Markdown("<h2 style='text-align:center;'>π Multi-lingual Sentiment (English + Persian)</h2>") | |
| with gr.Row(): | |
| comment = gr.Textbox(label="Comment", placeholder="Type your comment here...") | |
| lang = gr.Radio(["English", "Persian"], label="Language", value="English") | |
| predict_btn = gr.Button("Predict", variant="primary") | |
| output1 = gr.Textbox(label="Prediction") | |
| output2 = gr.Textbox(label="Confidence") | |
| output3 = gr.HTML(label="Explanation") | |
| predict_btn.click( | |
| predict_sentiment, | |
| inputs=[comment, lang], | |
| outputs=[output1, output2, output3, gr.Textbox(visible=False)] | |
| ) | |
| # ----------------------------------------------------- | |
| # β Launch App | |
| # ----------------------------------------------------- | |
| if __name__ == "__main__": | |
| app.launch() | |