redkits commited on
Commit
eb97492
·
verified ·
1 Parent(s): 6788315

Upload server.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. server.py +26 -13
server.py CHANGED
@@ -3,26 +3,34 @@ import sys
3
  import subprocess
4
  import base64
5
  import io
6
- import json
7
 
8
- # Install diffusers (latest PyPI supports WanPipeline) + flask + imageio for video
9
- subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "--upgrade", "diffusers", "flask", "accelerate", "sentencepiece", "protobuf", "imageio[ffmpeg]", "transformers"])
 
10
 
11
  import torch
12
  from flask import Flask, request, jsonify
13
- from diffusers import AutoencoderKLWan, WanPipeline
14
- from diffusers.utils import export_to_video
 
 
 
 
15
 
16
  app = Flask(__name__)
17
  pipe = None
18
 
19
  def load_model():
20
  global pipe
 
21
  print("[video] Loading Wan2.2-TI2V-5B pipeline...", flush=True)
 
 
22
  vae = AutoencoderKLWan.from_pretrained(
23
  "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
24
  subfolder="vae",
25
- torch_dtype=torch.bfloat16,
26
  )
27
  pipe = WanPipeline.from_pretrained(
28
  "Wan-AI/Wan2.2-TI2V-5B-Diffusers",
@@ -41,19 +49,23 @@ def health():
41
  @app.route("/", methods=["POST"])
42
  def generate():
43
  try:
44
- data = request.get_json()
 
 
45
  prompt = data.get("inputs", "")
46
  params = data.get("parameters", {})
47
 
48
- num_frames = int(params.get("num_frames", 49))
49
  height = int(params.get("height", 480))
50
  width = int(params.get("width", 832))
51
- steps = int(params.get("num_inference_steps", 30))
52
  fps = int(params.get("fps", 24))
53
  guidance = float(params.get("guidance_scale", 5.0))
54
  negative = params.get("negative_prompt", "low quality, blurry, distorted")
55
 
56
- print(f"[video] Generating {num_frames} frames: {prompt[:100]}...", flush=True)
 
 
57
  frames = pipe(
58
  prompt=prompt,
59
  negative_prompt=negative,
@@ -68,7 +80,7 @@ def generate():
68
  export_to_video(frames, buf, fps=fps)
69
  video_b64 = base64.b64encode(buf.getvalue()).decode("utf-8")
70
  duration = len(frames) / fps
71
- print(f"[video] Done, {len(frames)} frames, {duration:.1f}s", flush=True)
72
 
73
  return jsonify({
74
  "video": video_b64,
@@ -77,8 +89,9 @@ def generate():
77
  "duration": duration,
78
  })
79
  except Exception as e:
80
- print(f"[video] Error: {e}", flush=True)
81
- return jsonify({"error": str(e)}), 500
 
82
 
83
  if __name__ == "__main__":
84
  load_model()
 
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
 
12
  import torch
13
  from flask import Flask, request, jsonify
14
+
15
+ print(f"[video] torch version: {torch.__version__}", flush=True)
16
+ print(f"[video] CUDA available: {torch.cuda.is_available()}", flush=True)
17
+ if torch.cuda.is_available():
18
+ print(f"[video] GPU: {torch.cuda.get_device_name(0)}", flush=True)
19
+ print(f"[video] VRAM: {torch.cuda.get_device_properties(0).total_mem / 1e9:.1f} GB", flush=True)
20
 
21
  app = Flask(__name__)
22
  pipe = None
23
 
24
  def load_model():
25
  global pipe
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",
 
49
  @app.route("/", methods=["POST"])
50
  def generate():
51
  try:
52
+ from diffusers.utils import export_to_video
53
+
54
+ data = request.get_json(force=True)
55
  prompt = data.get("inputs", "")
56
  params = data.get("parameters", {})
57
 
58
+ num_frames = int(params.get("num_frames", 25))
59
  height = int(params.get("height", 480))
60
  width = int(params.get("width", 832))
61
+ steps = int(params.get("num_inference_steps", 15))
62
  fps = int(params.get("fps", 24))
63
  guidance = float(params.get("guidance_scale", 5.0))
64
  negative = params.get("negative_prompt", "low quality, blurry, distorted")
65
 
66
+ print(f"[video] Generating {num_frames} frames: {prompt[:100]}", flush=True)
67
+ print(f"[video] Params: {width}x{height}, steps={steps}, fps={fps}", flush=True)
68
+
69
  frames = pipe(
70
  prompt=prompt,
71
  negative_prompt=negative,
 
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,
 
89
  "duration": duration,
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__":
97
  load_model()