File size: 22,443 Bytes
25f60e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
import time
from dataclasses import dataclass, field
from typing import Optional, Dict, Tuple, List

import gradio as gr
import numpy as np
from PIL import Image

from deepface import DeepFace


# =========================
# 參數(可微調)
# =========================
ANALYZE_EVERY_SEC = 0.8
DOWNSAMPLE_W = 720
EMA_ALPHA = 0.45
CONF_TH = 40.0
SWITCH_CONFIRM_SEC = 2.5
IOU_TH = 0.25

DETECTOR_BACKEND = "mtcnn"     # 想快一點可改 "opencv"
ALIGN_FACE = True


# =========================
# 情緒中文 + 分數映射(7 -> 5)
# =========================
EMO_ZH = {
    "angry": "生氣",
    "disgust": "厭惡",
    "fear": "害怕",
    "happy": "開心",
    "sad": "難過",
    "surprise": "驚訝",
    "neutral": "平靜",
    "unknown": "未知",
}

# 你要的:把 7 種情緒塞進 5 分心情分數
EMO_TO_SCORE = {
    "angry": 1,
    "disgust": 1,
    "fear": 1,
    "sad": 2,
    "neutral": 3,
    "surprise": 4,
    "happy": 5,
    "unknown": 3,
}

# ### [整合需求 新增] ###
# 為了讓主程式 (app.py) 可以呼叫這個轉換邏輯,將其包裝成函式
def get_emotion_score(emo: str) -> int:
    return EMO_TO_SCORE.get(emo, 3)
# ######################

SCORE_LABELS = {
    1: "1(心情差)",
    2: "2(不太好)",
    3: "3(普通)",
    4: "4(不錯)",
    5: "5(超棒)",
}
SCORE_CHOICES = [SCORE_LABELS[i] for i in [1, 2, 3, 4, 5]]


# =========================
# 狀態
# =========================
@dataclass
class AppState:
    mode: str = "攝影機辨識"

    running: bool = True
    finished: bool = False
    last_analyze_t: float = 0.0

    # tracking
    track_bbox: Optional[Tuple[int, int, int, int]] = None

    # smoothing
    ema_scores: Optional[np.ndarray] = None
    emo_keys: List[str] = field(default_factory=list)

    # stable/candidate
    stable_emo: str = "unknown"
    candidate_emo: Optional[str] = None
    candidate_since: Optional[float] = None

    # final
    final_emo: Optional[str] = None
    final_conf: Optional[float] = None

    # ### [整合需求 新增] ###
    # 用來儲存最後計算出的整數分數 (1~5),讓外部程式容易讀取
    final_score: int = 3  
    # ######################


# =========================
# UI HTML
# =========================
def _hint_html(msg: str) -> str:
    return f"""
    <div style="border-radius:14px;padding:14px;border:1px dashed #DADADA;background:#FAFAFA;color:#111;">
      {msg}
    </div>
    """


def _camera_hint_before_start() -> str:
    return _hint_html(
        "請先按「Click to access webcam / 允許」開啟攝影機;"
        "接著,請按「錄製(Record)」開始。"
    )


def _camera_hint_done() -> str:
    return _hint_html("已完成辨識,可按「重新攝影機辨識」再次進行辨識。")


def _camera_hint_stopped() -> str:
    return _hint_html("已停止辨識。可按「重新攝影機辨識」再試一次。")


def _upload_hint_before() -> str:
    return _hint_html("請上傳一張清楚的臉部照片(臉越大越準)。")


def _upload_hint_done() -> str:
    return _hint_html("已完成圖片辨識,可按「重新上傳圖片辨識」再次辨識。")


def _upload_hint_stopped() -> str:
    return _hint_html("已停止圖片辨識。可按「重新上傳圖片辨識」再試一次。")


def _result_card_html(emo_key: str, conf: Optional[float]) -> str:
    zh = EMO_ZH.get(emo_key, emo_key)
    score = EMO_TO_SCORE.get(emo_key, 3)
    conf_txt = "" if conf is None else f"{conf:.1f}%"
    score_desc = SCORE_LABELS[score].split("(")[1].rstrip(")")

    # ✅ 結果字體用黑色,避免你說的灰字看不清楚
    return f"""
    <div style="
        border-radius:16px;
        padding:20px;
        border:1px solid #E6E6E6;
        background:#FFFFFF;
        color:#111;
    ">
      <div style="font-size:14px;color:#444;margin-bottom:8px;">
        辨識結果
      </div>

      <div style="
        font-size:42px;
        font-weight:800;
        line-height:1.1;
        margin-bottom:14px;
        color:#000;
      ">
        {zh}
      </div>

      <div style="font-size:16px;color:#111;margin-bottom:6px;">
        心情分數:<b>{score}</b>({score_desc})
      </div>

      <div style="font-size:14px;color:#111;">
        信心值:{conf_txt}
      </div>
    </div>
    """


