sayeed105236 commited on
Commit
40328c3
·
1 Parent(s): eb6e5e7

Switch to official Wan2.2 inference code for native T2V+I2V support

Browse files
Files changed (3) hide show
  1. Dockerfile +14 -3
  2. app.py +116 -162
  3. requirements.txt +0 -11
Dockerfile CHANGED
@@ -16,16 +16,27 @@ RUN useradd -m -u 1000 user
16
  USER user
17
  ENV PATH="/home/user/.local/bin:$PATH"
18
  ENV HF_HOME=/home/user/.cache/huggingface
 
19
 
20
  WORKDIR /app
21
 
 
 
 
22
  # Install PyTorch with CUDA 12.4 support
23
  RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
24
 
25
- # Copy and install Python deps
 
 
 
26
  COPY --chown=user ./requirements.txt requirements.txt
27
- RUN pip install --no-cache-dir -r requirements.txt && \
28
- pip install --no-cache-dir git+https://github.com/huggingface/diffusers
 
 
 
 
29
 
30
  # Copy app code
31
  COPY --chown=user ./app.py app.py
 
16
  USER user
17
  ENV PATH="/home/user/.local/bin:$PATH"
18
  ENV HF_HOME=/home/user/.cache/huggingface
19
+ ENV PYTHONUNBUFFERED=1
20
 
21
  WORKDIR /app
22
 
23
+ # Clone official Wan2.2 inference code (needed for generate.py)
24
+ RUN git clone https://github.com/Wan-Video/Wan2.2.git /app/Wan2.2
25
+
26
  # Install PyTorch with CUDA 12.4 support
27
  RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
28
 
29
+ # Install Wan2.2 deps
30
+ RUN cd /app/Wan2.2 && pip install --no-cache-dir -r requirements.txt
31
+
32
+ # Install FastAPI deps
33
  COPY --chown=user ./requirements.txt requirements.txt
34
+ RUN pip install --no-cache-dir -r requirements.txt
35
+
36
+ # Download the model (non-diffusers version, stored in /data for persistence)
37
+ # This uses the original Wan2.2 checkpoint which supports both T2V and I2V natively
38
+ RUN mkdir -p /data/models && \
39
+ python -c "from huggingface_hub import snapshot_download; snapshot_download('Wan-AI/Wan2.2-TI2V-5B', local_dir='/data/models/Wan2.2-TI2V-5B', ignore_patterns=['*.md','assets/*'])"
40
 
41
  # Copy app code
42
  COPY --chown=user ./app.py app.py
app.py CHANGED
@@ -1,42 +1,33 @@
1
  """
2
  Wan2.2 TI2V 5B — Text-to-Video & Image-to-Video
3
- Production FastAPI app for Hugging Face Docker Space.
 
4
  """
5
 
6
  import os
7
  import io
8
- import gc
9
  import json
10
  import uuid
11
  import time
12
  import asyncio
 
13
  from pathlib import Path
14
  from contextlib import asynccontextmanager
15
  from datetime import datetime
16
  from typing import Optional
17
 
18
- import torch
19
- import numpy as np
20
- from fastapi import FastAPI, Form, UploadFile, File, HTTPException, Query
21
- from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
22
- from fastapi.staticfiles import StaticFiles
23
- from diffusers import WanPipeline, AutoencoderKLWan
24
- from diffusers.utils import export_to_video
25
 
26
  # ---------- Config ----------
27
- MODEL_ID = os.getenv("MODEL_ID", "Wan-AI/Wan2.2-TI2V-5B-Diffusers")
28
- HF_CACHE = Path(os.getenv("HF_HOME", os.path.expanduser("~/.cache/huggingface")))
29
  OUTPUT_DIR = Path("/tmp/outputs")
30
  OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
31
  JOBS_FILE = OUTPUT_DIR / "jobs.json"
32
 
33
- DEVICE = None
34
- DTYPE = torch.bfloat16
35
- pipe = None
36
-
37
- # Job store (in-memory + persisted)
38
  _jobs: dict = {}
39
- _job_lock = asyncio.Lock()
40
 
41
 
42
  def _load_jobs():
@@ -52,94 +43,84 @@ def _save_jobs():
52
  JOBS_FILE.write_text(json.dumps(_jobs, default=str))
53
 
54
 
55
- def free_memory():
56
- gc.collect()
57
- if torch.cuda.is_available():
58
- torch.cuda.empty_cache()
59
- torch.cuda.synchronize()
60
-
61
-
62
  @asynccontextmanager
63
  async def lifespan(app: FastAPI):
64
- global DEVICE, pipe
65
  _load_jobs()
