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