jeevitha-app commited on
Commit
6ae834c
Β·
verified Β·
1 Parent(s): e2849a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -70
app.py CHANGED
@@ -1,84 +1,110 @@
1
- # =====================================================
2
- # 🌐 Multi-lingual Sentiment Analyzer (English + Persian)
3
- # =====================================================
4
 
5
  import gradio as gr
6
  import joblib
7
- import numpy as np
8
  import shap
9
- import os
10
-
11
- # -----------------------------------------------------
12
- # βœ… Load Models and Vectorizers
13
- # -----------------------------------------------------
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
 
21
- # -----------------------------------------------------
22
- # βœ… Label mapping
23
- # -----------------------------------------------------
24
- label_map = {0: "Negative", 1: "Neutral", 2: "Positive"}
25
 
26
- # -----------------------------------------------------
27
- # βœ… Prediction Function
28
- # -----------------------------------------------------
29
- def predict_sentiment(text, lang):
30
  if not text.strip():
31
- return "⚠️ Please enter some text.", "", "", ""
32
-
33
- # Select appropriate model and vectorizer
34
- if lang == "English":
35
- vectorizer = eng_vectorizer
36
  model = eng_model
 
37
  else:
38
- vectorizer = per_vectorizer
39
  model = per_model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- # Vectorize text
42
- X = vectorizer.transform([text])
43
- pred = model.predict(X)[0]
44
- probs = model.predict_proba(X)[0]
45
- conf = np.max(probs)
46
- sentiment = label_map.get(pred, "Unknown")
47
-
48
- # SHAP explanation
49
- try:
50
- explainer = shap.LinearExplainer(model, X, feature_dependence="independent")
51
- shap_values = explainer.shap_values(X)
52
- shap_html = shap.plots.text(explainer, X, display=False)
53
- except Exception:
54
- shap_html = "<p>⚠️ No explanation available for this input.</p>"
55
-
56
- return f"Prediction: {sentiment}", f"Confidence: {conf:.3f}", shap_html, ""
57
-
58
- # -----------------------------------------------------
59
- # βœ… Build Gradio Interface
60
- # -----------------------------------------------------
61
- with gr.Blocks(theme=gr.themes.Soft()) as app:
62
- gr.Markdown("<h2 style='text-align:center;'>🌍 Multi-lingual Sentiment (English + Persian)</h2>")
63
-
64
- with gr.Row():
65
- comment = gr.Textbox(label="Comment", placeholder="Type your comment here...")
66
- lang = gr.Radio(["English", "Persian"], label="Language", value="English")
67
-
68
- predict_btn = gr.Button("Predict", variant="primary")
69
-
70
- output1 = gr.Textbox(label="Prediction")
71
- output2 = gr.Textbox(label="Confidence")
72
- output3 = gr.HTML(label="Explanation")
73
-
74
- predict_btn.click(
75
- predict_sentiment,
76
- inputs=[comment, lang],
77
- outputs=[output1, output2, output3, gr.Textbox(visible=False)]
78
- )
79
-
80
- # -----------------------------------------------------
81
- # βœ… Launch App
82
- # -----------------------------------------------------
 
 
 
83
  if __name__ == "__main__":
84
- app.launch()
 
 
1
+ # ===============================================================
2
+ # 🌍 Sentiment Analysis App β€” English & Persian (Gradio Deployment)
3
+ # ===============================================================
4
 
5
  import gradio as gr
6
  import joblib
 
7
  import shap
8
+ import numpy as np
 
 
 
 
 
 
9
 
10
+ # ------------------------------------------------------------
11
+ # 1️⃣ Load pre-trained models and vectorizers
12
+ # ------------------------------------------------------------
13
+ eng_model = joblib.load("best_model.pkl")
14
+ eng_vectorizer = joblib.load("tfidf_vectorizer.pkl")
15
 