def _score_radio_value(score: int) -> str:
    score = int(score)
    score = min(5, max(1, score))
    return SCORE_LABELS[score]


def _downsample_rgb(frame: np.ndarray, target_w: int) -> np.ndarray:
    if frame is None:
        return None
    h, w = frame.shape[:2]
    if w <= target_w:
        return frame
    scale = target_w / w
    new_h = max(1, int(h * scale))
    img = Image.fromarray(frame)
    img = img.resize((target_w, new_h))
    return np.array(img)


def _iou(a: Tuple[int, int, int, int], b: Tuple[int, int, int, int]) -> float:
    ax, ay, aw, ah = a
    bx, by, bw, bh = b
    ax2, ay2 = ax + aw, ay + ah
    bx2, by2 = bx + bw, by + bh

    ix1, iy1 = max(ax, bx), max(ay, by)
    ix2, iy2 = min(ax2, bx2), min(ay2, by2)
    iw, ih = max(0, ix2 - ix1), max(0, iy2 - iy1)
    inter = iw * ih
    if inter <= 0:
        return 0.0
    union = aw * ah + bw * bh - inter
    return inter / max(1e-6, union)


def _pick_face_by_tracking(
    faces: List[Dict],
    prev_bbox: Optional[Tuple[int, int, int, int]]
) -> Tuple[Optional[Dict], Optional[Tuple[int, int, int, int]]]:
    if not faces:
        return None, None

    cand = []
    for f in faces:
        fa = f.get("facial_area", {}) or {}
        x = int(fa.get("x", 0) or 0)
        y = int(fa.get("y", 0) or 0)
        w = int(fa.get("w", 0) or 0)
        h = int(fa.get("h", 0) or 0)
        if w > 0 and h > 0 and f.get("face") is not None:
            cand.append((f, (x, y, w, h), w * h))

    if not cand:
        return None, None

    if prev_bbox is not None:
        best = None
        best_i = 0.0
        for f, bb, area in cand:
            i = _iou(prev_bbox, bb)
            if i > best_i:
                best_i = i
                best = (f, bb)
        if best is not None and best_i >= IOU_TH:
            return best[0], best[1]

    cand.sort(key=lambda x: x[2], reverse=True)
    return cand[0][0], cand[0][1]


def _reset_state_for_camera(st: AppState) -> AppState:
    st.running = True
    st.finished = False
    st.last_analyze_t = 0.0
    st.track_bbox = None
    st.ema_scores = None
    st.emo_keys = []
    st.stable_emo = "unknown"
    st.candidate_emo = None
    st.candidate_since = None
    st.final_emo = None
    st.final_conf = None
    
    # ### [整合需求 新增] ###
    st.final_score = 3
    # ######################
    
    return st


def _reset_state_for_upload(st: AppState) -> AppState:
    st.running = False
    st.finished = False
    st.last_analyze_t = 0.0
    st.track_bbox = None
    st.ema_scores = None
    st.emo_keys = []
    st.stable_emo = "unknown"
    st.candidate_emo = None
    st.candidate_since = None
    st.final_emo = None
    st.final_conf = None
    
    # ### [整合需求 新增] ###
    st.final_score = 3
    # ######################

    return st


