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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
import os
import json
import time
import shutil
import uuid
import threading
import html
from pathlib import Path
from PIL import Image

LEGACY_OUTPUT_DIR = Path("outputs")
OUTPUT_DIR = Path(os.environ.get("WAN_OUTPUT_DIR", "/data/outputs" if Path("/data").is_dir() else "outputs"))
UPLOADS_DIR = OUTPUT_DIR / "uploads"
VIDEOS_DIR = OUTPUT_DIR / "videos"
JOBS_FILE = OUTPUT_DIR / "jobs.json"


def unpack_image_files(input_image):
    if not input_image:
        return []

    raw_items = []
    if isinstance(input_image, (list, tuple)):
        raw_items = list(input_image)
    else:
        raw_items = [input_image]

    extracted_paths = []
    for item in raw_items:
        if item is None:
            continue

        path = None
        if isinstance(item, str):
            path = item
        elif hasattr(item, "path") and item.path:
            path = item.path
        elif hasattr(item, "name") and item.name:
            path = item.name
        elif isinstance(item, dict):
            path = item.get("path") or item.get("name")
        elif isinstance(item, Image.Image) or isinstance(item, np.ndarray):
            extracted_paths.append(item)
            continue

        if path:
            if os.path.isdir(path):
                for root, _, files in os.walk(path):
                    for f in sorted(files):
                        if f.lower().endswith((".png", ".jpg", ".jpeg", ".webp", ".bmp")):
                            extracted_paths.append(os.path.join(root, f))
            else:
                extracted_paths.append(path)

    return extracted_paths


