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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f2882e
 
 
 
 
 
 
 
 
a4f8eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Flask server for real-time 3D motion generation demo (HF Space version)
"""

import argparse
import threading
import time

from flask import Flask, jsonify, render_template, request
from flask_cors import CORS
from model_manager import get_model_manager


def _coerce_value(value, reference):
    """Coerce a value to match the type of a reference value"""
    if isinstance(reference, bool):
        return value if isinstance(value, bool) else str(value).lower() in ("true", "1")
    elif isinstance(reference, int):
        return int(value)
    elif isinstance(reference, float):
        return float(value)
    return str(value)


app = Flask(__name__)
CORS(app)

# Global model manager (loaded eagerly on startup)
model_manager = None
model_name_global = None  # Will be set once at startup

# Session tracking - only one active session can generate at a time
active_session_id = None  # The session ID currently generating
session_lock = threading.Lock()

# Frame consumption monitoring - detect if client disconnected by tracking frame consumption
last_frame_consumed_time = None
consumption_timeout = (
    5.0  # If no frame consumed for 5 seconds, assume client disconnected
)
consumption_monitor_thread = None
consumption_monitor_lock = threading.Lock()


def init_model():
    """Initialize model manager"""
    global model_manager
    if model_manager is None:
        if model_name_global is None:
            raise RuntimeError(
                "model_name_global not set. Server not properly initialized."
            )
        print(f"Initializing model manager with model: {model_name_global}")
        model_manager = get_model_manager(model_name=model_name_global)
        print("Model manager ready!")
    return model_manager


def consumption_monitor():
    """Monitor frame consumption and auto-reset if client stops consuming"""
    global last_frame_consumed_time, active_session_id, model_manager

    while True:
        time.sleep(2.0)  # Check every 2 seconds

        # Read state with proper locking - no nested locks!
        should_reset = False
        current_session = None
        time_since_last_consumption = 0

        # First, check consumption time
        with consumption_monitor_lock:
            if last_frame_consumed_time is not None:
                time_since_last_consumption = time.time() - last_frame_consumed_time
                if time_since_last_consumption > consumption_timeout:
                    # Need to check if still generating before reset
                    if model_manager and model_manager.is_generating:
                        should_reset = True

        # Then, get current session (separate lock)
        if should_reset:
            with session_lock:
                current_session = active_session_id

        # Perform reset outside of locks to avoid deadlock
        if should_reset and current_session is not None:
            print(
                f"No frame consumed for {time_since_last_consumption:.1f}s - client disconnected, auto-resetting..."
            )

            if model_manager:
                model_manager.reset()
                print(
                    "Generation reset due to client disconnect (no frame consumption)"
                )

            # Clear state with proper locking - no nested locks!
            with session_lock:
                if active_session_id == current_session:
                    active_session_id = None

            with consumption_monitor_lock:
                last_frame_consumed_time = None


def start_consumption_monitor():
    """Start the consumption monitoring thread if not already running"""
    global consumption_monitor_thread

    if consumption_monitor_thread is None or not consumption_monitor_thread.is_alive():
        consumption_monitor_thread = threading.Thread(
            target=consumption_monitor, daemon=True
        )
        consumption_monitor_thread.start()
        print("Consumption monitor started")


@app.route("/")
def index():
    """Main page"""
    return render_template("index.html")


@app.route("/api/config", methods=["GET"])
def get_config():
    """Get current config"""
    try:
        if model_manager:
            status = model_manager.get_buffer_status()
            return jsonify(
                {
                    "schedule_config": status["schedule_config"],
                    "cfg_config": status["cfg_config"],
                    "history_length": status["history_length"],
                    "smoothing_alpha": float(status["smoothing_alpha"]),
                }
            )
        else:
            # Model not loaded yet - return defaults
            return jsonify(
                {
                    "schedule_config": {},
                    "cfg_config": {},
                    "history_length": 30,
                    "smoothing_alpha": 0.5,
                }
            )
    except Exception as e:
        import traceback

        traceback.print_exc()
        return jsonify({"status": "error", "message": str(e)}), 500


@app.route("/api/config", methods=["POST"])
def update_config():
    """Update model config in memory"""
    try:
        global active_session_id, last_frame_consumed_time

        if not model_manager or not model_manager.model:
            return jsonify({"status": "error", "message": "Model not loaded yet"}), 400

        data = request.json
        new_schedule_config = data.get("schedule_config")
        new_cfg_config = data.get("cfg_config")
        history_length = data.get("history_length")
        smoothing_alpha = data.get("smoothing_alpha")

        valid_schedule_keys = set(model_manager._base_schedule_config.keys())
        valid_cfg_keys = set(model_manager._base_cfg_config.keys())

        # Validate and update schedule_config
        if new_schedule_config:
            for key in new_schedule_config:
                if key not in valid_schedule_keys:
                    return jsonify(
                        {
                            "status": "error",
                            "message": f"Unknown schedule_config key: {key}",
                        }
                    ), 400
            for key, value in new_schedule_config.items():
                model_manager._base_schedule_config[key] = _coerce_value(
                    value, model_manager._base_schedule_config[key]
                )

        # Validate and update cfg_config
        if new_cfg_config:
            for key in new_cfg_config:
                if key not in valid_cfg_keys:
                    return jsonify(
                        {"status": "error", "message": f"Unknown cfg_config key: {key}"}
                    ), 400
            for key, value in new_cfg_config.items():
                model_manager._base_cfg_config[key] = _coerce_value(
                    value, model_manager._base_cfg_config[key]
                )

        # Reset with new parameters
        model_manager.reset(
            history_length=history_length,
            smoothing_alpha=smoothing_alpha,
        )

        # Clear active session
        with session_lock:
            active_session_id = None
        with consumption_monitor_lock:
            last_frame_consumed_time = None

        return jsonify({"status": "success"})
    except Exception as e:
        import traceback

        traceback.print_exc()
        return jsonify({"status": "error", "message": str(e)}), 500


@app.route("/api/start", methods=["POST"])
def start_generation():
    """Start generation with given text"""
    try:
        global active_session_id, last_frame_consumed_time

        data = request.json
        session_id = data.get("session_id")
        text = data.get("text", "walk in a circle.")
        history_length = data.get("history_length")
        smoothing_alpha = data.get(
            "smoothing_alpha", None
        )  # Optional smoothing parameter
        force = data.get("force", False)  # Allow force takeover

        if not session_id:
            return jsonify(
                {"status": "error", "message": "session_id is required"}
            ), 400

        print(
            f"[Session {session_id}] Starting generation with text: {text}, history_length: {history_length}, force: {force}"
        )

        # Initialize model if needed
        mm = init_model()

        # Check if another session is already generating
        need_force_takeover = False

        with session_lock:
            if active_session_id and active_session_id != session_id:
                if not force:
                    # Another session is active, return conflict
                    return jsonify(
                        {
                            "status": "error",
                            "message": "Another session is already generating.",
                            "conflict": True,
                            "active_session_id": active_session_id,
                        }
                    ), 409
                else:
                    # Force takeover
                    print(
                        f"[Session {session_id}] Force takeover from session {active_session_id}"
                    )
                    need_force_takeover = True

            if mm.is_generating and active_session_id == session_id:
                return jsonify(
                    {
                        "status": "error",
                        "message": "Generation is already running for this session.",
                    }
                ), 400

            # Set this session as active
            active_session_id = session_id

        # Clear previous session's consumption tracking if force takeover (no nested locks)
        if need_force_takeover:
            with consumption_monitor_lock:
                last_frame_consumed_time = None

        # Reset and start generation
        mm.reset(history_length=history_length, smoothing_alpha=smoothing_alpha)
        mm.start_generation(text, history_length=history_length)

        # Initialize consumption tracking (no nested locks)
        with consumption_monitor_lock:
            last_frame_consumed_time = time.time()

        # Start consumption monitoring
        start_consumption_monitor()
        print(f"[Session {session_id}] Consumption monitoring activated")

        return jsonify(
            {
                "status": "success",
                "message": f"Generation started with text: {text}, history_length: {history_length}",
                "session_id": session_id,
            }
        )
    except Exception as e:
        print(f"Error in start_generation: {e}")
        import traceback

        traceback.print_exc()
        return jsonify({"status": "error", "message": str(e)}), 500


@app.route("/api/update_text", methods=["POST"])
def update_text():
    """Update the generation text"""
    try:
        data = request.json
        session_id = data.get("session_id")
        text = data.get("text", "")

        if not session_id:
            return jsonify(
                {"status": "error", "message": "session_id is required"}
            ), 400

        # Verify this is the active session
        with session_lock:
            if active_session_id != session_id:
                return jsonify(
                    {"status": "error", "message": "Not the active session"}
                ), 403

        if model_manager is None:
            return jsonify({"status": "error", "message": "Model not initialized"}), 400

        model_manager.update_text(text)

        return jsonify({"status": "success", "message": f"Text updated to: {text}"})
    except Exception as e:
        return jsonify({"status": "error", "message": str(e)}), 500


@app.route("/api/pause", methods=["POST"])
def pause_generation():
    """Pause generation (keeps state for resume)"""
    try:
        data = request.json if request.json else {}
        session_id = data.get("session_id")

        if not session_id:
            return jsonify(
                {"status": "error", "message": "session_id is required"}
            ), 400

        # Verify this is the active session
        with session_lock:
            if active_session_id != session_id:
                return jsonify(
                    {"status": "error", "message": "Not the active session"}
                ), 403

        if model_manager:
            model_manager.pause_generation()

        return jsonify({"status": "success", "message": "Generation paused"})
    except Exception as e:
        return jsonify({"status": "error", "message": str(e)}), 500


@app.route("/api/resume", methods=["POST"])
def resume_generation():
    """Resume generation from paused state"""
    try:
        global last_frame_consumed_time

        data = request.json if request.json else {}
        session_id = data.get("session_id")

        if not session_id:
            return jsonify(
                {"status": "error", "message": "session_id is required"}
            ), 400

        # Verify this is the active session
        with session_lock:
            if active_session_id != session_id:
                return jsonify(
                    {"status": "error", "message": "Not the active session"}
                ), 403

        if model_manager is None:
            return jsonify({"status": "error", "message": "Model not initialized"}), 400

        model_manager.resume_generation()

        # Reset consumption tracking when resuming
        with consumption_monitor_lock:
            last_frame_consumed_time = time.time()

        return jsonify({"status": "success", "message": "Generation resumed"})
    except Exception as e:
        return jsonify({"status": "error", "message": str(e)}), 500


@app.route("/api/reset", methods=["POST"])
def reset():
    """Reset generation state"""
    try:
        global active_session_id, last_frame_consumed_time

        data = request.json if request.json else {}
        session_id = data.get("session_id")
        history_length = data.get("history_length")
        smoothing_alpha = data.get("smoothing_alpha")

        # If session_id provided, verify it's the active session
        if session_id:
            with session_lock:
                if active_session_id and active_session_id != session_id:
                    return jsonify(
                        {"status": "error", "message": "Not the active session"}
                    ), 403

        if model_manager:
            model_manager.reset(
                history_length=history_length, smoothing_alpha=smoothing_alpha
            )

        # Clear the active session
        with session_lock:
            if active_session_id == session_id or not session_id:
                active_session_id = None

        # Clear consumption tracking
        with consumption_monitor_lock:
            last_frame_consumed_time = None

        print(f"[Session {session_id}] Reset complete, session cleared")

        return jsonify(
            {
                "status": "success",
                "message": "Reset complete",
            }
        )
    except Exception as e:
        return jsonify({"status": "error", "message": str(e)}), 500


@app.route("/api/get_frame", methods=["GET"])
def get_frame():
    """Get the next frame"""
    try:
        global last_frame_consumed_time

        session_id = request.args.get("session_id")

        if not session_id:
            return jsonify(
                {"status": "error", "message": "session_id is required"}
            ), 400

        if model_manager is None:
            return jsonify({"status": "error", "message": "Model not initialized"}), 400

        count = min(int(request.args.get("count", 8)), 20)

        # Check if this is the active session or a spectator
        with session_lock:
            is_active = active_session_id == session_id

        if is_active:
            # Active session: pop frames from generation buffer
            frames = []
            for _ in range(count):
                joints = model_manager.get_next_frame()
                if joints is None:
                    break
                frames.append(joints.tolist())

            if frames:
                with consumption_monitor_lock:
                    last_frame_consumed_time = time.time()

                return jsonify(
                    {
                        "status": "success",
                        "frames": frames,
                        "buffer_size": model_manager.frame_buffer.size(),
                    }
                )
        else:
            # Spectator: read from broadcast buffer (non-destructive)
            after_id = int(request.args.get("after_id", 0))
            broadcast = model_manager.get_broadcast_frames(after_id, count)
            if broadcast:
                last_id = broadcast[-1][0]
                frames = [joints.tolist() for _, joints in broadcast]
                return jsonify(
                    {
                        "status": "success",
                        "frames": frames,
                        "last_id": last_id,
                        "buffer_size": model_manager.frame_buffer.size(),
                    }
                )

        # No frames available (active or spectator)
        return jsonify(
            {
                "status": "waiting",
                "message": "No frame available yet",
                "buffer_size": model_manager.frame_buffer.size(),
            }
        )
    except Exception as e:
        print(f"Error in get_frame: {e}")
        import traceback

        traceback.print_exc()
        return jsonify({"status": "error", "message": str(e)}), 500


@app.route("/api/status", methods=["GET"])
def get_status():
    """Get generation status"""
    try:
        session_id = request.args.get("session_id")

        with session_lock:
            is_active_session = session_id and active_session_id == session_id
            current_active_session = active_session_id

        if model_manager is None:
            return jsonify(
                {
                    "initialized": False,
                    "buffer_size": 0,
                    "is_generating": False,
                    "is_active_session": is_active_session,
                    "active_session_id": current_active_session,
                }
            )

        status = model_manager.get_buffer_status()
        status["initialized"] = True
        status["is_active_session"] = is_active_session
        status["active_session_id"] = current_active_session

        return jsonify(status)
    except Exception as e:
        return jsonify({"status": "error", "message": str(e)}), 500


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Flask server for real-time 3D motion generation"
    )
    parser.add_argument(
        "--model_name",
        type=str,
        default="ShandaAI/FloodDiffusionTiny",
        help="HF Hub model name (default: ShandaAI/FloodDiffusionTiny)",
    )
    parser.add_argument(
        "--port",
        type=int,
        default=7860,
        help="Port to run the server on (default: 7860)",
    )
    args = parser.parse_args()

    model_name_global = args.model_name

    # Load model eagerly on startup (pre-downloaded in Docker)
    print(f"Loading model: {model_name_global}")
    init_model()

    print("Starting Flask server...")
    app.run(host="0.0.0.0", port=args.port, debug=False, threaded=True)