jeevitha-app commited on
Commit
226bed4
·
verified ·
1 Parent(s): cdc1936

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -64
app.py CHANGED
@@ -1,89 +1,65 @@
1
  # ============================================================
2
  # 🌍 Multi-Lingual Sentiment Analysis (English + Persian)
3
- # With SHAP Interpretability
4
  # ============================================================
5
 
6
  import gradio as gr
7
  import joblib
8
  import numpy as np
9
- import shap
10
- import matplotlib.pyplot as plt
11
- import os
12
 
13
  # ------------------------------------------------------------
14
  # 1️⃣ Load Pretrained Models and Vectorizers
15
  # ------------------------------------------------------------
16
- english_model = joblib.load("best_model.pkl")
17
- english_vectorizer = joblib.load("tfidf_vectorizer.pkl")
 
18
 
19
- persian_model = joblib.load("logistic_regression.pkl")
20
- persian_vectorizer = joblib.load("tfidf_vectorizer_persian.pkl")
 
 
 
 
21
 
22
  # Define class labels
23
  english_labels = ["Negative", "Neutral", "Positive"]
24
  persian_labels = ["منفی", "خنثی", "مثبت"]
25
 
26
  # ------------------------------------------------------------
27
- # 2️⃣ SHAP Visualization Function
28
- # ------------------------------------------------------------
29
- def get_shap_plot(model, vectorizer, text, class_index, class_name):
30
- X_input = vectorizer.transform([text])
31
- explainer = shap.Explainer(model, vectorizer.transform([" ".join(text.split()[:50])]))
32
- shap_values = explainer(X_input)
33
- shap_for_class = shap_values.values[0][:, class_index]
34
- feature_names = np.array(vectorizer.get_feature_names_out())
35
-
36
- top_idx = np.argsort(-np.abs(shap_for_class))[:10]
37
- top_words = feature_names[top_idx]
38
- top_impacts = shap_for_class[top_idx]
39
-
40
- plt.figure(figsize=(6, 3))
41
- colors = ["crimson" if v > 0 else "steelblue" for v in top_impacts]
42
- plt.barh(top_words, top_impacts, color=colors)
43
- plt.title(f"Top Words driving {class_name} prediction")
44
- plt.xlabel("SHAP Value (Impact)")
45
- plt.gca().invert_yaxis()
46
- plt.tight_layout()
47
- plt.savefig("shap_plot.png", bbox_inches='tight')
48
- plt.close()
49
-
50
- return top_words.tolist(), "shap_plot.png"
51
-
52
- # ------------------------------------------------------------
53
- # 3️⃣ Prediction + Interpretability Function
54
  # ------------------------------------------------------------
55
  def predict_sentiment(text, language):
56
  if not text.strip():
57
- return "Please enter a comment.", None
58
-
59
- if language == "English":
60
- model, vectorizer, labels = english_model, english_vectorizer, english_labels
61
- else:
62
- model, vectorizer, labels = persian_model, persian_vectorizer, persian_labels
63
-
64
- X_input = vectorizer.transform([text])
65
- probs = model.predict_proba(X_input)[0]
66
- pred_idx = np.argmax(probs)
67
- pred_class = labels[pred_idx]
68
- conf = probs[pred_idx]
69
-
70
- # SHAP interpretation
71
- top_words, shap_plot = get_shap_plot(model, vectorizer, text, pred_idx, pred_class)
72
-
73
- # Final output
74
- explanation = f"""
75
- **Predicted Sentiment:** {pred_class}
76
- **Confidence:** {conf:.2f}
77
- **Top Influential Words:** {', '.join(top_words)}
78
  """
79
- return explanation, shap_plot
 
 
 
80
 
81
  # ------------------------------------------------------------
82
- # 4️⃣ Gradio Interface
83
  # ------------------------------------------------------------
84
  title = "🌐 Multi-Lingual Sentiment Analysis (English + Persian)"