class QueueManager:
    def __init__(self):
        # Some UI actions (for example clear_queue) persist while already
        # holding the state lock, so this must be re-entrant.
        self.lock = threading.RLock()
        self.gpu_lock = threading.Lock()
        self.is_running = False
        self.worker_thread = None
        self.inference_fn = None
        self._ensure_dirs()
        self.jobs = self._load_jobs()
        self._recover_interrupted_jobs()

    def _ensure_dirs(self):
        # Preserve any queued/completed work created before outputs moved into
        # the mounted Space bucket. This is a copy, never a destructive move.
        if OUTPUT_DIR != LEGACY_OUTPUT_DIR and not JOBS_FILE.exists() and LEGACY_OUTPUT_DIR.exists():
            shutil.copytree(LEGACY_OUTPUT_DIR, OUTPUT_DIR, dirs_exist_ok=True)
        OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
        UPLOADS_DIR.mkdir(parents=True, exist_ok=True)
        VIDEOS_DIR.mkdir(parents=True, exist_ok=True)
        if not JOBS_FILE.exists():
            with open(JOBS_FILE, "w", encoding="utf-8") as f:
                json.dump([], f)

    def _load_jobs(self):
        with self.lock:
            if JOBS_FILE.exists():
                try:
                    with open(JOBS_FILE, "r", encoding="utf-8") as f:
                        return json.load(f)
                except Exception as e:
                    print(f"[QueueManager] Error loading jobs.json: {e}")
                    return []
            return []

    def _save_jobs(self):
        with self.lock:
            with open(JOBS_FILE, "w", encoding="utf-8") as f:
                json.dump(self.jobs, f, indent=2, ensure_ascii=False)

    def _recover_interrupted_jobs(self):
        updated = False
        for job in self.jobs:
            if job.get("status") == "running":
                job["status"] = "queued"
                updated = True
        if updated:
            self._save_jobs()

    def add_job(self, input_image, last_image, prompt, steps, negative_prompt,
                duration_seconds, sequential_renders, guidance_scale, guidance_scale_2, seed,
                randomize_seed, quality, scheduler, flow_shift, frame_multiplier,
                lora_groups):
        
        image_paths = unpack_image_files(input_image)
        if not image_paths:
            return "❌ Error: Please select at least one valid input image before adding to queue.", self.render_queue_html()

        import random

        added_ids = []
        for idx, img_src in enumerate(image_paths):
            try:
                if isinstance(img_src, str) and os.path.exists(img_src):
                    img_obj = Image.open(img_src)
                elif isinstance(img_src, Image.Image):
                    img_obj = img_src
                else:
                    img_obj = Image.fromarray(img_src)
            except Exception as e:
                print(f"[QueueManager] Error opening image index {idx}: {e}")
                continue

            timestamp_str = time.strftime("%Y%m%d_%H%M%S")
            short_id = uuid.uuid4().hex[:6]
            job_id = f"job_{timestamp_str}_{short_id}_{idx+1}"

            input_image_path = UPLOADS_DIR / f"{job_id}_input.png"
            img_obj.save(input_image_path)

            last_image_path = None
            if last_image is not None:
                last_image_path = str(UPLOADS_DIR / f"{job_id}_last.png")
                try:
                    last_src = last_image.name if hasattr(last_image, "name") else (last_image.path if hasattr(last_image, "path") else last_image)
                    if isinstance(last_src, Image.Image):
                        last_src.save(last_image_path)
                    elif isinstance(last_src, str) and os.path.exists(last_src):
                        shutil.copy(last_src, last_image_path)
                    else:
                        Image.fromarray(last_src).save(last_image_path)
                except Exception as e:
                    print(f"[QueueManager] Warning: failed to save last image: {e}")
                    last_image_path = None

            current_seed = random.randint(0, 2147483647) if randomize_seed else int(seed if seed is not None else 42)

            job = {
                "id": job_id,
                "created_at": time.strftime("%Y-%m-%d %H:%M:%S"),
                "status": "queued",
                "prompt": prompt or "",
                "input_image_path": str(input_image_path).replace("\\", "/"),
                "last_image_path": str(last_image_path).replace("\\", "/") if last_image_path else None,
                "video_path": None,
                "params": {
                    "steps": int(steps) if steps is not None else 4,
                    "negative_prompt": negative_prompt or "",
                    "duration_seconds": float(duration_seconds) if duration_seconds is not None else 10.0,
                    "sequential_renders": max(1, min(12, int(sequential_renders or 1))),
                    "guidance_scale": float(guidance_scale) if guidance_scale is not None else 1.0,
                    "guidance_scale_2": float(guidance_scale_2) if guidance_scale_2 is not None else 1.0,
                    "seed": current_seed,
                    "randomize_seed": bool(randomize_seed),
                    "quality": int(quality) if quality is not None else 6,
                    "scheduler": str(scheduler) if scheduler else "UniPCMultistep",
                    "flow_shift": float(flow_shift) if flow_shift is not None else 3.0,
                    "frame_multiplier": int(frame_multiplier) if frame_multiplier is not None else 16,
                    "lora_groups": lora_groups if isinstance(lora_groups, list) else []
                },
                "error": None,
                "completed_at": None
            }

            self.jobs.insert(0, job)
            added_ids.append(job_id)

        self._save_jobs()
        if len(added_ids) == 1:
            msg = f"βœ… 1 job ({added_ids[0]}) added to queue."
        elif len(added_ids) > 1:
            msg = f"βœ… {len(added_ids)} separate jobs added to queue using prompt."
        else:
            msg = "❌ No valid images found to queue."

        return msg, self.render_queue_html()

    def start_queue(self, inference_fn):
        self.inference_fn = inference_fn
        self.is_running = True
        if self.worker_thread is None or not self.worker_thread.is_alive():
            self.worker_thread = threading.Thread(target=self._worker_loop, daemon=True)
            self.worker_thread.start()
        return "▢️ Queue started.", self.render_queue_html()

    def stop_queue(self):
        self.is_running = False
        return "⏸️ Queue stopped.", self.render_queue_html()

    def clear_queue(self):
        with self.lock:
            # Keep currently running job if active, clear all other jobs
            self.jobs = [j for j in self.jobs if j.get("status") == "running"]
            self._save_jobs()
        return "πŸ—‘οΈ Queue cleared.", self.render_queue_html()

    def get_error_log(self):
        failed_jobs = [j for j in self.jobs if j.get("status") == "failed"]
        
        logs = []
        if os.path.exists("app.log"):
            try:
                with open("app.log", "r", encoding="utf-8", errors="ignore") as f:
                    lines = f.readlines()
                    err_lines = [l.strip() for l in lines if any(k in l.lower() for k in ["error", "exception", "traceback", "failed"])]
                    if err_lines:
                        logs.append("=== RECENT APP LOG ERRORS ===")
                        logs.extend(err_lines[-25:])
            except Exception as e:
                logs.append(f"Error reading app.log: {e}")

        if failed_jobs:
            logs.append("\n=== FAILED QUEUE JOBS ===")
            for j in failed_jobs:
                logs.append(f"[{j.get('created_at')}] Job {j.get('id')}: {j.get('error', 'Unknown error')}")

        if not logs:
            return "βœ… No errors found in logs or queue jobs! Everything is running smoothly."

        return "\n".join(logs)

    def check_queue(self):
        running_count = sum(1 for j in self.jobs if j["status"] == "running")
        queued_count = sum(1 for j in self.jobs if j["status"] == "queued")
        completed_count = sum(1 for j in self.jobs if j["status"] == "completed")
        msg = f"πŸ” Queue checked. Total: {len(self.jobs)} | Queued: {queued_count} | Running: {running_count} | Finished: {completed_count}"
        return msg, self.render_queue_html()

    def _worker_loop(self):
        print("[QueueManager] Background worker thread started.")
        while self.is_running:
            # Find next queued job (oldest created job that is still queued)
            next_job = None
            for job in reversed(self.jobs):
                if job.get("status") == "queued":
                    next_job = job
                    break

            if next_job is None:
                time.sleep(2)
                continue

            job_id = next_job["id"]
            print(f"[QueueManager] Processing job: {job_id}")

            # Enforce single job GPU execution
            with self.gpu_lock:
                next_job["status"] = "running"
                self._save_jobs()

                try:
                    input_img = Image.open(next_job["input_image_path"])
                    last_img = Image.open(next_job["last_image_path"]) if next_job.get("last_image_path") else None
                    params = next_job.get("params", {})

                    if self.inference_fn is None:
                        raise ValueError("Inference function not bound to QueueManager.")

                    res = self.inference_fn(
                        input_image=input_img,
                        last_image=last_img,
                        prompt=next_job.get("prompt", ""),
                        steps=params.get("steps", 4),
                        negative_prompt=params.get("negative_prompt", ""),
                        duration_seconds=params.get("duration_seconds", 10.0),
                        sequential_renders=params.get("sequential_renders", 1),
                        guidance_scale=params.get("guidance_scale", 1.0),
                        guidance_scale_2=params.get("guidance_scale_2", 1.0),
                        seed=params.get("seed", 42),
                        randomize_seed=params.get("randomize_seed", False),
                        quality=params.get("quality", 6),
                        scheduler=params.get("scheduler", "UniPCMultistep"),
                        flow_shift=params.get("flow_shift", 3.0),
                        frame_multiplier=params.get("frame_multiplier", 16),
                        lora_groups=params.get("lora_groups"),
                        video_component=True,
                        artifact_dir=str(VIDEOS_DIR / job_id),
                    )

                    temp_video = None
                    if isinstance(res, tuple) and len(res) >= 2:
                        temp_video = res[1] or res[0]
                    elif isinstance(res, str):
                        temp_video = res

                    dest_video = VIDEOS_DIR / f"{job_id}.mp4"
                    if temp_video and os.path.exists(temp_video):
                        shutil.copy(temp_video, dest_video)
                        next_job["status"] = "completed"
                        next_job["video_path"] = str(dest_video).replace("\\", "/")
                        next_job["completed_at"] = time.strftime("%Y-%m-%d %H:%M:%S")
                        print(f"[QueueManager] Job {job_id} completed successfully.")
                    else:
                        next_job["status"] = "failed"
                        next_job["error"] = "Output video was not generated or file not found."
                        print(f"[QueueManager] Job {job_id} failed: Video file not created.")
                except Exception as e:
                    print(f"[QueueManager] Exception in job {job_id}: {e}")
                    next_job["status"] = "failed"
                    next_job["error"] = str(e)

                self._save_jobs()
            time.sleep(1)
        print("[QueueManager] Background worker thread stopped.")

    def render_queue_html(self):
        if not self.jobs:
            return """
            <div class="queue-container">
                <div class="queue-empty">No jobs in queue yet.<br>Upload an image and click <b>Add to Queue</b> to begin.</div>
            </div>
            """

        status_class_map = {
            "queued": "badge-queued",
            "running": "badge-running",
            "completed": "badge-completed",
            "failed": "badge-failed"
        }
        status_label_map = {
            "queued": "Queued",
            "running": "Running ⏳",
            "completed": "Finished βœ…",
            "failed": "Failed ❌"
        }

        queue_status = "ACTIVE ▢️" if self.is_running else "STOPPED ⏸️"
        status_color = "#00b894" if self.is_running else "#ea868f"

        cards = []
        for job in self.jobs:
            status = job.get("status", "queued")
            badge_cls = status_class_map.get(status, "badge-queued")
            badge_lbl = status_label_map.get(status, status)
            job_id = job.get("id", "")
            created_at = job.get("created_at", "")
            prompt = job.get("prompt", "")
            video_path = job.get("video_path")
            params = job.get("params", {})
            lora_groups = params.get("lora_groups") or []
            lora_text = ", ".join(html.escape(str(name)) for name in lora_groups) if lora_groups else "None"

            # Gradio 6 serves allowed local files through /gradio_api/file=.
            # The legacy /file= route returns 404 even when the file exists.
            file_url = f"/gradio_api/file={video_path}" if video_path else ""

            # Class attribute for auto-download triggering via JS
            auto_dl_attr = ""
            if status == "completed" and file_url:
                auto_dl_attr = f'class="pending-auto-download" data-download-url="{file_url}" data-job-id="{job_id}" data-filename="{job_id}.mp4"'

            video_html = ""
            if status == "completed" and video_path and os.path.exists(video_path):
                video_html = f"""
                <div class="queue-card-video" {auto_dl_attr}>
                    <video src="{file_url}" controls width="100%" style="border-radius: 8px; border: 1px solid rgba(255,255,255,0.12); margin-top: 8px; max-height: 240px; background: #000;"></video>
                    <div style="margin-top: 8px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap;">
                        <button onclick="loadPreview('{file_url}')" class="btn-preview-link" type="button">πŸ‘οΈ Preview in Output Box</button>
                        <a href="{file_url}" download="{job_id}.mp4" class="btn-dl-link">πŸ“₯ Download Video</a>
                        <a href="{file_url}" target="_blank" class="btn-view-link">πŸ”— Direct Link</a>
                    </div>
                </div>
                """
            elif status == "failed":
                err_msg = job.get("error", "Unknown error")
                video_html = f'<div class="queue-error">⚠️ Error: {err_msg}</div>'
            elif status == "running":
                video_html = '<div class="queue-processing">βš™οΈ Generating video on GPU...</div>'
            elif status == "queued":
                video_html = '<div class="queue-pending">⏳ Waiting in queue...</div>'

            cards.append(f"""
            <div class="queue-card {status}">
                <div class="queue-card-header">
                    <span class="queue-job-id">{job_id}</span>
                    <span class="queue-badge {badge_cls}">{badge_lbl}</span>
                </div>
                <div class="queue-card-body">
                    <div class="queue-prompt"><b>Prompt:</b> {prompt}</div>
                    <div class="queue-loras"><b>LoRAs:</b> {lora_text}</div>
                    <div class="queue-meta">Created: {created_at} | Dur: {params.get('duration_seconds', 10.0)}s x {params.get('sequential_renders', 1)} segments | Steps: {params.get('steps', 4)} | FPS: {params.get('frame_multiplier', 16)}</div>
                    {video_html}
                </div>
            </div>
            """)

        cards_html = "\n".join(cards)
        return f"""
        <div class="queue-container">
            <div class="queue-header-bar">
                <span>Worker Status: <span style="color: {status_color};">{queue_status}</span></span>
                <span>Total Jobs: {len(self.jobs)}</span>
            </div>
            <div class="queue-cards-list">
                {cards_html}
            </div>
        </div>
        """