redkits commited on
Commit
afe2d60
·
verified ·
1 Parent(s): 04d3033

Upload server.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. server.py +19 -11
server.py CHANGED
@@ -2,10 +2,10 @@ import os
2
  import sys
3
  import subprocess
4
  import base64
5
- import io
6
  import traceback
7
 
8
- # Install latest diffusers (supports WanPipeline) + dependencies
9
  subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "--upgrade",
10
  "diffusers", "flask", "accelerate", "sentencepiece", "protobuf", "imageio[ffmpeg]", "transformers", "huggingface_hub"])
11
 
@@ -26,19 +26,18 @@ def load_model():
26
  from diffusers import AutoencoderKLWan, WanPipeline
27
  print("[video] Loading Wan2.2-TI2V-5B pipeline...", flush=True)
28
 
29
- # VAE in float32 for quality, model in bfloat16 for speed
30
  vae = AutoencoderKLWan.from_pretrained(
31
  "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
32
  subfolder="vae",
33
- torch_dtype=torch.float32,
34
  )
35
  pipe = WanPipeline.from_pretrained(
36
  "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
37
  vae=vae,
38
  torch_dtype=torch.bfloat16,
39
  )
40
- pipe.to("cuda")
41
- print("[video] Model loaded on CUDA!", flush=True)
42
 
43
  @app.route("/health", methods=["GET"])
44
  def health():
@@ -76,11 +75,19 @@ def generate():
76
  guidance_scale=guidance,
77
  ).frames[0]
78
 
79
- buf = io.BytesIO()
80
- export_to_video(frames, buf, fps=fps)
81
- video_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
 
 
 
 
 
 
 
 
82
  duration = len(frames) / fps
83
- print(f"[video] Done: {len(frames)} frames, {duration:.1f}s, {len(buf.getvalue())} bytes", flush=True)
84
 
85
  return jsonify({
86
  "video": video_b64,
@@ -90,7 +97,8 @@ def generate():
90
  })
91
  except Exception as e:
92
  tb = traceback.format_exc()
93
- print(f"[video] ERROR: {e}\n{tb}", flush=True)
 
94
  return jsonify({"error": str(e), "traceback": tb}), 500
95
 
96
  if __name__ == "__main__":
 
2
  import sys
3
  import subprocess
4
  import base64
5
+ import tempfile
6
  import traceback
7
 
8
+ # Install latest diffusers + dependencies
9
  subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "--upgrade",
10
  "diffusers", "flask", "accelerate", "sentencepiece", "protobuf", "imageio[ffmpeg]", "transformers", "huggingface_hub"])
11
 
 
26
  from diffusers import AutoencoderKLWan, WanPipeline
27
  print("[video] Loading Wan2.2-TI2V-5B pipeline...", flush=True)
28
 
 
29
  vae = AutoencoderKLWan.from_pretrained(
30
  "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
31
  subfolder="vae",
32
+ torch_dtype=torch.bfloat16,
33
  )
34
  pipe = WanPipeline.from_pretrained(
35
  "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
36
  vae=vae,
37
  torch_dtype=torch.bfloat16,
38
  )
39
+ pipe.enable_model_cpu_offload()
40
+ print("[video] Model loaded with CPU offload!", flush=True)
41
 
42
  @app.route("/health", methods=["GET"])
43
  def health():
 
75
  guidance_scale=guidance,
76
  ).frames[0]
77
 
78
+ # Write to temp file (export_to_video needs a file path, not BytesIO)
79
+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmp:
80
+ tmp_path = tmp.name
81
+
82
+ export_to_video(frames, tmp_path, fps=fps)
83
+
84
+ with open(tmp_path, "rb") as f:
85
+ video_bytes = f.read()
86
+ os.unlink(tmp_path)
87
+
88
+ video_b64 = base64.b64encode(video_bytes).decode("utf-8")
89
  duration = len(frames) / fps
90
+ print(f"[video] Done: {len(frames)} frames, {duration:.1f}s, {len(video_bytes)} bytes", flush=True)
91
 
92
  return jsonify({
93
  "video": video_b64,
 
97
  })
98
  except Exception as e:
99
  tb = traceback.format_exc()
100
+ print(f"[video] ERROR: {e}", flush=True)
101
+ print(f"[video] TRACEBACK:\n{tb}", flush=True)
102
  return jsonify({"error": str(e), "traceback": tb}), 500
103
 
104
  if __name__ == "__main__":