File size: 22,707 Bytes
4a8d980
71b68a0
 
 
4a8d980
 
ce90f72
71b68a0
145ddef
71b68a0
 
54400b4
4a8d980
71b68a0
145ddef
 
71b68a0
4a8d980
 
fbf8c1d
54400b4
df41ebe
64bc6bc
54400b4
71b68a0
 
 
 
 
 
4a8d980
 
dcf1608
 
 
 
 
 
 
 
 
 
71b68a0
fbf8c1d
71b68a0
 
fbf8c1d
71b68a0
 
 
 
 
fbf8c1d
 
71b68a0
fbf8c1d
 
 
dcf1608
 
fbf8c1d
 
71b68a0
 
54400b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71b68a0
 
 
 
 
 
 
 
54400b4
71b68a0
 
 
54400b4
 
fbf8c1d
3907535
71b68a0
 
54400b4
 
 
 
 
 
 
 
 
 
 
4a8d980
54400b4
4a8d980
54400b4
71b68a0
 
 
4a8d980
54400b4
71b68a0
4a8d980
71b68a0
 
4a8d980
71b68a0
 
 
54400b4
 
71b68a0
 
 
 
 
 
 
 
 
 
 
 
 
 
fbf8c1d
 
 
 
 
 
71b68a0
 
 
 
 
 
 
 
 
 
4a8d980
 
fbf8c1d
dcf1608
 
 
 
 
71b68a0
 
fbf8c1d
df41ebe
 
 
 
 
71b68a0
 
 
 
 
54400b4
 
 
 
71b68a0
 
 
 
 
 
 
54400b4
71b68a0
54400b4
 
 
 
71b68a0
 
f2a27d3
a0019b5
dcf1608
fbf8c1d
4a8d980
fbf8c1d
71b68a0
 
4a8d980
54400b4
 
71b68a0
 
 
fbf8c1d
71b68a0
fbf8c1d
71b68a0
fbf8c1d
71b68a0
 
 
fbf8c1d
dcf1608
fbf8c1d
 
 
 
 
4a8d980
 
71b68a0
df41ebe
71b68a0
fbf8c1d
71b68a0
 
 
 
 
 
 
 
a0019b5
71b68a0
 
 
df41ebe
 
71b68a0
fbf8c1d
df41ebe
71b68a0
fbf8c1d
 
df41ebe
 
 
 
71b68a0
47dcaa0
 
a0019b5
dcf1608
fbf8c1d
71b68a0
fbf8c1d
71b68a0
 
54400b4
 
71b68a0
 
 
fbf8c1d
 
dcf1608
fbf8c1d
 
 
 
 
71b68a0
fbf8c1d
71b68a0
fbf8c1d
71b68a0
54400b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64bc6bc
 
 
 
 
 
54400b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64bc6bc
54400b4
 
 
 
 
 
 
 
 
 
 
d35a46f
54400b4
 
 
64bc6bc
 
54400b4
 