# =========================
# Camera streaming
# =========================
def on_stream(frame_rgb: np.ndarray, st: AppState):
    # 上傳模式不走攝影機流程
    if st.mode != "攝影機辨識":
        return (
            gr.update(),  # cam
            gr.update(),  # result
            gr.update(),  # mood
            gr.update(value=_upload_hint_before()),
            st,
            gr.update(value="重新上傳圖片辨識"),
        )

    # ✅ 完成/停止狀態:不要再顯示「Click to access…/Record…」
    if st.finished or (not st.running):
        cam_visible = False

        if st.final_emo:
            result_html = _result_card_html(st.final_emo, st.final_conf)
            mood_val = _score_radio_value(EMO_TO_SCORE.get(st.final_emo, 3))
            hint_html = _camera_hint_done()
        else:
            result_html = _hint_html("尚未完成辨識。")
            mood_val = _score_radio_value(3)
            hint_html = _camera_hint_stopped()

        return (
            gr.update(visible=cam_visible),
            gr.update(value=result_html),
            gr.update(value=mood_val),
            gr.update(value=hint_html),
            st,
            gr.update(value="重新攝影機辨識"),
        )

    # ✅ 還沒拿到 frame:提示使用者按允許/Record(而不是亂寫已開啟)
    if frame_rgb is None:
        return (
            gr.update(visible=True),
            gr.update(value=_hint_html("等待影像…(尚未開始串流)")),
            gr.update(value=_score_radio_value(3)),
            gr.update(value=_camera_hint_before_start()),
            st,
            gr.update(value="重新攝影機辨識"),
        )

    now = time.time()
    if now - st.last_analyze_t < ANALYZE_EVERY_SEC:
        show = st.stable_emo or "unknown"
        return (
            gr.update(visible=True),
            gr.update(value=_result_card_html(show, st.final_conf)),
            gr.update(value=_score_radio_value(EMO_TO_SCORE.get(show, 3))),
            gr.update(value=_camera_hint_before_start()),
            st,
            gr.update(value="重新攝影機辨識"),
        )

    st.last_analyze_t = now

    small = _downsample_rgb(frame_rgb, DOWNSAMPLE_W)

    faces = DeepFace.extract_faces(
        img_path=small,
        detector_backend=DETECTOR_BACKEND,
        enforce_detection=False,
        align=ALIGN_FACE,
    )
    pick, bbox = _pick_face_by_tracking(faces, st.track_bbox)
    if pick is None or bbox is None or pick.get("face") is None:
        show = st.stable_emo or "unknown"
        return (
            gr.update(visible=True),
            gr.update(value=_result_card_html(show, st.final_conf)),
            gr.update(value=_score_radio_value(EMO_TO_SCORE.get(show, 3))),
            gr.update(value=_camera_hint_before_start()),
            st,
            gr.update(value="重新攝影機辨識"),
        )

    st.track_bbox = bbox
    face = pick["face"]
    if face.dtype != np.uint8:
        face = np.clip(face * 255.0, 0, 255).astype(np.uint8)

    result = DeepFace.analyze(
        img_path=face,
        actions=["emotion"],
        enforce_detection=False,
    )
    if isinstance(result, list):
        result = result[0]
    emo_dict = result.get("emotion", None)

    if not isinstance(emo_dict, dict) or len(emo_dict) == 0:
        show = st.stable_emo or "unknown"
        return (
            gr.update(visible=True),
            gr.update(value=_result_card_html(show, st.final_conf)),
            gr.update(value=_score_radio_value(EMO_TO_SCORE.get(show, 3))),
            gr.update(value=_camera_hint_before_start()),
            st,
            gr.update(value="重新攝影機辨識"),
        )

    top_key = max(emo_dict, key=lambda k: float(emo_dict[k]))
    top_conf = float(emo_dict[top_key])

    if top_conf < CONF_TH:
        show = st.stable_emo or "unknown"
        return (
            gr.update(visible=True),
            gr.update(value=_result_card_html(show, st.final_conf)),
            gr.update(value=_score_radio_value(EMO_TO_SCORE.get(show, 3))),
            gr.update(value=_camera_hint_before_start()),
            st,
            gr.update(value="重新攝影機辨識"),
        )

    keys = list(emo_dict.keys())
    vec = np.array([float(emo_dict[k]) for k in keys], dtype=np.float32)

    if st.ema_scores is None or st.emo_keys != keys:
        st.ema_scores = vec
        st.emo_keys = keys
    else:
        st.ema_scores = EMA_ALPHA * vec + (1.0 - EMA_ALPHA) * st.ema_scores

    stable_idx = int(np.argmax(st.ema_scores))
    stable_key = st.emo_keys[stable_idx]
    st.stable_emo = stable_key

    # 延遲確認
    if st.candidate_emo != stable_key:
        st.candidate_emo = stable_key
        st.candidate_since = now
    else:
        if st.candidate_since is None:
            st.candidate_since = now

    confirmed = (
        st.candidate_since is not None
        and (now - st.candidate_since) >= SWITCH_CONFIRM_SEC
    )

    if confirmed and st.final_emo is None:
        st.final_emo = stable_key
        st.final_conf = top_conf
        
        # ### [整合需求 新增] ###
        # 在辨識完成的瞬間,順便存入整數分數
        st.final_score = EMO_TO_SCORE.get(stable_key, 3)
        # ######################

        st.running = False
        st.finished = True

        return (
            gr.update(visible=False),
            gr.update(value=_result_card_html(stable_key, top_conf)),
            gr.update(value=_score_radio_value(EMO_TO_SCORE.get(stable_key, 3))),
            gr.update(value=_camera_hint_done()),
            st,
            gr.update(value="重新攝影機辨識"),
        )

    return (
        gr.update(visible=True),
        gr.update(value=_result_card_html(stable_key, top_conf)),
        gr.update(value=_score_radio_value(EMO_TO_SCORE.get(stable_key, 3))),
        gr.update(value=_camera_hint_before_start()),
        st,
        gr.update(value="重新攝影機辨識"),
    )


