gaurav0026 commited on
Commit
1861684
·
verified ·
1 Parent(s): 03a049c

Update gradio_app.py

Browse files

updated the ui using gradio file

Files changed (1) hide show
  1. gradio_app.py +253 -253
gradio_app.py CHANGED
@@ -1,254 +1,254 @@
1
- import gradio as gr
2
- import pickle as pk
3
- import pandas as pd
4
- from sklearn.feature_extraction.text import TfidfVectorizer
5
- import os
6
-
7
- # Get the absolute path of the current directory
8
- current_dir = os.path.dirname(os.path.abspath(__file__))
9
- model_path = os.path.join(current_dir, 'model.pkl')
10
- scaler_path = os.path.join(current_dir, 'scaler.pkl')
11
-
12
- def load_model_and_vectorizer():
13
- try:
14
- if not os.path.exists(model_path) or not os.path.exists(scaler_path):
15
- return None, None, f"Error: Model files not found at:\n{model_path}\n{scaler_path}"
16
-
17
- model = pk.load(open(model_path, 'rb'))
18
- vectorizer = pk.load(open(scaler_path, 'rb'))
19
- return model, vectorizer, None
20
- except Exception as e:
21
- return None, None, f"Error loading model: {str(e)}"
22
-
23
- # Load the model and vectorizer
24
- model, vectorizer, error = load_model_and_vectorizer()
25
-
26
- def predict_sentiment(review):
27
- if error:
28
- return error
29
-
30
- if not review:
31
- return "Please enter a review first!"
32
-
33
- try:
34
- # Vectorize the text
35
- review_vector = vectorizer.transform([review])
36
- # Make prediction
37
- result = model.predict(review_vector)[0]
38
-
39
- if result == 0:
40
- return "Negative Review 😞"
41
- else:
42
- return "Positive Review 😊"
43
- except Exception as e:
44
- return f"Error during prediction: {str(e)}"
45
-
46
- # Modern and aesthetic CSS adapted from the dashboard
47
- css = """
48
- body {
49
- background: linear-gradient(180deg, #2c3e50 0%, #3498db 100%);
50
- margin: 0;
51
- font-family: 'Roboto', -apple-system, BlinkMacSystemFont, sans-serif;
52
- color: #ecf0f1;
53
- }
54
-
55
- .gradio-container {
56
- max-width: 800px;
57
- margin: 3rem auto;
58
- background: rgba(44, 62, 80, 0.9);
59
- border-radius: 20px;
60
- padding: 2.5rem;
61
- box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
62
- backdrop-filter: blur(10px);
63
- transition: transform 0.3s ease;
64
- }
65
-
66
- .gradio-container:hover {
67
- transform: translateY(-5px);
68
- }
69
-
70
- .header {
71
- text-align: center;
72
- margin-bottom: 2rem;
73
- animation: fadeIn 1s ease-in-out;
74
- }
75
-
76
- .header h3 {
77
- font-size: 2.4rem;
78
- background: linear-gradient(45deg, #e74c3c, #f1c40f);
79
- -webkit-background-clip: text;
80
- -webkit-text-fill-color: transparent;
81
- margin: 0;
82
- font-weight: 700;
83
- }
84
-
85
- .header p {
86
- font-size: 1.2rem;
87
- color: #bdc3c7;
88
- margin-top: 0.5rem;
89
- font-weight: 300;
90
- }
91
-
92
- .review-input {
93
- background: rgba(255, 255, 255, 0.1);
94
- border: 2px solid #4ECDC4;
95
- border-radius: 12px;
96
- padding: 1.5rem;
97
- font-size: 1.1rem;
98
- color: #ecf0f1;
99
- width: 100%;
100
- box-sizing: border-box;
101
- box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
102
- transition: all 0.3s ease;
103
- }
104
-
105
- .review-input:focus {
106
- border-color: #f1c40f;
107
- box-shadow: 0 6px 20px rgba(241, 196, 15, 0.3);
108
- outline: none;
109
- transform: scale(1.01);
110
- }
111
-
112
- .review-input::placeholder {
113
- color: #95a5a6;
114
- font-style: italic;
115
- }
116
-
117
- .submit-btn {
118
- background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
119
- color: white;
120
- border: none;
121
- padding: 1rem 3rem;
122
- border-radius: 50px;
123
- font-size: 1.1rem;
124
- font-weight: 600;
125
- cursor: pointer;
126
- margin: 1.5rem auto;
127
- display: block;
128
- transition: all 0.3s ease;
129
- box-shadow: 0 6px 20px rgba(78, 205, 196, 0.4);
130
- }
131
-
132
- .submit-btn:hover {
133
- background: linear-gradient(45deg, #4ECDC4, #FF6B6B);
134
- transform: translateY(-3px) scale(1.05);
135
- box-shadow: 0 10px 25px rgba(78, 205, 196, 0.5);
136
- }
137
-
138
- .submit-btn:active {
139
- transform: translateY(0);
140
- box-shadow: 0 4px 15px rgba(78, 205, 196, 0.3);
141
- }
142
-
143
- .output-container {
144
- margin-top: 2rem;
145
- padding: 1.5rem;
146
- background: rgba(255, 255, 255, 0.05);
147
- border-radius: 15px;
148
- text-align: center;
149
- min-height: 80px;
150
- font-size: 1.3rem;
151
- color: #f1c40f;
152
- box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
153
- animation: slideUp 0.5s ease-out;
154
- }
155
-
156
- .error-message {
157
- background: rgba(231, 76, 60, 0.2);
158
- color: #e74c3c;
159
- padding: 1.2rem;
160
- border-radius: 12px;
161
- margin-bottom: 1.5rem;
162
- border-left: 5px solid #e74c3c;
163
- font-weight: 500;
164
- animation: shake 0.4s ease-in-out;
165
- }
166
-
167
- @keyframes fadeIn {
168
- from { opacity: 0; transform: translateY(-10px); }
169
- to { opacity: 1; transform: translateY(0); }
170
- }
171
-
172
- @keyframes slideUp {
173
- from { opacity: 0; transform: translateY(20px); }
174
- to { opacity: 1; transform: translateY(0); }
175
- }
176
-
177
- @keyframes shake {
178
- 0%, 100% { transform: translateX(0); }
179
- 20%, 60% { transform: translateX(-8px); }
180
- 40%, 80% { transform: translateX(8px); }
181
- }
182
-
183
- @media (max-width: 600px) {
184
- .gradio-container {
185
- margin: 1rem;
186
- padding: 1.5rem;
187
- }
188
-
189
- .header h1 {
190
- font-size: 2rem;
191
- }
192
-
193
- .review-input {
194
- padding: 1rem;
195
- }
196
-
197
- .submit-btn {
198
- padding: 0.8rem 2rem;
199
- }
200
- }
201
- """
202
-
203
- # Create the Gradio interface
204
- with gr.Blocks(
205
- title="Sentiment Analysis on Traveler Reviews",
206
- theme=gr.themes.Default(
207
- primary_hue="teal",
208
- secondary_hue="pink",
209
- neutral_hue="gray",
210
- radius_size="lg",
211
- ),
212
- css=css
213
- ) as demo:
214
- gr.Markdown("""
215
- <div class="header">
216
- <h3> Sentiment Analysis on Traveler Reviews</h3>
217
- <p>Type your review and let our AI uncover its vibe!</p>
218
- </div>
219
- """)
220
-
221
- if error:
222
- gr.Markdown(f"""
223
- <div class="error-message">
224
- ⚠️ {error}
225
- </div>
226
- """)
227
-
228
- review_input = gr.Textbox(
229
- placeholder="Share your thoughts about the your experience...",
230
- lines=5,
231
- elem_classes="review-input",
232
- show_label=False
233
- )
234
-
235
- predict_btn = gr.Button(
236
- "Analyze Sentiment",
237
- elem_classes="submit-btn"
238
- )
239
-
240
- output = gr.Textbox(
241
- placeholder="The sentiment will appear here...",
242
- elem_classes="output-container",
243
- show_label=False,
244
- interactive=False
245
- )
246
-
247
- predict_btn.click(
248
- fn=predict_sentiment,
249
- inputs=review_input,
250
- outputs=output
251
- )
252
-
253
- if __name__ == "__main__":
254
  demo.launch(share=True)
 
