import os import numpy as np import gradio as gr import torch from app.main import app as api from app.config import settings from app.services.hf_loader import get_mlp_model from app.utils.geometry import FEATURE_NAMES, extract_angles_from_landmarks from app.utils.rules_classifier import hybrid_classify # --------------------------------------------------------------------------- # Companion Gradio UI. HF Spaces on the Docker SDK cannot use ZeroGPU (free # dynamically-allocated GPU quota); the Gradio SDK can. Mounting Gradio onto # the existing FastAPI `app` (imported above, unchanged) keeps every /api/* # route the production Next.js frontend already depends on working exactly # as before -- this file only adds a themed demo page for anyone who opens # the raw Space URL directly, it does not replace the API. # --------------------------------------------------------------------------- THEME_CSS = """ @import url('https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,400;0,9..40,500;0,9..40,600;0,9..40,700;1,9..40,400&family=Lora:wght@600&display=swap'); :root { --color-primary: #4f67f4; --color-primary-2: #06b6d4; --color-success: #059669; --color-warning: #d97706; --color-error: #dc2626; --bg-base: #f7f6f3; --bg-surface: #ffffff; --color-text: #1a1614; --color-text-muted: #3d3530; } .gradio-container { background: var(--bg-base) !important; font-family: 'DM Sans', 'Helvetica Neue', system-ui, sans-serif !important; color: var(--color-text) !important; } #asana-header { background: linear-gradient(135deg, #4f67f4, #06b6d4); border-radius: 18px; padding: 28px 32px; color: #ffffff !important; margin-bottom: 20px; } #asana-header h1 { font-family: 'Lora', Georgia, serif !important; font-weight: 600 !important; color: #ffffff !important; margin: 0 0 6px 0 !important; } #asana-header p { color: rgba(255,255,255,0.92) !important; margin: 0 !important; } .asana-card { background: var(--bg-surface) !important; border: 1px solid rgba(0,0,0,0.08) !important; border-radius: 14px !important; box-shadow: 0 4px 16px rgba(0,0,0,0.05) !important; } #cta-row a.cta-button { display: inline-block; background: linear-gradient(135deg, #4f67f4, #06b6d4); color: #ffffff !important; padding: 10px 20px; border-radius: 9999px; text-decoration: none; font-weight: 600; font-size: 0.9rem; } """ VERCEL_URL = "https://yoga-posture-correction-system.vercel.app" def analyse_demo_image(image: np.ndarray): """ Runs MediaPipe Pose server-side on a single snapshot (webcam or upload), computes the same 15 joint-angle features the production client computes in-browser, and classifies it with the exact same MLP + rule-engine sanity-check pipeline the live /api/analyse_frame route serves -- so this demo reflects the real behavior, not a mock. """ if image is None: return "Capture or upload a photo of a yoga pose to see a live analysis.", {} try: import mediapipe as mp except Exception as e: return f"MediaPipe unavailable in this environment ({e}).", {} # mediapipe is unpinned in requirements.txt, so a rebuild can silently pull # a newer release -- and 0.10.35 REMOVED mp.solutions.pose in favour of the # Tasks API. Guarding only the import would leave that as an uncaught # AttributeError here, turning a dependency bump into a 500 on the demo # page. Degrade with an explanation instead; /api/analyse_frame is # unaffected either way, since the production client runs MediaPipe in the # browser and only ever posts the 15 angles. mp_pose = getattr(getattr(mp, "solutions", None), "pose", None) if mp_pose is None: return (f"This demo needs the legacy mediapipe solutions API, which " f"was removed in mediapipe 0.10.35 (installed: " f"{getattr(mp, '__version__', 'unknown')}). The live app is " f"unaffected -- it runs MediaPipe in the browser.", {}) with mp_pose.Pose(static_image_mode=True, model_complexity=1) as pose: results = pose.process(image) if not results.pose_landmarks: return "No person detected in the frame. Step back so your full body is visible.", {} points = np.array([[lm.x, lm.y, lm.z] for lm in results.pose_landmarks.landmark]) angles = extract_angles_from_landmarks(points) angles_dict = {FEATURE_NAMES[i]: angles[i] for i in range(15)} # This demo runs MediaPipe server-side, so pose_world_landmarks (metric- # scale 3D, separately calibrated from the default normalized landmarks) # is directly available here -- no client round-trip needed, unlike the # production /api/analyse_frame path. zero_z=False keeps its genuine depth. world_angles_dict = None if results.pose_world_landmarks: world_points = np.array([[lm.x, lm.y, lm.z] for lm in results.pose_world_landmarks.landmark]) world_angles = extract_angles_from_landmarks(world_points, zero_z=False) world_angles_dict = {FEATURE_NAMES[i]: world_angles[i] for i in range(15)} model, classes = get_mlp_model() x_tensor = torch.tensor([angles], dtype=torch.float32).to(settings.DEVICE) with torch.no_grad(): pose_logits, correctness_logit, deviations_pred = model(x_tensor) mlp_pose = classes[pose_logits.argmax(1).item()] mlp_correctness = torch.sigmoid(correctness_logit).item() devs_deg = (deviations_pred[0].cpu().numpy() * 180.0) mlp_devs = {FEATURE_NAMES[i]: float(devs_deg[i]) for i in range(15)} # Same MLP + rule-engine (+ world-landmarks 3-way vote) pattern as # /api/analyse_frame's hybrid_classify. predicted_pose, correctness, devs = hybrid_classify( mlp_pose, mlp_correctness, mlp_devs, angles_dict, world_angles_dict ) devs_dict = {name: round(min(180.0, max(0.0, float(val))), 1) for name, val in devs.items()} summary = ( f"**Detected pose:** {predicted_pose.replace('_', ' ').title()}\n\n" f"**Correctness score:** {correctness * 100:.1f}%" ) return summary, devs_dict # Deliberately not named `demo`: HF's Gradio SDK runtime auto-launches any # top-level variable called `demo` on its own, which raced with our explicit # uvicorn.run() below and crashed the Space with "address already in use". with gr.Blocks(title=settings.TITLE) as blocks_ui: gr.HTML( """
Real-time yoga posture correction — 3-head ResMLP pose classifier + ST-GCN flow model, served from this Space. This page is a lightweight demo of the same backend the full app uses.