85
  description = """
86
- Select a language, type a comment, and see both the sentiment prediction and SHAP interpretability.
87
  """
88
 
89
  examples = [
@@ -98,16 +74,14 @@ examples = [
98
  demo = gr.Interface(
99
  fn=predict_sentiment,
100
  inputs=[
101
- gr.Textbox(lines=3, label="Enter comment"),
102
  gr.Radio(["English", "Persian"], label="Choose Dataset/Language", value="English")
103
  ],
104
- outputs=[
105
- gr.Markdown(label="Prediction & Explanation"),
106
- gr.Image(label="Top Word Contributions")
107
- ],
108
  title=title,
109
  description=description,
110
  examples=examples,
 
111
  )
112
 
113
  if __name__ == "__main__":
 
1
  # ============================================================
2
  # 🌍 Multi-Lingual Sentiment Analysis (English + Persian)
3
+ # Simple & Clean Deployment (No SHAP Visualization)
4
  # ============================================================
5
 
6
  import gradio as gr
7
  import joblib
8
  import numpy as np
 
 
 
9
 
10
  # ------------------------------------------------------------
11
  # 1️⃣ Load Pretrained Models and Vectorizers
12
  # ------------------------------------------------------------
13
+ try:
14
+ english_model = joblib.load("best_model.pkl")
15
+ english_vectorizer = joblib.load("tfidf_vectorizer.pkl")
16
 
17
+ persian_model = joblib.load("logistic_regression.pkl")
18
+ persian_vectorizer = joblib.load("tfidf_vectorizer_persian.pkl")
19
+
20
+ print("✅ Models and vectorizers loaded successfully!")
21
+ except Exception as e:
22
+ raise RuntimeError(f"❌ Error loading models: {e}")
23
 
24
  # Define class labels
25
  english_labels = ["Negative", "Neutral", "Positive"]
26
  persian_labels = ["منفی", "خنثی", "مثبت"]
27
 
28
  # ------------------------------------------------------------
29
+ # 2️⃣ Prediction Function (No SHAP)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # ------------------------------------------------------------
31
  def predict_sentiment(text, language):
32
  if not text.strip():
33
+ return "⚠️ Please enter a comment to analyze."
34
+
35
+ try:
36
+ if language == "English":
37
+ model, vectorizer, labels = english_model, english_vectorizer, english_labels
38
+ else:
39
+ model, vectorizer, labels = persian_model, persian_vectorizer, persian_labels
40
+
41
+ X_input = vectorizer.transform([text])
42
+ probs = model.predict_proba(X_input)[0]
43
+ pred_idx = np.argmax(probs)
44
+ pred_class = labels[pred_idx]
45
+ conf = probs[pred_idx]
46
+
47
+ # Output explanation
48
+ explanation = f"""
49
+ ### 🧠 Predicted Sentiment: **{pred_class}**
50
+ **Confidence:** {conf:.2f}
 
 
 
51
  """
52
+ return explanation
53
+
54
+ except Exception as e:
55
+ return f"❌ Error during prediction: {str(e)}"
56
 
57
  # ------------------------------------------------------------
58
+ # 3️⃣ Gradio Interface
59
  # ------------------------------------------------------------
60
  title = "🌐 Multi-Lingual Sentiment Analysis (English + Persian)"
61
  description = """
62
+ Select a language, type a comment, and get the predicted sentiment instantly.
63
  """
64
 
65
  examples = [
 
74
  demo = gr.Interface(
75
  fn=predict_sentiment,
76
  inputs=[
77
+ gr.Textbox(lines=3, label="Enter your comment"),
78
  gr.Radio(["English", "Persian"], label="Choose Dataset/Language", value="English")
79
  ],
80
+ outputs=gr.Markdown(label="Prediction"),
 
 
 
81
  title=title,
82
  description=description,
83
  examples=examples,
84
+ theme="gradio/soft"
85
  )
86
 
87
  if __name__ == "__main__":