16
+ per_model = joblib.load("logistic_regression.pkl")
17
+ per_vectorizer = joblib.load("tfidf_vectorizer_persian.pkl")
18
 
19
+ # ------------------------------------------------------------
20
+ # 2️⃣ Define class labels
21
+ # ------------------------------------------------------------
22
+ class_names = ["Negative", "Neutral", "Positive"]
23
 
24
+ # ------------------------------------------------------------
25
+ # 3️⃣ Prediction Function
26
+ # ------------------------------------------------------------
27
+ def predict_sentiment(text, language):
28
  if not text.strip():
29
+ return "⚠️ Please enter some text!", None, None
30
+
31
+ if language == "English":
 
 
32
  model = eng_model
33
+ vectorizer = eng_vectorizer
34
  else:
 
35
  model = per_model
36
+ vectorizer = per_vectorizer
37
+
38
+ # Vectorize input
39
+ vec = vectorizer.transform([text])
40
+ probs = model.predict_proba(vec)[0]
41
+ pred_class = np.argmax(probs)
42
+ label = class_names[pred_class]
43
+ confidence = probs[pred_class]
44
+
45
+ # Interpret top words with SHAP
46
+ explainer = shap.Explainer(model, vectorizer.transform(["sample"]))
47
+ feature_names = vectorizer.get_feature_names_out()
48
+ shap_values = explainer(vec)
49
+
50
+ # top contributing words
51
+ shap_vals = shap_values[0].values[:, pred_class]
52
+ top_indices = np.argsort(-np.abs(shap_vals))[:10]
53
+ top_words = [feature_names[i] for i in top_indices]
54
+ top_contribs = shap_vals[top_indices]
55
+
56
+ interpretation = {
57
+ "words": top_words,
58
+ "contributions": top_contribs.tolist()
59
+ }
60
+
61
+ return f"🎯 **{label}** (confidence: {confidence:.2f})", probs.tolist(), interpretation
62
 
63
+
64
+ # ------------------------------------------------------------
65
+ # 4️⃣ Gradio Interface
66
+ # ------------------------------------------------------------
67
+ def gradio_ui(text, language):
68
+ pred, probs, interp = predict_sentiment(text, language)
69
+
70
+ if not probs:
71
+ return pred, None, None
72
+
73
+ # Confidence Bar Plot
74
+ bar_plot = {cls: float(p) for cls, p in zip(class_names, probs)}
75
+
76
+ # Word Contribution Table
77
+ word_table = None
78
+ if interp:
79
+ word_table = {
80
+ "Word": interp["words"],
81
+ "SHAP Impact": interp["contributions"]
82
+ }
83
+
84
+ return pred, bar_plot, word_table
85
+
86
+
87
+ # ------------------------------------------------------------
88
+ # 5️⃣ Gradio Layout
89
+ # ------------------------------------------------------------
90
+ with gr.Blocks(theme=gr.themes.Soft()) as interface:
91
+ gr.Markdown("## 🌍 Sentiment Analysis (English & Persian)
92
+ Select language, enter text, and view predictions with interpretable SHAP insights!")
93
+
94
+ lang = gr.Radio(["English", "Persian"], label="Select Dataset", value="English")
95
+ text = gr.Textbox(label="Enter your text here", placeholder="Type an English or Persian comment...")
96
+
97
+ output_pred = gr.Markdown(label="Prediction")
98
+ output_bar = gr.BarPlot(label="Confidence per Class")
99
+ output_table = gr.Dataframe(label="Top Influential Words", headers=["Word", "SHAP Impact"])
100
+
101
+ btn = gr.Button("πŸ” Analyze Sentiment")
102
+
103
+ btn.click(fn=gradio_ui, inputs=[text, lang], outputs=[output_pred, output_bar, output_table])
104
+
105
+ # ------------------------------------------------------------
106
+ # 6️⃣ Launch App
107
+ # ------------------------------------------------------------
108
  if __name__ == "__main__":
109
+ interface.launch(share=True)
110
+