Spaces:
Sleeping
Sleeping
File size: 3,087 Bytes
0567c9f 226bed4 0567c9f dbfb384 f585727 dbfb384 6e629f4 0567c9f 226bed4 3b908e2 0567c9f 3b908e2 226bed4 0567c9f 226bed4 0567c9f dbfb384 226bed4 dbfb384 226bed4 dbfb384 0567c9f 226bed4 0567c9f 226bed4 0567c9f 2f231d7 0567c9f dbfb384 226bed4 0567c9f dbfb384 226bed4 0567c9f 226bed4 dbfb384 0567c9f |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# ============================================================
# 🌍 Multi-Lingual Sentiment Analysis (English + Persian)
# Simple & Clean Deployment (No SHAP Visualization)
# ============================================================
import gradio as gr
import joblib
import numpy as np
# ------------------------------------------------------------
# 1️⃣ Load Pretrained Models and Vectorizers
# ------------------------------------------------------------
try:
english_model = joblib.load("best_model_english.pkl")
english_vectorizer = joblib.load("tfidf_vectorizer_english.pkl")
persian_model = joblib.load("best_model_persian.pkl")
persian_vectorizer = joblib.load("tfidf_vectorizer_persian.pkl")
print("✅ Models and vectorizers loaded successfully!")
except Exception as e:
raise RuntimeError(f"❌ Error loading models: {e}")
# Define class labels
english_labels = ["Negative", "Neutral", "Positive"]
persian_labels = ["منفی", "خنثی", "مثبت"]
# ------------------------------------------------------------
# 2️⃣ Prediction Function (No SHAP)
# ------------------------------------------------------------
def predict_sentiment(text, language):
if not text.strip():
return "⚠️ Please enter a comment to analyze."
try:
if language == "English":
model, vectorizer, labels = english_model, english_vectorizer, english_labels
else:
model, vectorizer, labels = persian_model, persian_vectorizer, persian_labels
X_input = vectorizer.transform([text])
probs = model.predict_proba(X_input)[0]
pred_idx = np.argmax(probs)
pred_class = labels[pred_idx]
conf = probs[pred_idx]
# Output explanation
explanation = f"""
### 🧠 Predicted Sentiment: **{pred_class}**
**Confidence:** {conf:.2f}
"""
return explanation
except Exception as e:
return f"❌ Error during prediction: {str(e)}"
# ------------------------------------------------------------
# 3️⃣ Gradio Interface
# ------------------------------------------------------------
title = "🌐 Multi-Lingual Sentiment Analysis (English + Persian)"
description = """
Select a language, type a comment, and get the predicted sentiment instantly.
"""
examples = [
["I love this product! Highly recommend.", "English"],
["Bad experience ever, totally disappointed.", "English"],
["The service was okay, nothing special.", "English"],
["این محصول فوقالعاده است", "Persian"],
["تجربهی بدی بود، ناراضیام", "Persian"],
["کیفیتش متوسط بود", "Persian"]
]
demo = gr.Interface(
fn=predict_sentiment,
inputs=[
gr.Textbox(lines=3, label="Enter your comment"),
gr.Radio(["English", "Persian"], label="Choose Dataset/Language", value="English")
],
outputs=gr.Markdown(label="Prediction"),
title=title,
description=description,
examples=examples,
theme="gradio/soft"
)
if __name__ == "__main__":
demo.launch()
|