Valestino commited on
Commit
b3c74c0
·
verified ·
1 Parent(s): 8d8d3c4

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +324 -0
app.py ADDED
@@ -0,0 +1,324 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """GradioWebsite.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/14D-sxTs35_Vc9__q6maqZ6cb8OqfhTqt
8
+ """
9
+
10
+ from google.colab import drive
11
+ drive.mount('/content/drive')
12
+
13
+ !pip install huggingface_hub
14
+
15
+ !pip install gradio
16
+ import gradio as gr
17
+ import torch
18
+ import torch.nn.functional as F
19
+ from transformers import AutoTokenizer
20
+ from safetensors.torch import load_file
21
+ import matplotlib.pyplot as plt
22
+ import numpy as np
23
+ import os
24
+
25
+ !pip install safetensors transformers torch gradio
26
+
27
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
28
+ from safetensors.torch import load_file
29
+ import torch
30
+
31
+ # Define your model path
32
+ model_path = "/content/drive/MyDrive/model_3500_data_pred_label_fold_5_dari_5(indoBERT_lite_large)/model.safetensors"
33
+
34
+ # Load the model architecture (BERT-like, IndoBERT, etc.)
35
+ model_name = "indobert-lite-large-p2" # Replace with the correct model name for your case
36
+ model = AutoModelForSequenceClassification.from_pretrained('indobenchmark/indobert-lite-large-p2', num_labels=3)
37
+
38
+ # Load the model weights using safetensors
39
+ weights = load_file(model_path)
40
+
41
+ # Load the weights into the model
42
+ model.load_state_dict(weights)
43
+
44
+ # Set the model to evaluation mode
45
+ model.eval()
46
+
47
+ # Verify the model loaded correctly
48
+ print(model)
49
+
50
+ from transformers import AutoTokenizer
51
+ from transformers import BertForSequenceClassification, BertTokenizer, AutoModelForSequenceClassification
52
+
53
+ # Load the tokenizer
54
+ tokenizer = BertTokenizer.from_pretrained('indobenchmark/indobert-lite-large-p2')
55
+
56
+ import torch
57
+ import torch.nn.functional as F
58
+ import gradio as gr
59
+
60
+ def predict_stress_with_accuracy(text_input):
61
+ if not text_input.strip():
62
+ return None, None, None, None
63
+
64
+ # Tokenize the input text
65
+ inputs = tokenizer(text_input, return_tensors="pt", padding=True, truncation=True, max_length=512)
66
+
67
+ # Get the model's output
68
+ with torch.no_grad():
69
+ output = model(**inputs)
70
+
71
+ # Apply softmax to get probabilities
72
+ probabilities = F.softmax(output.logits, dim=1)
73
+
74
+ # Get predictions for all classes
75
+ probs = probabilities[0].tolist()
76
+ confidence_scores = [round(p * 100, 1) for p in probs]
77
+
78
+ # Get main prediction
79
+ predicted_class = torch.argmax(probabilities, dim=1).item()
80
+ main_confidence = confidence_scores[predicted_class]
81
+
82
+ # Map the predicted class to stress level
83
+ stress_levels = {0: "Neutral", 1: "Mild Stress", 2: "Very Stress"}
84
+ prediction = stress_levels[predicted_class]
85
+
86
+ # Generate HTML for the main result
87
+ result_html = f"""
88
+ <div class="result-card">
89
+ <div class="prediction-text">{prediction}</div>
90
+ <div class="confidence-bar-container">
91
+ <div class="confidence-bar" style="width: {main_confidence}%"></div>
92
+ <span class="confidence-text">{main_confidence}% Confident</span>
93
+ </div>
94
+ </div>
95
+ """
96
+
97
+ # Generate HTML for detailed analysis
98
+ detailed_html = f"""
99
+ <div class="detailed-analysis">
100
+ <div class="analysis-title">Detailed Analysis</div>
101
+ <div class="analysis-bars">
102
+ <div class="analysis-bar">
103
+ <div class="bar-label">Neutral</div>
104
+ <div class="bar-container">
105
+ <div class="bar neutral" style="width: {confidence_scores[0]}%"></div>
106
+ <span class="bar-value">{confidence_scores[0]}%</span>
107
+ </div>
108
+ </div>
109
+ <div class="analysis-bar">
110
+ <div class="bar-label">Mild Stress</div>
111
+ <div class="bar-container">
112
+ <div class="bar mild" style="width: {confidence_scores[1]}%"></div>
113
+ <span class="bar-value">{confidence_scores[1]}%</span>
114
+ </div>
115
+ </div>
116
+ <div class="analysis-bar">
117
+ <div class="bar-label">Very Stress</div>
118
+ <div class="bar-container">
119
+ <div class="bar very" style="width: {confidence_scores[2]}%"></div>
120
+ <span class="bar-value">{confidence_scores[2]}%</span>
121
+ </div>
122
+ </div>
123
+ </div>
124
+ </div>
125
+ """
126
+
127
+ return result_html, detailed_html, prediction, main_confidence
128
+
129
+ # Create the interface
130
+ with gr.Blocks(css="""
131
+ #component-0 {
132
+ max-width: 900px;
133
+ margin: auto;
134
+ padding: 0 20px;
135
+ }
136
+
137
+ .container {
138
+ background: linear-gradient(135deg, #1a1c29, #2d3748);
139
+ border-radius: 20px;
140
+ padding: 2rem;
141
+ box-shadow: 0 10px 30px rgba(0,0,0,0.2);
142
+ }
143
+
144
+ .header {
145
+ text-align: center;
146
+ margin-bottom: 2rem;
147
+ }
148
+
149
+ .title {
150
+ font-size: 2.5rem;
151
+ font-weight: bold;
152
+ background: linear-gradient(45deg, #00c6ff, #0072ff);
153
+ -webkit-background-clip: text;
154
+ -webkit-text-fill-color: transparent;
155
+ margin-bottom: 0.5rem;
156
+ }
157
+
158
+ .subtitle {
159
+ color: #a0aec0;
160
+ font-size: 1.1rem;
161
+ }
162
+
163
+ /* Input styles */
164
+ .input-container {
165
+ background: rgba(255,255,255,0.05);
166
+ border-radius: 15px;
167
+ padding: 1.5rem;
168
+ margin-bottom: 2rem;
169
+ }
170
+
171
+ textarea {
172
+ background: rgba(255,255,255,0.07) !important;
173
+ border: 2px solid rgba(255,255,255,0.1) !important;
174
+ border-radius: 12px !important;
175
+ color: white !important;
176
+ font-size: 1.1rem !important;
177
+ transition: all 0.3s ease !important;
178
+ }
179
+
180
+ textarea:focus {
181
+ border-color: #00c6ff !important;
182
+ box-shadow: 0 0 20px rgba(0,198,255,0.2) !important;
183
+ }
184
+
185
+ /* Result card styles */
186
+ .result-card {
187
+ background: rgba(255,255,255,0.07);
188
+ border-radius: 15px;
189
+ padding: 1.5rem;
190
+ margin-bottom: 1.5rem;
191
+ animation: fadeIn 0.5s ease-out;
192
+ }
193
+
194
+ .prediction-text {
195
+ font-size: 1.8rem;
196
+ font-weight: bold;
197
+ color: white;
198
+ text-align: center;
199
+ margin-bottom: 1rem;
200
+ }
201
+
202
+ .confidence-bar-container {
203
+ background: rgba(255,255,255,0.1);
204
+ border-radius: 10px;
205
+ height: 20px;
206
+ position: relative;
207
+ overflow: hidden;
208
+ }
209
+
210
+ .confidence-bar {
211
+ background: linear-gradient(90deg, #00c6ff, #0072ff);
212
+ height: 100%;
213
+ border-radius: 10px;
214
+ transition: width 0.5s ease-out;
215
+ }
216
+
217
+ .confidence-text {
218
+ position: absolute;
219
+ top: 50%;
220
+ left: 50%;
221
+ transform: translate(-50%, -50%);
222
+ color: white;
223
+ font-weight: bold;
224
+ text-shadow: 0 0 10px rgba(0,0,0,0.5);
225
+ }
226
+
227
+ /* Detailed analysis styles */
228
+ .detailed-analysis {
229
+ background: rgba(255,255,255,0.07);
230
+ border-radius: 15px;
231
+ padding: 1.5rem;
232
+ animation: fadeIn 0.5s ease-out;
233
+ }
234
+
235
+ .analysis-title {
236
+ color: white;
237
+ font-size: 1.3rem;
238
+ font-weight: bold;
239
+ margin-bottom: 1rem;
240
+ text-align: center;
241
+ }
242
+
243
+ .analysis-bar {
244
+ margin-bottom: 1rem;
245
+ }
246
+
247
+ .bar-label {
248
+ color: #a0aec0;
249
+ margin-bottom: 0.5rem;
250
+ }
251
+
252
+ .bar-container {
253
+ background: rgba(255,255,255,0.1);
254
+ border-radius: 8px;
255
+ height: 15px;
256
+ position: relative;
257
+ overflow: hidden;
258
+ }
259
+
260
+ .bar {
261
+ height: 100%;
262
+ transition: width 0.5s ease-out;
263
+ }
264
+
265
+ .bar.neutral { background: linear-gradient(90deg, #00f2c3, #0098f0); }
266
+ .bar.mild { background: linear-gradient(90deg, #ffd600, #ff9100); }
267
+ .bar.very { background: linear-gradient(90deg, #ff5724, #ff2d55); }
268
+
269
+ .bar-value {
270
+ position: absolute;
271
+ right: 10px;
272
+ top: 50%;
273
+ transform: translateY(-50%);
274
+ color: white;
275
+ font-size: 0.9rem;
276
+ font-weight: bold;
277
+ }
278
+
279
+ @keyframes fadeIn {
280
+ from { opacity: 0; transform: translateY(10px); }
281
+ to { opacity: 1; transform: translateY(0); }
282
+ }
283
+
284
+ @media (max-width: 768px) {
285
+ .container {
286
+ padding: 1rem;
287
+ }
288
+
289
+ .title {
290
+ font-size: 2rem;
291
+ }
292
+
293
+ .prediction-text {
294
+ font-size: 1.5rem;
295
+ }
296
+ }
297
+ """) as iface:
298
+ gr.HTML("""
299
+ <div class="header">
300
+ <div class="title">Klasifikasi Tingkat Stress</div>
301
+ <div class="subtitle">Jelaskan keadaan emosi Anda dan biarkan AI menganalisis tingkat stres Anda</div>
302
+ </div>
303
+ """)
304
+
305
+ with gr.Column(elem_classes="container"):
306
+ text_input = gr.Textbox(
307
+ label="Describe Your Emotional State",
308
+ placeholder="Apa kabar hari ini?",
309
+ lines=4,
310
+ elem_classes="input-container"
311
+ )
312
+
313
+ result_html = gr.HTML()
314
+ detailed_html = gr.HTML()
315
+ prediction = gr.State()
316
+ confidence = gr.State()
317
+
318
+ text_input.change(
319
+ predict_stress_with_accuracy,
320
+ inputs=[text_input],
321
+ outputs=[result_html, detailed_html, prediction, confidence]
322
+ )
323
+
324
+ iface.launch(share=True)