64bc6bc
54400b4
 
 
64bc6bc
 
 
 
 
54400b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145ddef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0019b5
145ddef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0019b5
145ddef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Workout Coach β€” FastAPI Inference App
Runs DistilBERT classification + Claude debrief generation
Designed for Hugging Face Spaces with Docker
"""

from fastapi import FastAPI, HTTPException, Header, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from contextlib import asynccontextmanager
from typing import Optional, Dict, List
import torch
import anthropic
import asyncio
import json
import os
import logging

from model import PostWorkoutDistilBERT, load_post_model, PreWorkoutDistilBERT, load_pre_model
from inference import (
    predict_post, decode_post_predictions, build_post_prompt, parse_debrief,
    predict_pre, decode_pre_predictions, build_pre_prompt, parse_workout_plan,
)

# ─────────────────────────────────────────────
# LOGGING
# ─────────────────────────────────────────────

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# ─────────────────────────────────────────────
# API KEY AUTHENTICATION
# ─────────────────────────────────────────────

API_KEY = os.getenv("APP_API_KEY")

async def verify_key(x_api_key: str = Header(...)):
    if x_api_key != API_KEY:
        raise HTTPException(status_code=401, detail="Invalid API key")

# ─────────────────────────────────────────────
# POST-WORKOUT LABEL MAPS
# ─────────────────────────────────────────────

POST_MOOD_MAP = {
    0: "accomplished", 1: "anxious",    2: "distracted",
    3: "energized",    4: "fatigued",   5: "frustrated",
    6: "neutral",      7: "positive",
}

POST_EXERTION_MAP  = {0: "low", 1: "moderate", 2: "high"}
POST_COMPLETION_MAP = {0: "partial", 1: "full"}

POST_SORENESS_REGION_MAP = {
    0: "none",    1: "back",     2: "biceps",
    3: "chest",   4: "legs",     5: "shoulder",  6: "triceps",
}

POST_SORENESS_SEVERITY_MAP = {
    0: "none", 1: "mild", 2: "moderate", 3: "severe",
}

# ─────────────────────────────────────────────
# PRE-WORKOUT LABEL MAPS
# ─────────────────────────────────────────────

PRE_MOOD_MAP = {
    0: "accomplished", 1: "anxious",  2: "distracted",
    3: "energized",    4: "fatigued", 5: "frustrated",
    6: "neutral",      7: "positive",
}

PRE_ENERGY_MAP     = {0: "low", 1: "moderate", 2: "high"}
PRE_MOTIVATION_MAP = {0: "low", 1: "moderate", 2: "high"}
PRE_STRESS_MAP     = {0: "low", 1: "moderate", 2: "high"}

PRE_SORENESS_REGION_MAP = {
    0: "none",    1: "back",     2: "biceps",
    3: "chest",   4: "legs",     5: "shoulder",  6: "triceps",
}

PRE_SORENESS_SEVERITY_MAP = {
    0: "none", 1: "mild", 2: "moderate", 3: "severe",
}

# ─────────────────────────────────────────────
# APP STATE β€” model loaded once at startup
# ─────────────────────────────────────────────

app_state = {}

@asynccontextmanager
async def lifespan(app: FastAPI):
    """Load both models once at startup, clean up at shutdown."""
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    logger.info(f"Using device: {device}")

    # ── Post-workout model ────────────────────────────────────
    logger.info("Loading post-workout model...")
    post_model, post_tokenizer = load_post_model(
        model_path=os.getenv("MODEL_PATH", "post_best_overall_model.pt"),
        device=device,
    )
    app_state["post_model"]     = post_model
    app_state["post_tokenizer"] = post_tokenizer

    # ── Pre-workout model ─────────────────────────────────────
    logger.info("Loading pre-workout model...")
    pre_model, pre_tokenizer = load_pre_model(
        model_path=os.getenv("PRE_MODEL_PATH", "pre_best_overall_model.pt"),
        device=device,
    )
    app_state["pre_model"]     = pre_model
    app_state["pre_tokenizer"] = pre_tokenizer

    app_state["device"] = device

    # ── Anthropic client ──────────────────────────────────────
    app_state["anthropic_client"] = anthropic.Anthropic(
        api_key=os.getenv("ANTHROPIC_API_KEY")
    )

    logger.info("All models and clients loaded successfully.")
    yield

    app_state.clear()
    logger.info("App shutdown complete.")


app = FastAPI(
    title="Workout Coach Inference API",
    description="Post-workout DistilBERT + Pre-workout DistilBERT + Claude generation",
    version="2.0.0",
    lifespan=lifespan,
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# ─────────────────────────────────────────────
# REQUEST / RESPONSE SCHEMAS
# ─────────────────────────────────────────────

# ─────────────────────────────────────────────
# POST-WORKOUT SCHEMAS
# ─────────────────────────────────────────────

class PostWorkoutRequest(BaseModel):
    # Free-text input from the user β€” fed into PostWorkoutDistilBERT
    user_text: str = Field(..., min_length=5, max_length=500,
                           example="That was really tough, chest is killing me but I feel accomplished.")

    # UI form fields β€” collected separately in the app
    duration_minutes: int   = Field(..., ge=1, le=300, example=45)
    workout_type:     str   = Field(..., example="upper_body_push")
    user_goal:        str   = Field(..., example="muscle_gain")

    # Optional β€” whether to generate the Claude debrief
    generate_debrief: bool  = Field(default=True)


class PostWorkoutBertLabels(BaseModel):
    mood:              str
    exertion:          str
    soreness_region:   str
    soreness_severity: str
    completion:        str


class PostWorkoutSessionResponse(BaseModel):
    bert_labels:     PostWorkoutBertLabels
    acknowledgement: Optional[str] = None
    highlights:      Optional[str] = None
    next_session:    Optional[str] = None
    raw_debrief:     Optional[str] = None   # full unmodified response β€” fallback


class HealthResponse(BaseModel):
    model_config = {"protected_namespaces": ()}

    status:             str
    device:             str
    post_model_loaded:  bool
    pre_model_loaded:   bool

# ─────────────────────────────────────────────
# ROUTES
# ─────────────────────────────────────────────

@app.get("/health", response_model=HealthResponse)
def health():
    """Health check β€” confirms both models are loaded and ready."""
    return {
        "status":            "ok",
        "device":            str(app_state.get("device", "unknown")),
        "post_model_loaded": "post_model" in app_state,
        "pre_model_loaded":  "pre_model"  in app_state,
    }


@app.post("/post-classify", response_model=PostWorkoutSessionResponse,
          dependencies=[Depends(verify_key)])
def post_classify_session(req: PostWorkoutRequest):
    """
    Runs PostWorkoutDistilBERT inference on user_text and optionally
    generates a Claude debrief using the classified labels
    combined with the session form data.
    """
    model     = app_state["post_model"]
    tokenizer = app_state["post_tokenizer"]
    device    = app_state["device"]
    client    = app_state["anthropic_client"]

    # ── Step 1: PostWorkoutDistilBERT inference ───────────────
    try:
        raw_preds = predict_post(req.user_text, model, tokenizer, device)
    except Exception as e:
        logger.error(f"Post-workout inference error: {e}")
        raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")

    # ── Step 2: Decode integer labels β†’ strings ───────────────
    bert_labels = decode_post_predictions(
        raw_preds,
        POST_MOOD_MAP,
        POST_EXERTION_MAP,
        POST_SORENESS_REGION_MAP,
        POST_SORENESS_SEVERITY_MAP,
        POST_COMPLETION_MAP,
    )

    # ── Step 3: Optionally generate Claude debrief ────────────
    parsed = {}
    if req.generate_debrief:
        prompt = build_post_prompt(
            bert_labels=bert_labels,
            user_text=req.user_text,
            duration_minutes=req.duration_minutes,
            workout_type=req.workout_type,
            user_goal=req.user_goal,
        )
        try:
            message = client.messages.create(
                model="claude-haiku-4-5-20251001",
                max_tokens=400,
                messages=[{"role": "user", "content": prompt}],
            )
            raw_debrief = message.content[0].text
            parsed      = parse_debrief(raw_debrief)
        except Exception as e:
            logger.error(f"Claude API error (post-workout): {e}")
            parsed = {}

    return PostWorkoutSessionResponse(
        bert_labels=PostWorkoutBertLabels(**bert_labels),
        acknowledgement=parsed.get("acknowledgement"),
        highlights=parsed.get("highlights"),
        next_session=parsed.get("next_session"),
        raw_debrief=parsed.get("raw"),
    )


@app.post("/post-classify/labels-only", response_model=PostWorkoutBertLabels,
          dependencies=[Depends(verify_key)])
def post_classify_labels_only(req: PostWorkoutRequest):
    """
    Runs only PostWorkoutDistilBERT inference. Skips Claude.
    Useful for storing labels to DB without generating a debrief yet.
    """
    model     = app_state["post_model"]
    tokenizer = app_state["post_tokenizer"]
    device    = app_state["device"]

    try:
        raw_preds   = predict_post(req.user_text, model, tokenizer, device)
        bert_labels = decode_post_predictions(
            raw_preds,
            POST_MOOD_MAP,
            POST_EXERTION_MAP,
            POST_SORENESS_REGION_MAP,
            POST_SORENESS_SEVERITY_MAP,
            POST_COMPLETION_MAP,
        )
        return PostWorkoutBertLabels(**bert_labels)
    except Exception as e:
        logger.error(f"Post-workout inference error: {e}")
        raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")


# ─────────────────────────────────────────────
# PRE-WORKOUT SCHEMAS
# ─────────────────────────────────────────────

class PreWorkoutRequest(BaseModel):
    user_text: str = Field(
        ..., min_length=5, max_length=500,
        example="Feeling a bit tired but motivated. Back is slightly sore from Tuesday.",
    )
    workout_type:     str       = Field(..., example="upper_body_push")
    duration_minutes: int       = Field(..., ge=10, le=300, example=45)
    user_goal:        str       = Field(..., example="muscle_gain")
    equipment:        List[str] = Field(default=[], example=["barbell", "dumbbells", "bench"])
    generate_plan:    bool      = Field(default=True)


class PreWorkoutBertLabels(BaseModel):
    mood:              str
    energy:            str
    motivation:        str
    stress:            str
    soreness_region:   str
    soreness_severity: str


class PreWorkoutResponse(BaseModel):
    bert_labels:   PreWorkoutBertLabels
    warm_up:       Optional[str] = None
    main_workout:  Optional[str] = None
    cool_down:     Optional[str] = None
    coaching_note: Optional[str] = None
    raw_plan:      Optional[str] = None   # full unmodified response β€” fallback


# ─────────────────────────────────────────────
# PRE-WORKOUT ROUTES
# ─────────────────────────────────────────────

@app.post("/pre-classify", response_model=PreWorkoutResponse,
          dependencies=[Depends(verify_key)])
def pre_classify_session(req: PreWorkoutRequest):
    """
    Runs PreWorkoutDistilBERT inference on user_text and optionally
    calls Claude to generate a structured workout plan using the
    classified labels combined with the form selections.
    """
    model     = app_state["pre_model"]
    tokenizer = app_state["pre_tokenizer"]
    device    = app_state["device"]
    client    = app_state["anthropic_client"]

    # ── Step 1: DistilBERT inference ─────────────────────────
    try:
        raw_preds = predict_pre(req.user_text, model, tokenizer, device)
    except Exception as e:
        logger.error(f"Pre-workout inference error: {e}")
        raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")

    # ── Step 2: Decode integer labels β†’ strings ───────────────
    bert_labels = decode_pre_predictions(
        raw_preds,
        PRE_MOOD_MAP,
        PRE_ENERGY_MAP,
        PRE_MOTIVATION_MAP,
        PRE_STRESS_MAP,
        PRE_SORENESS_REGION_MAP,
        PRE_SORENESS_SEVERITY_MAP,
    )

    # ── Step 3: Optionally generate Claude workout plan ───────
    parsed = {}
    if req.generate_plan:
        prompt = build_pre_prompt(
            bert_labels=bert_labels,
            user_text=req.user_text,
            workout_type=req.workout_type,
            duration_minutes=req.duration_minutes,
            user_goal=req.user_goal,
            equipment=req.equipment,
        )
        try:
            message = client.messages.create(
                model="claude-haiku-4-5-20251001",
                max_tokens=800,
                messages=[{"role": "user", "content": prompt}],
            )
            raw_plan = message.content[0].text
            parsed   = parse_workout_plan(raw_plan)
        except Exception as e:
            logger.error(f"Claude API error (pre-workout): {e}")
            parsed = {}

    return PreWorkoutResponse(
        bert_labels=PreWorkoutBertLabels(**bert_labels),
        warm_up=parsed.get("warm_up"),
        main_workout=parsed.get("main_workout"),
        cool_down=parsed.get("cool_down"),
        coaching_note=parsed.get("coaching_note"),
        raw_plan=parsed.get("raw"),
    )


@app.post("/pre-classify/labels-only", response_model=PreWorkoutBertLabels,
          dependencies=[Depends(verify_key)])
def pre_classify_labels_only(req: PreWorkoutRequest):
    """
    Runs only pre-workout DistilBERT inference. Skips Claude.
    Useful for storing labels to DB without generating a plan yet.
    """
    model     = app_state["pre_model"]
    tokenizer = app_state["pre_tokenizer"]
    device    = app_state["device"]

    try:
        raw_preds   = predict_pre(req.user_text, model, tokenizer, device)
        bert_labels = decode_pre_predictions(
            raw_preds,
            PRE_MOOD_MAP,
            PRE_ENERGY_MAP,
            PRE_MOTIVATION_MAP,
            PRE_STRESS_MAP,
            PRE_SORENESS_REGION_MAP,
            PRE_SORENESS_SEVERITY_MAP,
        )
        return PreWorkoutBertLabels(**bert_labels)
    except Exception as e:
        logger.error(f"Pre-workout inference error: {e}")
        raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")


# ─────────────────────────────────────────────
# STREAMING ROUTES
# ─────────────────────────────────────────────
# These endpoints use Server-Sent Events (SSE).
# Each event is a line starting with "data: "
# followed by a JSON payload, terminated by "\n\n".
#
# Event types:
#   {"type": "labels",  "bert_labels": {...}}   β€” emitted immediately after
#                                                  DistilBERT inference (~0.5s)
#   {"type": "token",   "text": "..."}          β€” one Claude output token
#   {"type": "done"}                             β€” stream finished cleanly
#   {"type": "error",   "detail": "..."}        β€” non-fatal error


def _sse(payload: dict) -> str:
    """Format a dict as a single SSE data line."""
    return f"data: {json.dumps(payload)}\n\n"


@app.post("/post-classify/stream", dependencies=[Depends(verify_key)])
async def post_classify_stream(req: PostWorkoutRequest):
    """
    Streaming version of /classify.
    Emits BERT labels immediately, then streams Claude debrief tokens.
    """
    model     = app_state["post_model"]
    tokenizer = app_state["post_tokenizer"]
    device    = app_state["device"]
    client    = app_state["anthropic_client"]

    # ── Step 1: DistilBERT inference (fast β€” runs before stream opens) ──
    try:
        raw_preds   = predict_post(req.user_text, model, tokenizer, device)
        bert_labels = decode_post_predictions(
            raw_preds,
            POST_MOOD_MAP,
            POST_EXERTION_MAP,
            POST_SORENESS_REGION_MAP,
            POST_SORENESS_SEVERITY_MAP,
            POST_COMPLETION_MAP,
        )
    except Exception as e:
        logger.error(f"Post-workout inference error: {e}")
        raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")

    # ── Step 2: Build Claude prompt ─────────────────────────────────────
    prompt = build_post_prompt(
        bert_labels=bert_labels,
        user_text=req.user_text,
        duration_minutes=req.duration_minutes,
        workout_type=req.workout_type,
        user_goal=req.user_goal,
    )

    async def event_stream():
        # Emit labels immediately β€” iOS renders chips before Claude starts
        yield _sse({"type": "labels", "bert_labels": bert_labels})

        if not req.generate_debrief:
            yield _sse({"type": "done"})
            return

        # Stream Claude tokens as they arrive
        try:
            with client.messages.stream(
                model="claude-haiku-4-5-20251001",
                max_tokens=400,
                messages=[{"role": "user", "content": prompt}],
            ) as stream:
                for text_chunk in stream.text_stream:
                    yield _sse({"type": "token", "text": text_chunk})
                    await asyncio.sleep(0)   # yield control to event loop
        except Exception as e:
            logger.error(f"Claude streaming error (post-workout): {e}")
            yield _sse({"type": "error", "detail": str(e)})
            return

        yield _sse({"type": "done"})

    return StreamingResponse(
        event_stream(),
        media_type="text/event-stream",
        headers={
            "Cache-Control":    "no-cache",
            "X-Accel-Buffering": "no",   # disables nginx buffering on HF Spaces
        },
    )


@app.post("/pre-classify/stream", dependencies=[Depends(verify_key)])
async def pre_classify_stream(req: PreWorkoutRequest):
    """
    Streaming version of /pre-classify.
    Emits BERT labels immediately, then streams Claude workout plan tokens.
    """
    model     = app_state["pre_model"]
    tokenizer = app_state["pre_tokenizer"]
    device    = app_state["device"]
    client    = app_state["anthropic_client"]

    # ── Step 1: DistilBERT inference ────────────────────────────────────
    try:
        raw_preds   = predict_pre(req.user_text, model, tokenizer, device)
        bert_labels = decode_pre_predictions(
            raw_preds,
            PRE_MOOD_MAP,
            PRE_ENERGY_MAP,
            PRE_MOTIVATION_MAP,
            PRE_STRESS_MAP,
            PRE_SORENESS_REGION_MAP,
            PRE_SORENESS_SEVERITY_MAP,
        )
    except Exception as e:
        logger.error(f"Pre-workout inference error: {e}")
        raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")

    # ── Step 2: Build Claude prompt ─────────────────────────────────────
    prompt = build_pre_prompt(
        bert_labels=bert_labels,
        user_text=req.user_text,
        workout_type=req.workout_type,
        duration_minutes=req.duration_minutes,
        user_goal=req.user_goal,
        equipment=req.equipment,
    )

    async def event_stream():
        # Emit labels immediately
        yield _sse({"type": "labels", "bert_labels": bert_labels})

        if not req.generate_plan:
            yield _sse({"type": "done"})
            return

        # Stream Claude tokens as they arrive
        try:
            with client.messages.stream(
                model="claude-haiku-4-5-20251001",
                max_tokens=800,
                messages=[{"role": "user", "content": prompt}],
            ) as stream:
                for text_chunk in stream.text_stream:
                    yield _sse({"type": "token", "text": text_chunk})
                    await asyncio.sleep(0)
        except Exception as e:
            logger.error(f"Claude streaming error (pre-workout): {e}")
            yield _sse({"type": "error", "detail": str(e)})
            return

        yield _sse({"type": "done"})

    return StreamingResponse(
        event_stream(),
        media_type="text/event-stream",
        headers={
            "Cache-Control":    "no-cache",
            "X-Accel-Buffering": "no",
        },
    )