hoom4n commited on
Commit
62edb7d
·
verified ·
1 Parent(s): 7eb4475

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +89 -0
  2. requirements.txt +5 -0
  3. sentiment_pipeline.joblib +3 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import nltk
3
+ import joblib
4
+
5
+ nltk.download("punkt")
6
+ nltk.download("wordnet")
7
+ nltk.download('punkt_tab')
8
+
9
+ model = joblib.load("sentiment_pipeline.joblib")
10
+
11
+ def predict_sentiment(text, show_preprocessed=False):
12
+ proba = model.predict_proba([text])[0]
13
+ sentiment = "Positive 😀" if proba[1] >= 0.5 else "Negative 😞"
14
+ confidence = f"{round(max(proba) * 100, 2)}%"
15
+ pre_out = ""
16
+ if show_preprocessed:
17
+ pre_out = model.named_steps["textpreprocessor"].transform([text])[0]
18
+ return sentiment, confidence, pre_out
19
+
20
+ with gr.Blocks(css="""
21
+ .pipeline-container {
22
+ background-color: blue;
23
+ border:1px solid #ddd;
24
+ border-radius:8px;
25
+ padding:8px;
26
+ margin-bottom: 4px;
27
+ }
28
+ .footer {
29
+ margin-top: 24px;
30
+ font-size:0.9rem;
31
+ text-align:center;
32
+ }
33
+ """) as demo:
34
+
35
+ gr.Markdown("# 🎬 SentiMDB")
36
+ gr.Markdown(
37
+ "### SentiMDB is a lightweight, production-ready Sentiment Analysis Pipeline based on IMDb movie reviews. It features a Flask web app, a Dockerized setup for easy deployment, and a Hugging Face Spaces-powered online demo. The project includes a comprehensive Jupyter Notebook, offering a guide to English Text Preprocessing and detailing the full Machine Learning Development process, including Model Selection, Error Analysis, and Fine-Tuning. By leveraging classic machine learning tools alone, the model achieved 91.67% prediction accuracy."
38
+ )
39
+
40
+
41
+ with gr.Row():
42
+ with gr.Column(scale=1):
43
+
44
+
45
+ gr.HTML("""
46
+ <div class="pipeline-container">
47
+ <h4 style="text-align:center; margin:0 0 8px 0;">Pipeline</h4>
48
+ <div style="display:flex; justify-content:space-around; align-items:center;">
49
+ <div>📝 Input Text</div>
50
+ <div>→</div>
51
+ <div>🔧 TextPreprocessor</div>
52
+ <div>→</div>
53
+ <div>📊 TF‑IDF</div>
54
+ <div>→</div>
55
+ <div>🤖 Logistic Regressor</div>
56
+ </div>
57
+ </div>
58
+ """)
59
+
60
+
61
+
62
+ review = gr.Textbox(lines=3, placeholder="Type your movie review here…")
63
+ show_pre = gr.Checkbox(label="Show Preprocessed text", value=True)
64
+ analyze_btn = gr.Button("Analyze", variant="primary")
65
+
66
+
67
+
68
+ with gr.Column(scale=1):
69
+
70
+ sentiment_out = gr.Label(label="Sentiment")
71
+ confidence_out = gr.Textbox(label="Confidence")
72
+ pre_out = gr.Textbox(label="Preprocessed Text", interactive=False)
73
+
74
+
75
+
76
+
77
+ analyze_btn.click(
78
+ fn=predict_sentiment,
79
+ inputs=[review, show_pre],
80
+ outputs=[sentiment_out, confidence_out, pre_out]
81
+ )
82
+
83
+
84
+ gr.HTML(
85
+ '<div class="footer">For the full project Jupyter Notebook, Flask Web App & Docker Config, visit: <a href="https://github.com/Hoom4n/SentiMDB" target="_blank">https://github.com/Hoom4n/SentiMDB</a></div>'
86
+ )
87
+
88
+ if __name__ == "__main__":
89
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ scikit-learn==1.6.1
2
+ joblib==1.5.0
3
+ hoomanmltk==0.1.0
4
+ nltk==3.9.1
5
+ gradio
sentiment_pipeline.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ca2d97da646a42395dc8f2bb887cea2cdbd5ae39b8f52914f5c15f86696e967
3
+ size 1823252