66
- DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
67
- if not torch.cuda.is_available():
68
- raise RuntimeError("CUDA GPU required — add a GPU to this Space (Settings → Hardware).")
69
-
70
- vram_gb = torch.cuda.get_device_properties(0).total_memory / 1e9
71
- print(f"[startup] GPU: {torch.cuda.get_device_name(0)} ({vram_gb:.1f} GB VRAM)")
72
- print(f"[startup] Loading {MODEL_ID} …")
73
-
74
- vae = AutoencoderKLWan.from_pretrained(MODEL_ID, subfolder="vae", torch_dtype=torch.float32, cache_dir=str(HF_CACHE))
75
- pipe = WanPipeline.from_pretrained(MODEL_ID, vae=vae, torch_dtype=DTYPE, cache_dir=str(HF_CACHE))
76
-
77
- # CPU offload — required for T4 (16 GB VRAM). Keeps each module on GPU only when active.
78
- pipe.enable_model_cpu_offload()
79
- pipe.enable_attention_slicing()
80
- # No pipe.to(DEVICE) — enable_model_cpu_offload handles device management
81
-
82
- print("[startup] Ready. (CPU offload + attention slicing enabled)")
83
  yield
84
- free_memory()
85
 
86
 
87
  app = FastAPI(lifespan=lifespan, title="Wan2.2 TI2V 5B")
88
 
89
- # ----- Serving -----
90
 
91
- async def _run_generation(job_id: str, prompt: str, input_image, steps: int, guidance_scale: float,
92
- num_frames: int, seed: Optional[int]):
 
 
 
 
 
93
  _jobs[job_id]["status"] = "running"
94
  _jobs[job_id]["started_at"] = time.time()
95
  _save_jobs()
96
 
97
- out_name = f"{job_id}.mp4"
98
- out_path = OUTPUT_DIR / out_name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  try:
101
- height, width = 704, 1280
102
-
103
- if input_image is not None:
104
- aspect = input_image.height / input_image.width
105
- max_area = height * width
106
- mod = pipe.vae_scale_factor_spatial * pipe.transformer.config.patch_size[1]
107
- h = round(np.sqrt(max_area * aspect)) // mod * mod
108
- w = round(np.sqrt(max_area / aspect)) // mod * mod
109
- input_image = input_image.resize((w, h))
110
- height, width = h, w
111
-
112
- gen_kwargs = dict(
113
- prompt=prompt,
114
- height=height,
115
- width=width,
116
- num_frames=num_frames,
117
- num_inference_steps=steps,
118
- guidance_scale=guidance_scale,
119
  )
120
- if input_image is not None:
121
- gen_kwargs["image"] = input_image
122
- if seed is not None:
123
- gen_kwargs["generator"] = torch.Generator(device=DEVICE).manual_seed(seed)
124
 
125
- frames = pipe(**gen_kwargs).frames[0]
126
- export_to_video(frames, str(out_path), fps=24)
 
 
 
 
 
 
 
 
 
 
127
 
128
  duration = round(time.time() - _jobs[job_id]["started_at"], 1)
129
  _jobs[job_id]["status"] = "done"
130
  _jobs[job_id]["duration"] = duration
131
- _jobs[job_id]["output"] = f"/output/{out_name}"
132
- _jobs[job_id]["width"] = width
133
- _jobs[job_id]["height"] = height
134
 
135
- except torch.cuda.OutOfMemoryError:
136
  _jobs[job_id]["status"] = "error"
137
- _jobs[job_id]["error"] = "GPU out of memory. Try fewer frames or upgrade GPU tier."
138
  except Exception as e:
139
  _jobs[job_id]["status"] = "error"
140
  _jobs[job_id]["error"] = str(e)
141
  finally:
142
- free_memory()
143
  _save_jobs()
144
 
145
 
@@ -157,7 +138,7 @@ def index():
157
  --bg: #0a0a14; --surface: #13132a; --surface2: #1c1c3a;
158
  --border: #2a2a4a; --text: #e2e2f0; --text2: #8888aa;
159
  --accent: #7c4dff; --accent2: #b388ff; --danger: #ff5252;
160
- --radius: 14px; --shadow: 0 4px 40px rgba(124,77,255,0.08);
161
  }
162
  *{box-sizing:border-box;margin:0;padding:0}
163
  body{font-family:-apple-system,'Segoe UI',Roboto,sans-serif;background:var(--bg);color:var(--text);min-height:100vh}