# =========================
# Upload image
# =========================
def on_upload(img: np.ndarray, st: AppState):
    if st.mode != "上傳圖片辨識":
        return (
            gr.update(), gr.update(), st,
            gr.update(value=_camera_hint_before_start()),
            gr.update(value="重新攝影機辨識")
        )

    st = _reset_state_for_upload(st)

    if img is None:
        return (
            gr.update(value=_upload_hint_before()),
            gr.update(value=_score_radio_value(3)),
            st,
            gr.update(value=_upload_hint_before()),
            gr.update(value="重新上傳圖片辨識"),
        )

    small = _downsample_rgb(img, DOWNSAMPLE_W)
    faces = DeepFace.extract_faces(
        img_path=small,
        detector_backend=DETECTOR_BACKEND,
        enforce_detection=False,
        align=ALIGN_FACE,
    )
    pick, bbox = _pick_face_by_tracking(faces, None)
    if pick is None or pick.get("face") is None:
        return (
            gr.update(value=_hint_html("沒有偵測到人臉,請換一張更清楚、臉更大的照片。")),
            gr.update(value=_score_radio_value(3)),
            st,
            gr.update(value=_upload_hint_before()),
            gr.update(value="重新上傳圖片辨識"),
        )

    face = pick["face"]
    if face.dtype != np.uint8:
        face = np.clip(face * 255.0, 0, 255).astype(np.uint8)

    result = DeepFace.analyze(
        img_path=face,
        actions=["emotion"],
        enforce_detection=False,
    )
    if isinstance(result, list):
        result = result[0]
    emo_dict = result.get("emotion", None)
    if not isinstance(emo_dict, dict) or len(emo_dict) == 0:
        return (
            gr.update(value=_hint_html("辨識失敗,請換一張照片再試一次。")),
            gr.update(value=_score_radio_value(3)),
            st,
            gr.update(value=_upload_hint_before()),
            gr.update(value="重新上傳圖片辨識"),
        )

    top_key = max(emo_dict, key=lambda k: float(emo_dict[k]))
    top_conf = float(emo_dict[top_key])

    st.final_emo = top_key
    st.final_conf = top_conf
    
    # ### [整合需求 新增] ###
    # 圖片辨識完成,存入分數
    st.final_score = EMO_TO_SCORE.get(top_key, 3)
    # ######################

    st.finished = True

    return (
        gr.update(value=_result_card_html(top_key, top_conf)),
        gr.update(value=_score_radio_value(EMO_TO_SCORE.get(top_key, 3))),
        st,
        gr.update(value=_upload_hint_done()),
        gr.update(value="重新上傳圖片辨識"),
    )


# =========================
# Mode / Buttons
# =========================
def on_mode_change(mode: str, st: AppState):
    st.mode = mode

    if mode == "攝影機辨識":
        st = _reset_state_for_camera(st)
        return (
            gr.update(visible=True),                  # cam
            gr.update(visible=False, value=None),     # upload
            gr.update(value=_hint_html("等待影像…(尚未開始串流)")),  # result
            gr.update(value=_score_radio_value(3)),   # mood
            gr.update(value=_camera_hint_before_start()),
            st,
            gr.update(value="重新攝影機辨識"),
        )
    else:
        st = _reset_state_for_upload(st)
        return (
            gr.update(visible=False),                 # cam
            gr.update(visible=True, value=None),      # upload
            gr.update(value=_upload_hint_before()),   # result
            gr.update(value=_score_radio_value(3)),   # mood
            gr.update(value=_upload_hint_before()),   # hint
            st,
            gr.update(value="重新上傳圖片辨識"),
        )


def on_restart(st: AppState):
    if st.mode == "攝影機辨識":
        st = _reset_state_for_camera(st)
        return (
            gr.update(visible=True),                               # cam
            gr.update(value=_hint_html("等待影像…(尚未開始串流)")),  # result
            gr.update(value=_score_radio_value(3)),                # mood
            gr.update(value=_camera_hint_before_start()),          # hint
            st,
            gr.update(value="重新攝影機辨識"),                      # btn label
            gr.update(),  # upload
        )
    else:
        st = _reset_state_for_upload(st)
        return (
            gr.update(),                               # cam
            gr.update(value=_upload_hint_before()),     # result
            gr.update(value=_score_radio_value(3)),     # mood
            gr.update(value=_upload_hint_before()),     # hint
            st,
            gr.update(value="重新上傳圖片辨識"),
            gr.update(value=None),                      # upload clear
        )


