Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# ==========================================
|
| 2 |
# EMOTION DETECTION WEB APP
|
| 3 |
-
# Model: koyelog/face
|
| 4 |
-
#
|
| 5 |
# Created by: Koyeliya Ghosh
|
| 6 |
# ==========================================
|
| 7 |
|
|
@@ -18,7 +18,7 @@ print("π AI EMOTION DETECTOR - INITIALIZING")
|
|
| 18 |
print("="*70)
|
| 19 |
|
| 20 |
# ===== CONFIGURATION =====
|
| 21 |
-
MODEL_ID = "koyelog/face"
|
| 22 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
|
| 24 |
print(f"\nπ¦ Model ID: {MODEL_ID}")
|
|
@@ -29,14 +29,8 @@ print(f"πΎ PyTorch Version: {torch.__version__}")
|
|
| 29 |
print("\nβ³ Loading model from HuggingFace...")
|
| 30 |
|
| 31 |
try:
|
| 32 |
-
model = ViTForImageClassification.from_pretrained(
|
| 33 |
-
|
| 34 |
-
cache_dir="./model_cache"
|
| 35 |
-
)
|
| 36 |
-
processor = ViTImageProcessor.from_pretrained(
|
| 37 |
-
MODEL_ID,
|
| 38 |
-
cache_dir="./model_cache"
|
| 39 |
-
)
|
| 40 |
model.to(DEVICE)
|
| 41 |
model.eval()
|
| 42 |
print("β
Model loaded successfully!")
|
|
@@ -90,8 +84,7 @@ def predict_emotion(image):
|
|
| 90 |
if image.mode != 'RGB':
|
| 91 |
image = image.convert('RGB')
|
| 92 |
|
| 93 |
-
|
| 94 |
-
print(f"\nπΈ Processing image: {original_size[0]}x{original_size[1]}")
|
| 95 |
|
| 96 |
# Preprocess
|
| 97 |
inputs = processor(images=image, return_tensors="pt")
|
|
@@ -111,10 +104,6 @@ def predict_emotion(image):
|
|
| 111 |
|
| 112 |
print(f"π― Prediction: {emotion['emoji']} {emotion['name']}")
|
| 113 |
print(f"π Confidence: {confidence*100:.2f}%")
|
| 114 |
-
print(f"π Top 3 emotions:")
|
| 115 |
-
top3_indices = torch.topk(probs, 3).indices
|
| 116 |
-
for idx in top3_indices:
|
| 117 |
-
print(f" {EMOTIONS[idx.item()]['emoji']} {EMOTIONS[idx.item()]['name']}: {probs[idx]*100:.2f}%")
|
| 118 |
|
| 119 |
# Format results for Gradio Label component
|
| 120 |
results = {
|
|
@@ -136,8 +125,6 @@ def predict_emotion(image):
|
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
print(f"β ERROR during prediction: {e}")
|
| 139 |
-
import traceback
|
| 140 |
-
traceback.print_exc()
|
| 141 |
|
| 142 |
error_html = f"""
|
| 143 |
<div style='text-align: center; padding: 40px; background: #ffe6e6; border-radius: 15px;'>
|
|
@@ -170,7 +157,7 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 170 |
<span style='font-weight: 700; color: {emo['color']}; font-size: 1.1em;'>{percentage:.1f}%</span>
|
| 171 |
</div>
|
| 172 |
<div style='width: 100%; background: #e9ecef; border-radius: 10px; height: 12px; overflow: hidden; box-shadow: inset 0 2px 4px rgba(0,0,0,0.06);'>
|
| 173 |
-
<div style='width: {bar_width}%; background: linear-gradient(90deg, {emo['color']}, {emo['color']}dd); height: 100%; transition: width 0.8s
|
| 174 |
</div>
|
| 175 |
</div>
|
| 176 |
"""
|
|
@@ -192,8 +179,8 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 192 |
<div style='
|
| 193 |
font-size: 120px;
|
| 194 |
margin: 0 0 20px 0;
|
| 195 |
-
animation: bounceIn 0.8s cubic-bezier(0.68, -0.55, 0.265, 1.55);
|
| 196 |
display: inline-block;
|
|
|
|
| 197 |
'>
|
| 198 |
{emoji}
|
| 199 |
</div>
|
|
@@ -204,7 +191,6 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 204 |
margin: 20px 0 10px 0;
|
| 205 |
font-weight: 800;
|
| 206 |
text-shadow: 2px 2px 8px rgba(0,0,0,0.1);
|
| 207 |
-
letter-spacing: -1px;
|
| 208 |
'>
|
| 209 |
{name}
|
| 210 |
</h1>
|
|
@@ -232,7 +218,7 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 232 |
<span style='font-size: 2em; font-weight: 800; color: {color};'>{confidence*100:.1f}%</span>
|
| 233 |
</div>
|
| 234 |
|
| 235 |
-
<!--
|
| 236 |
<div style='
|
| 237 |
width: 100%;
|
| 238 |
max-width: 500px;
|
|
@@ -242,18 +228,16 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 242 |
overflow: hidden;
|
| 243 |
margin: 30px auto 0;
|
| 244 |
box-shadow: inset 0 4px 8px rgba(0,0,0,0.1);
|
| 245 |
-
position: relative;
|
| 246 |
'>
|
| 247 |
<div style='
|
| 248 |
width: {confidence*100}%;
|
| 249 |
height: 100%;
|
| 250 |
background: linear-gradient(90deg, {color}, {color}cc);
|
| 251 |
border-radius: 25px;
|
| 252 |
-
transition: width 1.5s
|
| 253 |
display: flex;
|
| 254 |
align-items: center;
|
| 255 |
justify-content: center;
|
| 256 |
-
box-shadow: 0 0 20px {color}80;
|
| 257 |
'>
|
| 258 |
<span style='
|
| 259 |
color: white;
|
|
@@ -280,9 +264,6 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 280 |
color: #333;
|
| 281 |
font-size: 1.8em;
|
| 282 |
font-weight: 700;
|
| 283 |
-
display: flex;
|
| 284 |
-
align-items: center;
|
| 285 |
-
gap: 10px;
|
| 286 |
'>
|
| 287 |
π Detailed Emotion Analysis
|
| 288 |
</h2>
|
|
@@ -290,7 +271,7 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 290 |
{bars_html}
|
| 291 |
</div>
|
| 292 |
|
| 293 |
-
<!-- Model Info
|
| 294 |
<div style='
|
| 295 |
margin-top: 25px;
|
| 296 |
padding: 20px;
|
|
@@ -301,7 +282,7 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 301 |
color: #666;
|
| 302 |
'>
|
| 303 |
<p style='margin: 5px 0;'>
|
| 304 |
-
<strong>Model:</strong> koyelog/face
|
| 305 |
<strong>Accuracy:</strong> 98.80% |
|
| 306 |
<strong>Parameters:</strong> 85.8M
|
| 307 |
</p>
|
|
@@ -309,21 +290,9 @@ def generate_result_html(name, emoji, color, description, confidence, probs):
|
|
| 309 |
</div>
|
| 310 |
|
| 311 |
<style>
|
| 312 |
-
@keyframes
|
| 313 |
-
0% {{
|
| 314 |
-
|
| 315 |
-
transform: scale(0.3) translateY(-50px);
|
| 316 |
-
}}
|
| 317 |
-
50% {{
|
| 318 |
-
opacity: 1;
|
| 319 |
-
transform: scale(1.05);
|
| 320 |
-
}}
|
| 321 |
-
70% {{
|
| 322 |
-
transform: scale(0.9);
|
| 323 |
-
}}
|
| 324 |
-
100% {{
|
| 325 |
-
transform: scale(1);
|
| 326 |
-
}}
|
| 327 |
}}
|
| 328 |
</style>
|
| 329 |
"""
|
|
@@ -340,22 +309,6 @@ custom_css = """
|
|
| 340 |
max-width: 1400px !important;
|
| 341 |
}
|
| 342 |
|
| 343 |
-
.main-header {
|
| 344 |
-
text-align: center;
|
| 345 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 346 |
-
color: white;
|
| 347 |
-
padding: 60px 30px;
|
| 348 |
-
border-radius: 25px;
|
| 349 |
-
margin-bottom: 40px;
|
| 350 |
-
box-shadow: 0 15px 50px rgba(102, 126, 234, 0.3);
|
| 351 |
-
}
|
| 352 |
-
|
| 353 |
-
.tab-nav button {
|
| 354 |
-
font-size: 18px !important;
|
| 355 |
-
font-weight: 600 !important;
|
| 356 |
-
padding: 18px 30px !important;
|
| 357 |
-
}
|
| 358 |
-
|
| 359 |
.gr-button-primary {
|
| 360 |
background: linear-gradient(135deg, #667eea, #764ba2) !important;
|
| 361 |
border: none !important;
|
|
@@ -363,12 +316,6 @@ custom_css = """
|
|
| 363 |
font-weight: 600 !important;
|
| 364 |
padding: 16px 40px !important;
|
| 365 |
border-radius: 12px !important;
|
| 366 |
-
transition: all 0.3s ease !important;
|
| 367 |
-
}
|
| 368 |
-
|
| 369 |
-
.gr-button-primary:hover {
|
| 370 |
-
transform: translateY(-2px) !important;
|
| 371 |
-
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4) !important;
|
| 372 |
}
|
| 373 |
|
| 374 |
footer {
|
|
@@ -380,48 +327,40 @@ footer {
|
|
| 380 |
with gr.Blocks(
|
| 381 |
theme=gr.themes.Soft(
|
| 382 |
primary_hue="purple",
|
| 383 |
-
secondary_hue="pink"
|
| 384 |
-
font=gr.themes.GoogleFont("Inter")
|
| 385 |
),
|
| 386 |
css=custom_css,
|
| 387 |
-
title="π AI Emotion Detector | koyelog"
|
| 388 |
-
analytics_enabled=False
|
| 389 |
) as demo:
|
| 390 |
|
| 391 |
# Header
|
| 392 |
gr.HTML("""
|
| 393 |
-
<div
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
π AI Emotion Detector
|
| 396 |
</h1>
|
| 397 |
-
<p style='font-size: 1.5em; margin: 20px 0 10px; opacity: 0.95;
|
| 398 |
-
Powered by Vision Transformer | 98.80%
|
| 399 |
</p>
|
| 400 |
<p style='font-size: 1.1em; opacity: 0.85;'>
|
| 401 |
-
Model: <strong>koyelog/face</strong> | 85.8M Parameters
|
| 402 |
</p>
|
| 403 |
<div style='margin-top: 20px; display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;'>
|
| 404 |
-
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;
|
| 405 |
-
|
| 406 |
-
</span>
|
| 407 |
-
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;
|
| 408 |
-
|
| 409 |
-
</span>
|
| 410 |
-
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;
|
| 411 |
-
π¨ Fear
|
| 412 |
-
</span>
|
| 413 |
-
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px; backdrop-filter: blur(10px);'>
|
| 414 |
-
π Happy
|
| 415 |
-
</span>
|
| 416 |
-
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px; backdrop-filter: blur(10px);'>
|
| 417 |
-
π’ Sad
|
| 418 |
-
</span>
|
| 419 |
-
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px; backdrop-filter: blur(10px);'>
|
| 420 |
-
π² Surprise
|
| 421 |
-
</span>
|
| 422 |
-
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px; backdrop-filter: blur(10px);'>
|
| 423 |
-
π Neutral
|
| 424 |
-
</span>
|
| 425 |
</div>
|
| 426 |
</div>
|
| 427 |
""")
|
|
@@ -429,10 +368,10 @@ with gr.Blocks(
|
|
| 429 |
with gr.Tabs():
|
| 430 |
|
| 431 |
# TAB 1: WEBCAM
|
| 432 |
-
with gr.Tab("πΉ Live Webcam
|
| 433 |
gr.Markdown("""
|
| 434 |
-
### π₯ Capture Your
|
| 435 |
-
|
| 436 |
""")
|
| 437 |
|
| 438 |
with gr.Row():
|
|
@@ -440,21 +379,18 @@ with gr.Blocks(
|
|
| 440 |
webcam_input = gr.Image(
|
| 441 |
sources=["webcam"],
|
| 442 |
type="pil",
|
| 443 |
-
label="πΈ Your Face"
|
| 444 |
-
streaming=False,
|
| 445 |
-
mirror_webcam=True
|
| 446 |
)
|
| 447 |
webcam_button = gr.Button(
|
| 448 |
"π Detect My Emotion",
|
| 449 |
variant="primary",
|
| 450 |
-
size="lg"
|
| 451 |
-
scale=1
|
| 452 |
)
|
| 453 |
|
| 454 |
with gr.Column(scale=1):
|
| 455 |
-
webcam_html = gr.HTML(label="π―
|
| 456 |
webcam_label = gr.Label(
|
| 457 |
-
label="π
|
| 458 |
num_top_classes=7
|
| 459 |
)
|
| 460 |
|
|
@@ -467,15 +403,15 @@ with gr.Blocks(
|
|
| 467 |
# TAB 2: UPLOAD
|
| 468 |
with gr.Tab("πΌοΈ Upload Image"):
|
| 469 |
gr.Markdown("""
|
| 470 |
-
### π€ Upload
|
| 471 |
-
|
| 472 |
""")
|
| 473 |
|
| 474 |
with gr.Row():
|
| 475 |
with gr.Column(scale=1):
|
| 476 |
image_input = gr.Image(
|
| 477 |
type="pil",
|
| 478 |
-
label="πΌοΈ Upload
|
| 479 |
sources=["upload", "clipboard"]
|
| 480 |
)
|
| 481 |
image_button = gr.Button(
|
|
@@ -485,9 +421,9 @@ with gr.Blocks(
|
|
| 485 |
)
|
| 486 |
|
| 487 |
with gr.Column(scale=1):
|
| 488 |
-
image_html = gr.HTML(label="π―
|
| 489 |
image_label = gr.Label(
|
| 490 |
-
label="π
|
| 491 |
num_top_classes=7
|
| 492 |
)
|
| 493 |
|
|
@@ -505,11 +441,8 @@ with gr.Blocks(
|
|
| 505 |
padding: 50px 30px;
|
| 506 |
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 507 |
border-radius: 25px;
|
| 508 |
-
box-shadow: 0 8px 32px rgba(0,0,0,0.08);
|
| 509 |
'>
|
| 510 |
-
<h2 style='color: #333; margin-bottom: 30px;
|
| 511 |
-
π Model Information
|
| 512 |
-
</h2>
|
| 513 |
|
| 514 |
<div style='
|
| 515 |
display: grid;
|
|
@@ -517,44 +450,28 @@ with gr.Blocks(
|
|
| 517 |
gap: 25px;
|
| 518 |
margin: 30px 0;
|
| 519 |
'>
|
| 520 |
-
<div style='background: white; padding: 25px; border-radius: 15px;
|
| 521 |
-
<p style='font-weight: 700; color: #667eea;
|
| 522 |
-
<p style='font-size: 1.2em; color: #333;
|
| 523 |
</div>
|
| 524 |
-
<div style='background: white; padding: 25px; border-radius: 15px;
|
| 525 |
-
<p style='font-weight: 700; color: #667eea;
|
| 526 |
-
<p style='font-size: 1.2em; color: #333;
|
| 527 |
</div>
|
| 528 |
-
<div style='background: white; padding: 25px; border-radius: 15px;
|
| 529 |
-
<p style='font-weight: 700; color: #667eea;
|
| 530 |
-
<p style='font-size: 1.2em; color: #333;
|
| 531 |
</div>
|
| 532 |
-
<div style='background: white; padding: 25px; border-radius: 15px;
|
| 533 |
-
<p style='font-weight: 700; color: #667eea;
|
| 534 |
-
<p style='font-size: 1.2em; color: #333;
|
| 535 |
</div>
|
| 536 |
</div>
|
| 537 |
|
| 538 |
-
<
|
| 539 |
-
<p style='font-weight: 700; color: #333; font-size: 1.3em; margin-bottom: 15px;'>
|
| 540 |
-
Training Details
|
| 541 |
-
</p>
|
| 542 |
-
<p style='color: #666; font-size: 1.05em; line-height: 1.6;'>
|
| 543 |
-
<strong>Dataset:</strong> 181,230 images across 7 emotion categories<br>
|
| 544 |
-
<strong>Training Epochs:</strong> 20 epochs with dual T4 GPUs<br>
|
| 545 |
-
<strong>Best Epoch:</strong> Epoch 20/20 (Val Acc: 98.80%)<br>
|
| 546 |
-
<strong>License:</strong> MIT License
|
| 547 |
-
</p>
|
| 548 |
-
</div>
|
| 549 |
-
|
| 550 |
-
<p style='color: #666; font-size: 1.05em; margin-top: 30px; line-height: 1.6;'>
|
| 551 |
-
β οΈ <strong>Best Results:</strong> Front-facing photos | Good lighting | Single face | Clear expressions
|
| 552 |
-
</p>
|
| 553 |
-
|
| 554 |
-
<p style='color: #999; font-size: 0.95em; margin-top: 30px;'>
|
| 555 |
Created by <strong style='color: #667eea;'>Koyeliya Ghosh</strong><br>
|
| 556 |
-
<a href='https://huggingface.co/koyelog/face' target='_blank' style='color: #667eea;
|
| 557 |
-
View Model
|
| 558 |
</a>
|
| 559 |
</p>
|
| 560 |
</div>
|
|
@@ -563,16 +480,9 @@ with gr.Blocks(
|
|
| 563 |
# ===== LAUNCH =====
|
| 564 |
if __name__ == "__main__":
|
| 565 |
print("\n" + "="*70)
|
| 566 |
-
print("π LAUNCHING
|
| 567 |
print("="*70)
|
| 568 |
-
print("β
Model
|
| 569 |
-
print("β
Gradio interface built")
|
| 570 |
print("β
Starting server...\n")
|
| 571 |
|
| 572 |
-
demo.launch(
|
| 573 |
-
server_name="0.0.0.0",
|
| 574 |
-
server_port=7860,
|
| 575 |
-
share=False,
|
| 576 |
-
show_error=True,
|
| 577 |
-
show_api=True
|
| 578 |
-
)
|
|
|
|
| 1 |
# ==========================================
|
| 2 |
# EMOTION DETECTION WEB APP
|
| 3 |
+
# Model: koyelog/face
|
| 4 |
+
# Compatible with Gradio 6.1.0
|
| 5 |
# Created by: Koyeliya Ghosh
|
| 6 |
# ==========================================
|
| 7 |
|
|
|
|
| 18 |
print("="*70)
|
| 19 |
|
| 20 |
# ===== CONFIGURATION =====
|
| 21 |
+
MODEL_ID = "koyelog/face"
|
| 22 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
|
| 24 |
print(f"\nπ¦ Model ID: {MODEL_ID}")
|
|
|
|
| 29 |
print("\nβ³ Loading model from HuggingFace...")
|
| 30 |
|
| 31 |
try:
|
| 32 |
+
model = ViTForImageClassification.from_pretrained(MODEL_ID)
|
| 33 |
+
processor = ViTImageProcessor.from_pretrained(MODEL_ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
model.to(DEVICE)
|
| 35 |
model.eval()
|
| 36 |
print("β
Model loaded successfully!")
|
|
|
|
| 84 |
if image.mode != 'RGB':
|
| 85 |
image = image.convert('RGB')
|
| 86 |
|
| 87 |
+
print(f"\nπΈ Processing image: {image.size[0]}x{image.size[1]}")
|
|
|
|
| 88 |
|
| 89 |
# Preprocess
|
| 90 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
| 104 |
|
| 105 |
print(f"π― Prediction: {emotion['emoji']} {emotion['name']}")
|
| 106 |
print(f"π Confidence: {confidence*100:.2f}%")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# Format results for Gradio Label component
|
| 109 |
results = {
|
|
|
|
| 125 |
|
| 126 |
except Exception as e:
|
| 127 |
print(f"β ERROR during prediction: {e}")
|
|
|
|
|
|
|
| 128 |
|
| 129 |
error_html = f"""
|
| 130 |
<div style='text-align: center; padding: 40px; background: #ffe6e6; border-radius: 15px;'>
|
|
|
|
| 157 |
<span style='font-weight: 700; color: {emo['color']}; font-size: 1.1em;'>{percentage:.1f}%</span>
|
| 158 |
</div>
|
| 159 |
<div style='width: 100%; background: #e9ecef; border-radius: 10px; height: 12px; overflow: hidden; box-shadow: inset 0 2px 4px rgba(0,0,0,0.06);'>
|
| 160 |
+
<div style='width: {bar_width}%; background: linear-gradient(90deg, {emo['color']}, {emo['color']}dd); height: 100%; transition: width 0.8s ease; border-radius: 10px;'></div>
|
| 161 |
</div>
|
| 162 |
</div>
|
| 163 |
"""
|
|
|
|
| 179 |
<div style='
|
| 180 |
font-size: 120px;
|
| 181 |
margin: 0 0 20px 0;
|
|
|
|
| 182 |
display: inline-block;
|
| 183 |
+
animation: bounce 1s ease infinite;
|
| 184 |
'>
|
| 185 |
{emoji}
|
| 186 |
</div>
|
|
|
|
| 191 |
margin: 20px 0 10px 0;
|
| 192 |
font-weight: 800;
|
| 193 |
text-shadow: 2px 2px 8px rgba(0,0,0,0.1);
|
|
|
|
| 194 |
'>
|
| 195 |
{name}
|
| 196 |
</h1>
|
|
|
|
| 218 |
<span style='font-size: 2em; font-weight: 800; color: {color};'>{confidence*100:.1f}%</span>
|
| 219 |
</div>
|
| 220 |
|
| 221 |
+
<!-- Confidence Bar -->
|
| 222 |
<div style='
|
| 223 |
width: 100%;
|
| 224 |
max-width: 500px;
|
|
|
|
| 228 |
overflow: hidden;
|
| 229 |
margin: 30px auto 0;
|
| 230 |
box-shadow: inset 0 4px 8px rgba(0,0,0,0.1);
|
|
|
|
| 231 |
'>
|
| 232 |
<div style='
|
| 233 |
width: {confidence*100}%;
|
| 234 |
height: 100%;
|
| 235 |
background: linear-gradient(90deg, {color}, {color}cc);
|
| 236 |
border-radius: 25px;
|
| 237 |
+
transition: width 1.5s ease;
|
| 238 |
display: flex;
|
| 239 |
align-items: center;
|
| 240 |
justify-content: center;
|
|
|
|
| 241 |
'>
|
| 242 |
<span style='
|
| 243 |
color: white;
|
|
|
|
| 264 |
color: #333;
|
| 265 |
font-size: 1.8em;
|
| 266 |
font-weight: 700;
|
|
|
|
|
|
|
|
|
|
| 267 |
'>
|
| 268 |
π Detailed Emotion Analysis
|
| 269 |
</h2>
|
|
|
|
| 271 |
{bars_html}
|
| 272 |
</div>
|
| 273 |
|
| 274 |
+
<!-- Model Info -->
|
| 275 |
<div style='
|
| 276 |
margin-top: 25px;
|
| 277 |
padding: 20px;
|
|
|
|
| 282 |
color: #666;
|
| 283 |
'>
|
| 284 |
<p style='margin: 5px 0;'>
|
| 285 |
+
<strong>Model:</strong> koyelog/face |
|
| 286 |
<strong>Accuracy:</strong> 98.80% |
|
| 287 |
<strong>Parameters:</strong> 85.8M
|
| 288 |
</p>
|
|
|
|
| 290 |
</div>
|
| 291 |
|
| 292 |
<style>
|
| 293 |
+
@keyframes bounce {{
|
| 294 |
+
0%, 100% {{ transform: translateY(0); }}
|
| 295 |
+
50% {{ transform: translateY(-20px); }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
}}
|
| 297 |
</style>
|
| 298 |
"""
|
|
|
|
| 309 |
max-width: 1400px !important;
|
| 310 |
}
|
| 311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
.gr-button-primary {
|
| 313 |
background: linear-gradient(135deg, #667eea, #764ba2) !important;
|
| 314 |
border: none !important;
|
|
|
|
| 316 |
font-weight: 600 !important;
|
| 317 |
padding: 16px 40px !important;
|
| 318 |
border-radius: 12px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
}
|
| 320 |
|
| 321 |
footer {
|
|
|
|
| 327 |
with gr.Blocks(
|
| 328 |
theme=gr.themes.Soft(
|
| 329 |
primary_hue="purple",
|
| 330 |
+
secondary_hue="pink"
|
|
|
|
| 331 |
),
|
| 332 |
css=custom_css,
|
| 333 |
+
title="π AI Emotion Detector | koyelog"
|
|
|
|
| 334 |
) as demo:
|
| 335 |
|
| 336 |
# Header
|
| 337 |
gr.HTML("""
|
| 338 |
+
<div style='
|
| 339 |
+
text-align: center;
|
| 340 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 341 |
+
color: white;
|
| 342 |
+
padding: 60px 30px;
|
| 343 |
+
border-radius: 25px;
|
| 344 |
+
margin-bottom: 40px;
|
| 345 |
+
box-shadow: 0 15px 50px rgba(102, 126, 234, 0.3);
|
| 346 |
+
'>
|
| 347 |
+
<h1 style='font-size: 4em; margin: 0; font-weight: 900;'>
|
| 348 |
π AI Emotion Detector
|
| 349 |
</h1>
|
| 350 |
+
<p style='font-size: 1.5em; margin: 20px 0 10px; opacity: 0.95;'>
|
| 351 |
+
Powered by Vision Transformer | 98.80% Accuracy
|
| 352 |
</p>
|
| 353 |
<p style='font-size: 1.1em; opacity: 0.85;'>
|
| 354 |
+
Model: <strong>koyelog/face</strong> | 85.8M Parameters
|
| 355 |
</p>
|
| 356 |
<div style='margin-top: 20px; display: flex; gap: 15px; justify-content: center; flex-wrap: wrap;'>
|
| 357 |
+
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;'>π Angry</span>
|
| 358 |
+
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;'>π€’ Disgust</span>
|
| 359 |
+
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;'>π¨ Fear</span>
|
| 360 |
+
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;'>π Happy</span>
|
| 361 |
+
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;'>π’ Sad</span>
|
| 362 |
+
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;'>π² Surprise</span>
|
| 363 |
+
<span style='background: rgba(255,255,255,0.25); padding: 10px 25px; border-radius: 25px;'>π Neutral</span>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
</div>
|
| 365 |
</div>
|
| 366 |
""")
|
|
|
|
| 368 |
with gr.Tabs():
|
| 369 |
|
| 370 |
# TAB 1: WEBCAM
|
| 371 |
+
with gr.Tab("πΉ Live Webcam"):
|
| 372 |
gr.Markdown("""
|
| 373 |
+
### π₯ Capture Your Face
|
| 374 |
+
Use your webcam to capture your face and detect your emotion in real-time!
|
| 375 |
""")
|
| 376 |
|
| 377 |
with gr.Row():
|
|
|
|
| 379 |
webcam_input = gr.Image(
|
| 380 |
sources=["webcam"],
|
| 381 |
type="pil",
|
| 382 |
+
label="πΈ Your Face"
|
|
|
|
|
|
|
| 383 |
)
|
| 384 |
webcam_button = gr.Button(
|
| 385 |
"π Detect My Emotion",
|
| 386 |
variant="primary",
|
| 387 |
+
size="lg"
|
|
|
|
| 388 |
)
|
| 389 |
|
| 390 |
with gr.Column(scale=1):
|
| 391 |
+
webcam_html = gr.HTML(label="π― Result")
|
| 392 |
webcam_label = gr.Label(
|
| 393 |
+
label="π Probabilities",
|
| 394 |
num_top_classes=7
|
| 395 |
)
|
| 396 |
|
|
|
|
| 403 |
# TAB 2: UPLOAD
|
| 404 |
with gr.Tab("πΌοΈ Upload Image"):
|
| 405 |
gr.Markdown("""
|
| 406 |
+
### π€ Upload Face Image
|
| 407 |
+
Upload or drag & drop an image to detect emotions!
|
| 408 |
""")
|
| 409 |
|
| 410 |
with gr.Row():
|
| 411 |
with gr.Column(scale=1):
|
| 412 |
image_input = gr.Image(
|
| 413 |
type="pil",
|
| 414 |
+
label="πΌοΈ Upload Image",
|
| 415 |
sources=["upload", "clipboard"]
|
| 416 |
)
|
| 417 |
image_button = gr.Button(
|
|
|
|
| 421 |
)
|
| 422 |
|
| 423 |
with gr.Column(scale=1):
|
| 424 |
+
image_html = gr.HTML(label="π― Result")
|
| 425 |
image_label = gr.Label(
|
| 426 |
+
label="π Probabilities",
|
| 427 |
num_top_classes=7
|
| 428 |
)
|
| 429 |
|
|
|
|
| 441 |
padding: 50px 30px;
|
| 442 |
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 443 |
border-radius: 25px;
|
|
|
|
| 444 |
'>
|
| 445 |
+
<h2 style='color: #333; margin-bottom: 30px;'>π Model Information</h2>
|
|
|
|
|
|
|
| 446 |
|
| 447 |
<div style='
|
| 448 |
display: grid;
|
|
|
|
| 450 |
gap: 25px;
|
| 451 |
margin: 30px 0;
|
| 452 |
'>
|
| 453 |
+
<div style='background: white; padding: 25px; border-radius: 15px;'>
|
| 454 |
+
<p style='font-weight: 700; color: #667eea; margin-bottom: 10px;'>Model ID</p>
|
| 455 |
+
<p style='font-size: 1.2em; color: #333;'>koyelog/face</p>
|
| 456 |
</div>
|
| 457 |
+
<div style='background: white; padding: 25px; border-radius: 15px;'>
|
| 458 |
+
<p style='font-weight: 700; color: #667eea; margin-bottom: 10px;'>Architecture</p>
|
| 459 |
+
<p style='font-size: 1.2em; color: #333;'>Vision Transformer</p>
|
| 460 |
</div>
|
| 461 |
+
<div style='background: white; padding: 25px; border-radius: 15px;'>
|
| 462 |
+
<p style='font-weight: 700; color: #667eea; margin-bottom: 10px;'>Parameters</p>
|
| 463 |
+
<p style='font-size: 1.2em; color: #333;'>85.8 Million</p>
|
| 464 |
</div>
|
| 465 |
+
<div style='background: white; padding: 25px; border-radius: 15px;'>
|
| 466 |
+
<p style='font-weight: 700; color: #667eea; margin-bottom: 10px;'>Accuracy</p>
|
| 467 |
+
<p style='font-size: 1.2em; color: #333;'>98.80%</p>
|
| 468 |
</div>
|
| 469 |
</div>
|
| 470 |
|
| 471 |
+
<p style='color: #666; margin-top: 30px;'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
Created by <strong style='color: #667eea;'>Koyeliya Ghosh</strong><br>
|
| 473 |
+
<a href='https://huggingface.co/koyelog/face' target='_blank' style='color: #667eea;'>
|
| 474 |
+
View Model β
|
| 475 |
</a>
|
| 476 |
</p>
|
| 477 |
</div>
|
|
|
|
| 480 |
# ===== LAUNCH =====
|
| 481 |
if __name__ == "__main__":
|
| 482 |
print("\n" + "="*70)
|
| 483 |
+
print("π LAUNCHING APP")
|
| 484 |
print("="*70)
|
| 485 |
+
print("β
Model ready")
|
|
|
|
| 486 |
print("β
Starting server...\n")
|
| 487 |
|
| 488 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|