@@ -167,8 +148,8 @@ header h1{font-size:2rem;background:linear-gradient(135deg,var(--accent),var(--a
167
  header .tagline{color:var(--text2);margin-top:.4rem;font-size:.95rem}
168
  .layout{display:grid;grid-template-columns:1fr 1fr;gap:1.5rem;align-items:start}
169
  @media(max-width:800px){.layout{grid-template-columns:1fr}}
170
- .card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:1.5rem;box-shadow:var(--shadow)}
171
- .card h2{font-size:1.15rem;margin-bottom:1rem;display:flex;align-items:center;gap:.5rem}
172
  .field{margin-bottom:1rem}
173
  .field label{display:block;font-weight:600;margin-bottom:.35rem;font-size:.88rem;color:var(--text2)}
174
  .field textarea,.field input,.field select{width:100%;padding:.7rem .85rem;border-radius:10px;border:1px solid var(--border);background:var(--surface2);color:var(--text);font-size:.93rem;font-family:inherit}
@@ -187,12 +168,11 @@ header .tagline{color:var(--text2);margin-top:.4rem;font-size:.95rem}
187
  .file-upload.has-image{padding:.5rem}
188
  .file-upload img{max-height:140px;border-radius:8px}
189
  .preview-name{font-size:.85rem;color:var(--accent2);margin-top:.3rem}
190
- /* Gallery */
191
- .gallery{display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:.75rem;max-height:70vh;overflow-y:auto;padding-right:.3rem}
192
  .job-card{background:var(--surface2);border:1px solid var(--border);border-radius:10px;overflow:hidden}
193
  .job-card video{width:100%;display:block;border-radius:10px 10px 0 0;background:#000}
194
  .job-card .meta{padding:.6rem .8rem;font-size:.8rem}
195
- .job-card .prompt{color:var(--text);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;margin-bottom:.2rem}
196
  .job-card .info{color:var(--text2);display:flex;justify-content:space-between}
197
  .job-card .status{font-weight:600}
198
  .job-card .status.running{color:#ffab40}
@@ -218,11 +198,10 @@ footer a{color:var(--accent2)}
218
  <div class="app">
219
  <header>
220
  <h1>🎬 Wan2.2 TI2V 5B</h1>
221
- <p class="tagline">Text-to-Video &amp; Image-to-Video · 720p @ 24fps · Single-GPU</p>
222
  </header>
223
 
224
  <div class="layout">
225
- <!-- Left: Generation Form -->
226
  <div class="card">
227
  <h2>✨ Create Video</h2>
228
  <form id="gen-form">
@@ -235,7 +214,7 @@ footer a{color:var(--accent2)}
235
  <div class="file-upload" id="file-area">
236
  <div id="file-placeholder">
237
  <div class="icon">🖼️</div>
238
- <p class="hint">Click or drag to upload an image</p>
239
  </div>
240
  <img id="preview-img" style="display:none" alt=""/>
241
  <div class="preview-name" id="preview-name" style="display:none"></div>
@@ -244,23 +223,21 @@ footer a{color:var(--accent2)}
244
  </div>
245
  <div class="row">
246
  <div class="field">
247
- <label for="steps">Steps <span style="color:var(--text2);font-weight:400">(20–50)</span></label>
248
  <input type="number" id="steps" value="30" min="20" max="50"/>
249
  </div>
250
  <div class="field">
251
- <label for="frames">Frames <span style="color:var(--text2);font-weight:400">(9–121)</span></label>
252
- <input type="number" id="frames" value="81" min="9" max="121"/>
 
 
 
 
253
  </div>
254
  </div>
255
- <div class="row">
256
- <div class="field">
257
- <label for="guidance">Guidance Scale</label>
258
- <input type="number" id="guidance" value="5" min="1" max="10" step="0.5"/>
259
- </div>
260
- <div class="field">
261
- <label for="seed">Seed <span style="color:var(--text2);font-weight:400">(empty = random)</span></label>
262
- <input type="number" id="seed" placeholder="Random"/>
263
- </div>
264
  </div>
265
  <button type="submit" class="btn btn-primary" id="submit-btn">🎬 Generate Video</button>
266
  <div id="status-msg"></div>
@@ -268,7 +245,6 @@ footer a{color:var(--accent2)}
268
  </form>
269
  </div>
270
 
271
- <!-- Right: Gallery -->
272
  <div class="card">
273
  <div class="tabs">
274
  <div class="tab active" data-tab="gallery">📽️ Gallery</div>
@@ -276,38 +252,32 @@ footer a{color:var(--accent2)}
276
  </div>
277
  <div id="tab-gallery">
278
  <div class="gallery" id="gallery"></div>
279
- <div class="empty-state" id="empty-state"><div class="icon">🎞️</div><p>Your generated videos will appear here</p></div>
280
  </div>
281
  <div id="tab-api" style="display:none">
282
  <h2 style="font-size:1rem;margin-bottom:.5rem">API Usage</h2>
283
- <p style="color:var(--text2);font-size:.85rem;margin-bottom:1rem">The <code>/generate</code> endpoint accepts form-data POST:</p>
284
- <pre style="background:var(--surface2);padding:1rem;border-radius:10px;overflow-x:auto;font-size:.8rem;line-height:1.5;color:var(--text)"><b># Text-to-Video</b>
285
  curl -X POST <span id="api-url">…</span>/generate \\
286
  -F "prompt=A cat astronaut in space" \\
287
- -F "steps=30" -F "num_frames=81" \\
288
- -o video.mp4
289
 
290
  <b># Image-to-Video</b>
291
  curl -X POST <span id="api-url2">…</span>/generate \\
292
  -F "prompt=Animate this scene" \\
293
- -F "image=@cat.jpg" \\
294
- -F "steps=30" -o video.mp4
295
-
296
- <b># Check status</b>
297
- curl <span id="api-url3">…</span>/health | jq</pre>
298
  </div>
299
  </div>
300
  </div>
301
 
302
  <footer>
303
- Powered by <a href="https://huggingface.co/Wan-AI/Wan2.2-TI2V-5B-Diffusers" target="_blank">Wan-AI/Wan2.2-TI2V-5B</a>
304
- &middot; Apache 2.0 &middot; Diffusers
305
  </footer>
306
  </div>
307
 
308
  <script>
309
  const API = window.location.origin;
310
- document.querySelectorAll('#api-url,#api-url2,#api-url3').forEach(e=>e.textContent=API);
311
 
312
  const form = document.getElementById('gen-form');
313
  const submitBtn = document.getElementById('submit-btn');
@@ -317,7 +287,6 @@ const progressFill = progressBar.querySelector('.fill');
317
  const gallery = document.getElementById('gallery');
318
  const emptyState = document.getElementById('empty-state');
319
 
320
- // File preview
321
  const imageInput = document.getElementById('image');
322
  const previewImg = document.getElementById('preview-img');
323
  const previewName = document.getElementById('preview-name');
@@ -332,7 +301,6 @@ imageInput.addEventListener('change',()=>{
332
  const r=new FileReader();r.onload=e=>{previewImg.src=e.target.result};r.readAsDataURL(f);
333
  });
334
 
335
- // Tabs
336
  document.querySelectorAll('.tab').forEach(t=>t.addEventListener('click',()=>{
337
  document.querySelectorAll('.tab').forEach(x=>x.classList.remove('active'));
338
  t.classList.add('active');
@@ -340,30 +308,28 @@ document.querySelectorAll('.tab').forEach(t=>t.addEventListener('click',()=>{
340
  document.getElementById('tab-api').style.display=t.dataset.tab==='api'?'':'none';
341
  }));
342
 
343
- // Submit
344
  form.addEventListener('submit',async e=>{
345
  e.preventDefault();
346
  submitBtn.disabled=true;
347
- statusMsg.textContent='⏳ Queuing…';
348
- progressBar.style.display='';progressFill.style.width='10%';
349
 
350
  const fd=new FormData(form);
351
  try{
352
  const res=await fetch(API+'/generate',{method:'POST',body:fd});
353
  if(!res.ok){const t=await res.text();throw new Error(t||res.statusText)}
354
  progressFill.style.width='95%';
355
- statusMsg.textContent='✅ Done! Refreshing gallery…';
356
- setTimeout(loadGallery,1500);
357
  }catch(err){
358
  statusMsg.textContent='❌ '+err.message;
359
  progressBar.style.display='none';
360
  }finally{
361
  submitBtn.disabled=false;
362
- setTimeout(()=>{progressBar.style.display='none';statusMsg.textContent=''},4000);
363
  }
364
  });
365
 
366
- // Load gallery
367
  async function loadGallery(){
368
  try{
369
  const res=await fetch(API+'/jobs');
@@ -372,11 +338,11 @@ async function loadGallery(){
372
  emptyState.style.display='none';
373
  gallery.innerHTML=jobs.reverse().map(j=>{
374
  const dur=j.duration?j.duration+'s':'';
375
- const statusCls=j.status;
376
  if(j.status==='done'&&j.output){
377
- return `<div class="job-card"><video src="${j.output}" controls preload="metadata"></video><div class="meta"><div class="prompt">${esc(j.prompt)}</div><div class="info"><span class="status ${statusCls}">✓ Done</span><span>${dur}</span></div></div></div>`;
378
  }else if(j.status==='running'){
379
- return `<div class="job-card" style="padding:2rem;text-align:center"><div class="spinner"></div><div class="meta" style="margin-top:.5rem"><div class="prompt">${esc(j.prompt)}</div><div class="info"><span class="status running">Generating…</span></div></div></div>`;
380
  }else if(j.status==='queued'){
381
  return `<div class="job-card" style="padding:2rem;text-align:center"><div style="font-size:2rem">⏳</div><div class="meta"><div class="prompt">${esc(j.prompt)}</div><div class="info"><span class="status queued">Queued</span></div></div></div>`;
382
  }else{
@@ -387,7 +353,6 @@ async function loadGallery(){
387
  }
388
  function esc(s){const d=document.createElement('div');d.textContent=s||'';return d.innerHTML}
389
 
390
- // Poll running jobs
391
  setInterval(loadGallery,5000);
392
  loadGallery();
393
  </script>
@@ -395,26 +360,27 @@ loadGallery();
395
  </html>""")
396
 
397
 
398
- # ---------- Generate endpoint ----------
399
  @app.post("/generate")
400
  async def generate(
401
  prompt: str = Form(...),
402
  image: UploadFile = File(None),
403
  steps: int = Form(30, ge=20, le=50),
404
- num_frames: int = Form(81, ge=9, le=121),
405
  guidance_scale: float = Form(5.0),
406
- seed: Optional[int] = Form(None),
407
  ):
408
- global pipe
409
- if pipe is None:
410
- raise HTTPException(503, "Model still loading — try again in a moment.")
411
 
412
- input_image = None
 
413
  if image and image.filename:
414
  try:
415
  from PIL import Image as PILImage
416
  contents = await image.read()
417
- input_image = PILImage.open(io.BytesIO(contents)).convert("RGB")
 
 
 
418
  except Exception as e:
419
  raise HTTPException(400, f"Failed to read image: {e}")
420
 
@@ -424,46 +390,37 @@ async def generate(
424
  "prompt": prompt[:300],
425
  "status": "queued",
426
  "steps": steps,
427
- "num_frames": num_frames,
428
  "guidance_scale": guidance_scale,
429
- "seed": seed,
430
- "has_image": input_image is not None,
431
  "created_at": datetime.utcnow().isoformat(),
432
  }
433
  _save_jobs()
434
 
435
- # Run generation in background
436
- asyncio.create_task(_run_generation(job_id, prompt, input_image, steps, guidance_scale, num_frames, seed))
437
 
438
- # Poll until done (max 10 minutes)
439
  for _ in range(600):
440
  await asyncio.sleep(1)
441
  status = _jobs.get(job_id, {}).get("status")
442
  if status == "done":
443
- return FileResponse(str(OUTPUT_DIR / f"{job_id}.mp4"), media_type="video/mp4", filename=f"wan2.2_{job_id}.mp4")
 
 
 
 
444
  if status == "error":
445
  raise HTTPException(500, _jobs[job_id].get("error", "Unknown error"))
446
 
447
  raise HTTPException(504, "Generation timed out after 10 minutes.")
448
 
449
 
450
- # ---------- Gallery / Jobs API ----------
451
  @app.get("/jobs")
452
  async def list_jobs():
453
- jobs = sorted(_jobs.values(), key=lambda j: j.get("created_at", ""), reverse=True)
454
- # Strip sensitive / large fields
455
- return [{k: v for k, v in j.items() if k != "error" or v} for j in jobs]
456
-
457
-
458
- @app.get("/jobs/{job_id}")
459
- async def get_job(job_id: str):
460
- j = _jobs.get(job_id)
461
- if not j:
462
- raise HTTPException(404, "Job not found")
463
- return j
464
 
465
 
466
- # ---------- Serve outputs ----------
467
  @app.get("/output/{filename}")
468
  async def serve_output(filename: str):
469
  fp = OUTPUT_DIR / filename
@@ -472,17 +429,14 @@ async def serve_output(filename: str):
472
  return FileResponse(str(fp), media_type="video/mp4")
473
 
474
 
475
- # ---------- Health ----------
476
  @app.get("/health")
477
  def health():
478
- gpu = torch.cuda.is_available()
479
  return {
480
- "status": "ok" if gpu and pipe is not None else "loading",
481
- "gpu": gpu,
482
- "gpu_name": torch.cuda.get_device_name(0) if gpu else None,
483
- "vram_gb": round(torch.cuda.get_device_properties(0).total_memory / 1e9, 1) if gpu else None,
484
- "model_loaded": pipe is not None,
485
- "model_id": MODEL_ID,
486
  "jobs_count": len(_jobs),
487
  }
488
 
 
1
  """
2
  Wan2.2 TI2V 5B — Text-to-Video & Image-to-Video
3
+ Uses official Wan2.2 inference code (generate.py) for native T2V + I2V support.
4
+ FastAPI wrapper for Hugging Face Docker Space.
5
  """
6
 
7
  import os
8
  import io
 
9
  import json
10
  import uuid
11
  import time
12
  import asyncio
13
+ import subprocess
14
  from pathlib import Path
15
  from contextlib import asynccontextmanager
16
  from datetime import datetime
17
  from typing import Optional
18
 
19
+ from fastapi import FastAPI, Form, UploadFile, File, HTTPException
20
+ from fastapi.responses import FileResponse, HTMLResponse
 
 
 
 
 
21
 
22
  # ---------- Config ----------
23
+ CKPT_DIR = os.getenv("CKPT_DIR", "/data/models/Wan2.2-TI2V-5B")
24
+ WAN22_DIR = Path("/app/Wan2.2")
25
  OUTPUT_DIR = Path("/tmp/outputs")
26
  OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
27
  JOBS_FILE = OUTPUT_DIR / "jobs.json"
28
 
29
+ # Job store
 
 
 
 
30
  _jobs: dict = {}
 
31
 
32
 
33
  def _load_jobs():
 
43
  JOBS_FILE.write_text(json.dumps(_jobs, default=str))
44
 
45
 
 
 
 
 
 
 
 
46
  @asynccontextmanager
47
  async def lifespan(app: FastAPI):
 
48
  _load_jobs()
49
+ ckpt_ok = Path(CKPT_DIR).exists()
50
+ print(f"[startup] CKPT_DIR={CKPT_DIR} exists={ckpt_ok}")
51
+ print(f"[startup] Wan2.2 dir={WAN22_DIR} exists={WAN22_DIR.exists()}")
52
+ if not ckpt_ok:
53
+ print("[startup] WARNING: model checkpoint not found!")
 
 
 
 
 
 
 
 
 
 
 
 
54
  yield
 
55
 
56
 
57
  app = FastAPI(lifespan=lifespan, title="Wan2.2 TI2V 5B")
58
 
 
59
 
60
+ def _check_model():
61
+ if not Path(CKPT_DIR).exists():
62
+ raise HTTPException(503, "Model checkpoint not found. Building/downloading…")
63
+
64
+
65
+ async def _run_generation(job_id: str, prompt: str, image_path: Optional[str], steps: int,
66
+ guidance_scale: float, size: str):
67
  _jobs[job_id]["status"] = "running"
68
  _jobs[job_id]["started_at"] = time.time()
69
  _save_jobs()
70
 
71
+ out_path = OUTPUT_DIR / f"{job_id}.mp4"
72
+
73
+ cmd = [
74
+ "python", str(WAN22_DIR / "generate.py"),
75
+ "--task", "ti2v-5B",
76
+ "--size", size,
77
+ "--ckpt_dir", CKPT_DIR,
78
+ "--offload_model", "True",
79
+ "--convert_model_dtype",
80
+ "--t5_cpu",
81
+ "--prompt", prompt,
82
+ "--sample_steps", str(steps),
83
+ "--guide_scale", str(guidance_scale),
84
+ ]
85
+ if image_path:
86
+ cmd += ["--image", image_path]
87
+
88
+ print(f"[generate {job_id}] {'I2V' if image_path else 'T2V'} prompt={prompt[:80]!r}")
89
 
90
  try:
91
+ proc = await asyncio.create_subprocess_exec(
92
+ *cmd,
93
+ stdout=asyncio.subprocess.PIPE,
94
+ stderr=asyncio.subprocess.PIPE,
95
+ cwd=str(WAN22_DIR),
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  )
97
+ stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=600)
 
 
 
98
 
99
+ if proc.returncode != 0:
100
+ err = stderr.decode()[-500:]
101
+ raise RuntimeError(f"generate.py failed (code {proc.returncode}): {err}")
102
+
103
+ # The official generate.py saves output to the working dir.
104
+ # Find the generated file and move it.
105
+ generated = sorted(WAN22_DIR.glob("*.mp4"), key=os.path.getmtime, reverse=True)
106
+ if generated:
107
+ import shutil
108
+ shutil.move(str(generated[0]), str(out_path))
109
+ else:
110
+ raise RuntimeError("No output video produced. stdout: " + stdout.decode()[-200:])
111
 
112
  duration = round(time.time() - _jobs[job_id]["started_at"], 1)
113
  _jobs[job_id]["status"] = "done"
114
  _jobs[job_id]["duration"] = duration
115
+ _jobs[job_id]["output"] = f"/output/{job_id}.mp4"
 
 
116
 
117
+ except asyncio.TimeoutError:
118
  _jobs[job_id]["status"] = "error"
119
+ _jobs[job_id]["error"] = "Generation timed out after 10 minutes."
120
  except Exception as e:
121
  _jobs[job_id]["status"] = "error"
122
  _jobs[job_id]["error"] = str(e)
123
  finally:
 
124
  _save_jobs()
125
 
126
 
 
138
  --bg: #0a0a14; --surface: #13132a; --surface2: #1c1c3a;
139
  --border: #2a2a4a; --text: #e2e2f0; --text2: #8888aa;
140
  --accent: #7c4dff; --accent2: #b388ff; --danger: #ff5252;
141
+ --radius: 14px;
142
  }
143
  *{box-sizing:border-box;margin:0;padding:0}
144
  body{font-family:-apple-system,'Segoe UI',Roboto,sans-serif;background:var(--bg);color:var(--text);min-height:100vh}
 
148
  header .tagline{color:var(--text2);margin-top:.4rem;font-size:.95rem}
149
  .layout{display:grid;grid-template-columns:1fr 1fr;gap:1.5rem;align-items:start}
150
  @media(max-width:800px){.layout{grid-template-columns:1fr}}
151
+ .card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:1.5rem}
152
+ .card h2{font-size:1.15rem;margin-bottom:1rem}
153
  .field{margin-bottom:1rem}
154
  .field label{display:block;font-weight:600;margin-bottom:.35rem;font-size:.88rem;color:var(--text2)}
155
  .field textarea,.field input,.field select{width:100%;padding:.7rem .85rem;border-radius:10px;border:1px solid var(--border);background:var(--surface2);color:var(--text);font-size:.93rem;font-family:inherit}
 
168
  .file-upload.has-image{padding:.5rem}
169
  .file-upload img{max-height:140px;border-radius:8px}
170
  .preview-name{font-size:.85rem;color:var(--accent2);margin-top:.3rem}
171
+ .gallery{display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:.75rem;max-height:70vh;overflow-y:auto}
 
172
  .job-card{background:var(--surface2);border:1px solid var(--border);border-radius:10px;overflow:hidden}
173
  .job-card video{width:100%;display:block;border-radius:10px 10px 0 0;background:#000}
174
  .job-card .meta{padding:.6rem .8rem;font-size:.8rem}
175
+ .job-card .prompt{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;margin-bottom:.2rem}
176
  .job-card .info{color:var(--text2);display:flex;justify-content:space-between}
177
  .job-card .status{font-weight:600}
178
  .job-card .status.running{color:#ffab40}
 
198
  <div class="app">
199
  <header>
200
  <h1>🎬 Wan2.2 TI2V 5B</h1>
201
+ <p class="tagline">Text-to-Video &amp; Image-to-Video · 720p @ 24fps</p>
202
  </header>
203
 
204
  <div class="layout">
 
205
  <div class="card">
206
  <h2>✨ Create Video</h2>
207
  <form id="gen-form">
 
214
  <div class="file-upload" id="file-area">
215
  <div id="file-placeholder">
216
  <div class="icon">🖼️</div>
217
+ <p class="hint">Click or drag to upload</p>
218
  </div>
219
  <img id="preview-img" style="display:none" alt=""/>
220
  <div class="preview-name" id="preview-name" style="display:none"></div>
 
223
  </div>
224
  <div class="row">
225
  <div class="field">
226
+ <label for="steps">Steps (20–50)</label>
227
  <input type="number" id="steps" value="30" min="20" max="50"/>
228
  </div>
229
  <div class="field">
230
+ <label for="size">Resolution</label>
231
+ <select id="size" name="size">
232
+ <option value="1280*704">1280×704 (16:9)</option>
233
+ <option value="704*1280">704×1280 (9:16)</option>
234
+ <option value="960*960">960×960 (1:1)</option>
235
+ </select>
236
  </div>
237
  </div>
238
+ <div class="field">
239
+ <label for="guidance">Guidance Scale</label>
240
+ <input type="number" id="guidance" value="5" min="1" max="10" step="0.5"/>
 
 
 
 
 
 
241
  </div>
242
  <button type="submit" class="btn btn-primary" id="submit-btn">🎬 Generate Video</button>
243
  <div id="status-msg"></div>
 
245
  </form>
246
  </div>
247
 
 
248
  <div class="card">
249
  <div class="tabs">
250
  <div class="tab active" data-tab="gallery">📽️ Gallery</div>
 
252
  </div>
253
  <div id="tab-gallery">
254
  <div class="gallery" id="gallery"></div>
255
+ <div class="empty-state" id="empty-state"><div class="icon">🎞️</div><p>Generated videos appear here</p></div>
256
  </div>
257
  <div id="tab-api" style="display:none">
258
  <h2 style="font-size:1rem;margin-bottom:.5rem">API Usage</h2>
259
+ <pre style="background:var(--surface2);padding:1rem;border-radius:10px;overflow-x:auto;font-size:.8rem;line-height:1.5"><b># Text-to-Video</b>
 
260
  curl -X POST <span id="api-url">…</span>/generate \\
261
  -F "prompt=A cat astronaut in space" \\
262
+ -F "steps=30" -o video.mp4
 
263
 
264
  <b># Image-to-Video</b>
265
  curl -X POST <span id="api-url2">…</span>/generate \\
266
  -F "prompt=Animate this scene" \\
267
+ -F "image=@cat.jpg" -o video.mp4</pre>
 
 
 
 
268
  </div>
269
  </div>
270
  </div>
271
 
272
  <footer>
273
+ Powered by <a href="https://huggingface.co/Wan-AI/Wan2.2-TI2V-5B" target="_blank">Wan-AI/Wan2.2-TI2V-5B</a>
274
+ · Official inference · Apache 2.0
275
  </footer>
276
  </div>
277
 
278
  <script>
279
  const API = window.location.origin;
280
+ document.querySelectorAll('#api-url,#api-url2').forEach(e=>e.textContent=API);
281
 
282
  const form = document.getElementById('gen-form');
283
  const submitBtn = document.getElementById('submit-btn');
 
287
  const gallery = document.getElementById('gallery');
288
  const emptyState = document.getElementById('empty-state');
289
 
 
290
  const imageInput = document.getElementById('image');
291
  const previewImg = document.getElementById('preview-img');
292
  const previewName = document.getElementById('preview-name');
 
301
  const r=new FileReader();r.onload=e=>{previewImg.src=e.target.result};r.readAsDataURL(f);
302
  });
303
 
 
304
  document.querySelectorAll('.tab').forEach(t=>t.addEventListener('click',()=>{
305
  document.querySelectorAll('.tab').forEach(x=>x.classList.remove('active'));
306
  t.classList.add('active');
 
308
  document.getElementById('tab-api').style.display=t.dataset.tab==='api'?'':'none';
309
  }));
310
 
 
311
  form.addEventListener('submit',async e=>{
312
  e.preventDefault();
313
  submitBtn.disabled=true;
314
+ statusMsg.textContent='⏳ Generating… (~8 min for 81 frames on T4)';
315
+ progressBar.style.display='';progressFill.style.width='15%';
316
 
317
  const fd=new FormData(form);
318
  try{
319
  const res=await fetch(API+'/generate',{method:'POST',body:fd});
320
  if(!res.ok){const t=await res.text();throw new Error(t||res.statusText)}
321
  progressFill.style.width='95%';
322
+ statusMsg.textContent='✅ Done!';
323
+ loadGallery();
324
  }catch(err){
325
  statusMsg.textContent='❌ '+err.message;
326
  progressBar.style.display='none';
327
  }finally{
328
  submitBtn.disabled=false;
329
+ setTimeout(()=>{progressBar.style.display='none';statusMsg.textContent=''},5000);
330
  }
331
  });
332
 
 
333
  async function loadGallery(){
334
  try{
335
  const res=await fetch(API+'/jobs');
 
338
  emptyState.style.display='none';
339
  gallery.innerHTML=jobs.reverse().map(j=>{
340
  const dur=j.duration?j.duration+'s':'';
341
+ const cls=j.status;
342
  if(j.status==='done'&&j.output){
343
+ return `<div class="job-card"><video src="${j.output}" controls preload="metadata"></video><div class="meta"><div class="prompt">${esc(j.prompt)}</div><div class="info"><span class="status ${cls}">✓ Done</span><span>${dur}</span></div></div></div>`;
344
  }else if(j.status==='running'){
345
+ return `<div class="job-card" style="padding:2rem;text-align:center"><div class="spinner"></div><div class="meta"><div class="prompt">${esc(j.prompt)}</div><div class="info"><span class="status running">Generating…</span></div></div></div>`;
346
  }else if(j.status==='queued'){
347
  return `<div class="job-card" style="padding:2rem;text-align:center"><div style="font-size:2rem">⏳</div><div class="meta"><div class="prompt">${esc(j.prompt)}</div><div class="info"><span class="status queued">Queued</span></div></div></div>`;
348
  }else{
 
353
  }
354
  function esc(s){const d=document.createElement('div');d.textContent=s||'';return d.innerHTML}
355
 
 
356
  setInterval(loadGallery,5000);
357
  loadGallery();
358
  </script>
 
360
  </html>""")
361
 
362
 
363
+ # ---------- Generate ----------
364
  @app.post("/generate")
365
  async def generate(
366
  prompt: str = Form(...),
367
  image: UploadFile = File(None),
368
  steps: int = Form(30, ge=20, le=50),
369
+ size: str = Form("1280*704"),
370
  guidance_scale: float = Form(5.0),
 
371
  ):
372
+ _check_model()
 
 
373
 
374
+ # Save uploaded image if present
375
+ image_path = None
376
  if image and image.filename:
377
  try:
378
  from PIL import Image as PILImage
379
  contents = await image.read()
380
+ img = PILImage.open(io.BytesIO(contents)).convert("RGB")
381
+ img_path = OUTPUT_DIR / f"{uuid.uuid4().hex[:8]}.jpg"
382
+ img.save(str(img_path), "JPEG", quality=95)
383
+ image_path = str(img_path)
384
  except Exception as e:
385
  raise HTTPException(400, f"Failed to read image: {e}")
386
 
 
390
  "prompt": prompt[:300],
391
  "status": "queued",
392
  "steps": steps,
393
+ "size": size,
394
  "guidance_scale": guidance_scale,
395
+ "has_image": image_path is not None,
 
396
  "created_at": datetime.utcnow().isoformat(),
397
  }
398
  _save_jobs()
399
 
400
+ asyncio.create_task(_run_generation(job_id, prompt, image_path, steps, guidance_scale, size))
 
401
 
402
+ # Poll until done (max 10 min)
403
  for _ in range(600):
404
  await asyncio.sleep(1)
405
  status = _jobs.get(job_id, {}).get("status")
406
  if status == "done":
407
+ return FileResponse(
408
+ str(OUTPUT_DIR / f"{job_id}.mp4"),
409
+ media_type="video/mp4",
410
+ filename=f"wan2.2_{job_id}.mp4"
411
+ )
412
  if status == "error":
413
  raise HTTPException(500, _jobs[job_id].get("error", "Unknown error"))
414
 
415
  raise HTTPException(504, "Generation timed out after 10 minutes.")
416
 
417
 
418
+ # ---------- Gallery / Jobs ----------
419
  @app.get("/jobs")
420
  async def list_jobs():
421
+ return sorted(_jobs.values(), key=lambda j: j.get("created_at", ""), reverse=True)
 
 
 
 
 
 
 
 
 
 
422
 
423
 
 
424
  @app.get("/output/{filename}")
425
  async def serve_output(filename: str):
426
  fp = OUTPUT_DIR / filename
 
429
  return FileResponse(str(fp), media_type="video/mp4")
430
 
431
 
 
432
  @app.get("/health")
433
  def health():
434
+ ckpt_ok = Path(CKPT_DIR).exists()
435
  return {
436
+ "status": "ok" if ckpt_ok else "building",
437
+ "model_dir": CKPT_DIR,
438
+ "model_exists": ckpt_ok,
439
+ "wan22_dir": str(WAN22_DIR),
 
 
440
  "jobs_count": len(_jobs),
441
  }
442
 
requirements.txt CHANGED
@@ -1,15 +1,4 @@
1
  fastapi
2
  uvicorn[standard]
3
  python-multipart
4
- transformers
5
- accelerate
6
- ftfy
7
- sentencepiece
8
- protobuf
9
- imageio
10
- imageio-ffmpeg
11
- numpy<2
12
  Pillow
13
- safetensors
14
- huggingface_hub
15
- einops
 
1
  fastapi
2
  uvicorn[standard]
3
  python-multipart
 
 
 
 
 
 
 
 
4
  Pillow