def on_stop(st: AppState):
    if st.mode == "攝影機辨識":
        st.running = False
        st.finished = True
        # ✅ 停止後也不要再顯示 Click/Record 提示,改成「已停止/可重新」
        if st.final_emo is None:
            return (
                gr.update(visible=False),
                gr.update(value=_hint_html("已停止辨識。")),
                gr.update(value=_score_radio_value(3)),
                gr.update(value=_camera_hint_stopped()),
                st,
                gr.update(value="重新攝影機辨識"),
                gr.update(),
            )

        return (
            gr.update(visible=False),
            gr.update(value=_result_card_html(st.final_emo, st.final_conf)),
            gr.update(value=_score_radio_value(EMO_TO_SCORE.get(st.final_emo, 3))),
            gr.update(value=_camera_hint_done()),
            st,
            gr.update(value="重新攝影機辨識"),
            gr.update(),
        )

    # 上傳模式:停止 → 回到可重新上傳
    st = _reset_state_for_upload(st)
    return (
        gr.update(),
        gr.update(value=_hint_html("已停止圖片辨識。")),
        gr.update(value=_score_radio_value(3)),
        gr.update(value=_upload_hint_stopped()),
        st,
        gr.update(value="重新上傳圖片辨識"),
        gr.update(value=None),
    )


# =========================
# Gradio UI
# =========================
# ### [整合需求] 下方的 UI Block 可以保留,
# 之後整合進 app.py 時,app.py 會只 import 上面的函式,不會執行這裡。
# 這樣隊友還是可以單獨執行這個檔案來測試。
css = """
#app_container { max-width: 960px; margin: 0 auto; }
.gradio-container { background: #f1f5f9 !important; }
"""

# 為了方便 app.py 整合,我們把 UI 宣告包成一個函式 render_ui (或是 create_demo)
# 但為了不改動隊友原本的結構,我們保留下面的寫法,但加上 if __name__ == "__main__"
# 這樣 app.py import 時不會自動跑出介面

if __name__ == "__main__":
    with gr.Blocks(title="Emotion Detector") as demo:
        st = gr.State(AppState())

        gr.Markdown("## 情緒辨識")
        gr.Markdown("請先選擇辨識方式:**攝影機** 或 **上傳圖片**。")

        mode = gr.Radio(
            choices=["攝影機辨識", "上傳圖片辨識"],
            value="攝影機辨識",
            label="辨識方式",
        )

        hint = gr.HTML(_camera_hint_before_start())

        cam = gr.Image(
            sources=["webcam"],
            streaming=True,
            type="numpy",
            label="攝影機",
            visible=True,
        )

        upload = gr.Image(
            sources=["upload"],
            type="numpy",
            label="上傳圖片",
            visible=False,
        )

        with gr.Row():
            btn_restart = gr.Button("重新攝影機辨識", variant="primary")
            btn_stop = gr.Button("停止", variant="secondary")

        # ✅ 你要的排序:結果在上、分數在下
        gr.Markdown("### 辨識結果")
        result = gr.HTML(_hint_html("等待影像…(尚未開始串流)"))

        gr.Markdown("### 心情分數(系統判定)")
        mood = gr.Radio(
            choices=SCORE_CHOICES,
            value=_score_radio_value(3),
            label="心情分數",
            interactive=False,
        )

        mode.change(
            fn=on_mode_change,
            inputs=[mode, st],
            outputs=[cam, upload, result, mood, hint, st, btn_restart],
            show_progress="minimal",
        )

        cam.stream(
            fn=on_stream,
            inputs=[cam, st],
            outputs=[cam, result, mood, hint, st, btn_restart],
            show_progress="minimal",
        )

        upload.change(
            fn=on_upload,
            inputs=[upload, st],
            outputs=[result, mood, st, hint, btn_restart],
            show_progress="minimal",
        )

        btn_restart.click(
            fn=on_restart,
            inputs=[st],
            outputs=[cam, result, mood, hint, st, btn_restart, upload],
            show_progress="minimal",
        )

        btn_stop.click(
            fn=on_stop,
            inputs=[st],
            outputs=[cam, result, mood, hint, st, btn_restart, upload],
            show_progress="minimal",
        )

    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)