1
+ import gradio as gr
2
+ import pickle as pk
3
+ import pandas as pd
4
+ from sklearn.feature_extraction.text import TfidfVectorizer
5
+ import os
6
+
7
+ # Get the absolute path of the current directory
8
+ current_dir = os.path.dirname(os.path.abspath(__file__))
9
+ model_path = os.path.join(current_dir, 'model.pkl')
10
+ scaler_path = os.path.join(current_dir, 'scaler.pkl')
11
+
12
+ def load_model_and_vectorizer():
13
+ try:
14
+ if not os.path.exists(model_path) or not os.path.exists(scaler_path):
15
+ return None, None, f"Error: Model files not found at:\n{model_path}\n{scaler_path}"
16
+
17
+ model = pk.load(open(model_path, 'rb'))
18
+ vectorizer = pk.load(open(scaler_path, 'rb'))
19
+ return model, vectorizer, None
20
+ except Exception as e:
21
+ return None, None, f"Error loading model: {str(e)}"
22
+
23
+ # Load the model and vectorizer
24
+ model, vectorizer, error = load_model_and_vectorizer()
25
+
26
+ def predict_sentiment(review):
27
+ if error:
28
+ return error
29
+
30
+ if not review:
31
+ return "Please enter a review first!"
32
+
33
+ try:
34
+ # Vectorize the text
35
+ review_vector = vectorizer.transform([review])
36
+ # Make prediction
37
+ result = model.predict(review_vector)[0]
38
+
39
+ if result == 0:
40
+ return "Negative Review 😞"
41
+ else:
42
+ return "Positive Review 😊"
43
+ except Exception as e:
44
+ return f"Error during prediction: {str(e)}"
45
+
46
+ # Modern and aesthetic CSS adapted from the dashboard
47
+ css = """
48
+ body {
49
+ background: linear-gradient(180deg, #2c3e50 0%, #3498db 100%);
50
+ margin: 0;
51
+ font-family: 'Roboto', -apple-system, BlinkMacSystemFont, sans-serif;
52
+ color: #ecf0f1;
53
+ }
54
+
55
+ .gradio-container {
56
+ max-width: 800px;
57
+ margin: 3rem auto;
58
+ background: rgba(44, 62, 80, 0.9);
59
+ border-radius: 20px;
60
+ padding: 2.5rem;
61
+ box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
62
+ backdrop-filter: blur(10px);
63
+ transition: transform 0.3s ease;
64
+ }
65
+
66
+ .gradio-container:hover {
67
+ transform: translateY(-5px);
68
+ }
69
+
70
+ .header {
71
+ text-align: center;
72
+ margin-bottom: 2rem;
73
+ animation: fadeIn 1s ease-in-out;
74
+ }
75
+
76
+ .header h3 {
77
+ font-size: 2.4rem;
78
+ background: linear-gradient(45deg, #e74c3c, #f1c40f);
79
+ -webkit-background-clip: text;
80
+ -webkit-text-fill-color: transparent;
81
+ margin: 0;
82
+ font-weight: 700;
83
+ }
84
+
85
+ .header p {
86
+ font-size: 1.2rem;
87
+ color: #bdc3c7;
88
+ margin-top: 0.5rem;
89
+ font-weight: 300;
90
+ }
91
+
92
+ .review-input {
93
+ background: rgba(255, 255, 255, 0.1);
94
+ border: 2px solid #4ECDC4;
95
+ border-radius: 12px;
96
+ padding: 1.5rem;
97
+ font-size: 1.1rem;
98
+ color: #ecf0f1;
99
+ width: 100%;
100
+ box-sizing: border-box;
101
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
102
+ transition: all 0.3s ease;
103
+ }
104
+
105
+ .review-input:focus {
106
+ border-color: #f1c40f;
107
+ box-shadow: 0 6px 20px rgba(241, 196, 15, 0.3);
108
+ outline: none;
109
+ transform: scale(1.01);
110
+ }
111
+
112
+ .review-input::placeholder {
113
+ color: #95a5a6;
114
+ font-style: italic;
115
+ }
116
+
117
+ .submit-btn {
118
+ background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
119
+ color: white;
120
+ border: none;
121
+ padding: 1rem 3rem;
122
+ border-radius: 50px;
123
+ font-size: 1.1rem;
124
+ font-weight: 600;
125
+ cursor: pointer;
126
+ margin: 1.5rem auto;
127
+ display: block;
128
+ transition: all 0.3s ease;
129
+ box-shadow: 0 6px 20px rgba(78, 205, 196, 0.4);
130
+ }
131
+
132
+ .submit-btn:hover {
133
+ background: linear-gradient(45deg, #4ECDC4, #FF6B6B);
134
+ transform: translateY(-3px) scale(1.05);
135
+ box-shadow: 0 10px 25px rgba(78, 205, 196, 0.5);
136
+ }
137
+
138
+ .submit-btn:active {
139
+ transform: translateY(0);
140
+ box-shadow: 0 4px 15px rgba(78, 205, 196, 0.3);
141
+ }
142
+
143
+ .output-container {
144
+ margin-top: 2rem;
145
+ padding: 1.5rem;
146
+ background: rgba(255, 255, 255, 0.05);
147
+ border-radius: 15px;
148
+ text-align: center;
149
+ min-height: 80px;
150
+ font-size: 1.3rem;
151
+ color: #f1c40f;
152
+ box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
153
+ animation: slideUp 0.5s ease-out;
154
+ }
155
+
156
+ .error-message {
157
+ background: rgba(231, 76, 60, 0.2);
158
+ color: #e74c3c;
159
+ padding: 1.2rem;
160
+ border-radius: 12px;
161
+ margin-bottom: 1.5rem;
162
+ border-left: 5px solid #e74c3c;
163
+ font-weight: 500;
164
+ animation: shake 0.4s ease-in-out;
165
+ }
166
+
167
+ @keyframes fadeIn {
168
+ from { opacity: 0; transform: translateY(-10px); }
169
+ to { opacity: 1; transform: translateY(0); }
170
+ }
171
+
172
+ @keyframes slideUp {
173
+ from { opacity: 0; transform: translateY(20px); }
174
+ to { opacity: 1; transform: translateY(0); }
175
+ }
176
+
177
+ @keyframes shake {
178
+ 0%, 100% { transform: translateX(0); }
179
+ 20%, 60% { transform: translateX(-8px); }
180
+ 40%, 80% { transform: translateX(8px); }
181
+ }
182
+
183
+ @media (max-width: 600px) {
184
+ .gradio-container {
185
+ margin: 1rem;
186
+ padding: 1.5rem;
187
+ }
188
+
189
+ .header h1 {
190
+ font-size: 2rem;
191
+ }
192
+
193
+ .review-input {
194
+ padding: 1rem;
195
+ }
196
+
197
+ .submit-btn {
198
+ padding: 0.8rem 2rem;
199
+ }
200
+ }
201
+ """
202
+
203
+ # Create the Gradio interface
204
+ with gr.Blocks(
205
+ title="Sentiment Analysis on Traveler Reviews",
206
+ theme=gr.themes.Default(
207
+ primary_hue="teal",
208
+ secondary_hue="pink",
209
+ neutral_hue="gray",
210
+ radius_size="lg",
211
+ ),
212
+ css=css
213
+ ) as demo:
214
+ gr.Markdown("""
215
+ <div class="header">
216
+ <h1> Sentiment Analysis on Traveler Reviews</h1>
217
+ <p>Type your review and let our AI uncover its vibe!</p>
218
+ </div>
219
+ """)
220
+
221
+ if error:
222
+ gr.Markdown(f"""
223
+ <div class="error-message">
224
+ ⚠️ {error}
225
+ </div>
226
+ """)
227
+
228
+ review_input = gr.Textbox(
229
+ placeholder="Share your thoughts about the your experience...",
230
+ lines=5,
231
+ elem_classes="review-input",
232
+ show_label=False
233
+ )
234
+
235
+ predict_btn = gr.Button(
236
+ "Analyze Sentiment",
237
+ elem_classes="submit-btn"
238
+ )
239
+
240
+ output = gr.Textbox(
241
+ placeholder="The sentiment will appear here...",
242
+ elem_classes="output-container",
243
+ show_label=False,
244
+ interactive=False
245
+ )
246
+
247
+ predict_btn.click(
248
+ fn=predict_sentiment,
249
+ inputs=review_input,
250
+ outputs=output
251
+ )
252
+
253
+ if __name__ == "__main__":
254
  demo.launch(share=True)