jbilcke-hf HF staff commited on
Commit
c55eec4
1 Parent(s): 4648c2c

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +19 -13
handler.py CHANGED
@@ -13,6 +13,13 @@ from hyvideo.constants import NEGATIVE_PROMPT
13
  # Configure logger
14
  logger.add("handler_debug.log", rotation="500 MB")
15
 
 
 
 
 
 
 
 
16
  def setup_vae_path(vae_path: Path) -> Path:
17
  """Create a temporary directory with correctly named VAE config file"""
18
  tmp_vae_dir = Path("/tmp/vae")
@@ -43,7 +50,7 @@ def get_default_args():
43
 
44
  # Model configuration
45
  parser.add_argument("--model", type=str, default="HYVideo-T/2-cfgdistill")
46
- parser.add_argument("--model-resolution", type=str, default="720p", choices=["540p", "720p"])
47
  parser.add_argument("--latent-channels", type=int, default=16)
48
  parser.add_argument("--precision", type=str, default="bf16", choices=["bf16", "fp32", "fp16"])
49
  parser.add_argument("--rope-theta", type=int, default=256)
@@ -93,7 +100,7 @@ def get_default_args():
93
  # Hardware settings
94
  parser.add_argument("--use-cpu-offload", action="store_true", default=False)
95
  parser.add_argument("--batch-size", type=int, default=1)
96
- parser.add_argument("--infer-steps", type=int, default=50)
97
  parser.add_argument("--disable-autocast", action="store_true")
98
 
99
  # Output settings
@@ -103,8 +110,8 @@ def get_default_args():
103
 
104
  # Generation settings
105
  parser.add_argument("--num-videos", type=int, default=1)
106
- parser.add_argument("--video-size", nargs="+", type=int, default=[720, 1280])
107
- parser.add_argument("--video-length", type=int, default=129)
108
  parser.add_argument("--prompt", type=str, default=None)
109
  parser.add_argument("--seed-type", type=str, default="auto", choices=["file", "random", "fixed", "auto"])
110
  parser.add_argument("--seed", type=int, default=None)
@@ -214,14 +221,14 @@ class EndpointHandler:
214
  raise ValueError("No prompt provided in the 'inputs' field")
215
 
216
  # Parse resolution
217
- resolution = data.pop("resolution", "1280x720")
218
  width, height = map(int, resolution.split("x"))
219
 
220
  # Get other parameters with defaults
221
- video_length = int(data.pop("video_length", 129))
222
  seed = data.pop("seed", -1)
223
  seed = None if seed == -1 else int(seed)
224
- num_inference_steps = int(data.pop("num_inference_steps", 50))
225
  guidance_scale = float(data.pop("guidance_scale", 1.0))
226
  flow_shift = float(data.pop("flow_shift", 7.0))
227
  embedded_guidance_scale = float(data.pop("embedded_guidance_scale", 6.0))
@@ -253,7 +260,7 @@ class EndpointHandler:
253
 
254
  # Save to temporary file
255
  temp_path = "/tmp/temp_video.mp4"
256
- save_videos_grid(sample, temp_path, fps=24)
257
 
258
  # Read video file and convert to base64
259
  with open(temp_path, "rb") as f:
@@ -261,16 +268,15 @@ class EndpointHandler:
261
  import base64
262
  video_base64 = base64.b64encode(video_bytes).decode()
263
 
 
 
 
264
  # Cleanup
265
  os.remove(temp_path)
266
 
267
  logger.info("Successfully generated and encoded video")
268
 
269
- return {
270
- "video_base64": video_base64,
271
- "seed": outputs['seeds'][0],
272
- "prompt": outputs['prompts'][0]
273
- }
274
 
275
  except Exception as e:
276
  logger.error(f"Error during video generation: {str(e)}")
 
13
  # Configure logger
14
  logger.add("handler_debug.log", rotation="500 MB")
15
 
16
+ DEFAULT_RESOLUTION = "720p"
17
+ DEFAULT_WIDTH = 1280
18
+ DEFAULT_HEIGHT = 720
19
+ DEFAULT_NB_FRAMES = (4 * 30) + 1 # or 129 (note: hunyan requires an extra +1 frame)
20
+ DEFAULT_NB_STEPS = 22 # or 50
21
+ DEFAULT_FPS = 24
22
+
23
  def setup_vae_path(vae_path: Path) -> Path:
24
  """Create a temporary directory with correctly named VAE config file"""
25
  tmp_vae_dir = Path("/tmp/vae")
 
50
 
51
  # Model configuration
52
  parser.add_argument("--model", type=str, default="HYVideo-T/2-cfgdistill")
53
+ parser.add_argument("--model-resolution", type=str, default=DEFAULT_RESOLUTION, choices=["540p", "720p"])
54
  parser.add_argument("--latent-channels", type=int, default=16)
55
  parser.add_argument("--precision", type=str, default="bf16", choices=["bf16", "fp32", "fp16"])
56
  parser.add_argument("--rope-theta", type=int, default=256)
 
100
  # Hardware settings
101
  parser.add_argument("--use-cpu-offload", action="store_true", default=False)
102
  parser.add_argument("--batch-size", type=int, default=1)
103
+ parser.add_argument("--infer-steps", type=int, default=DEFAULT_NB_STEPS)
104
  parser.add_argument("--disable-autocast", action="store_true")
105
 
106
  # Output settings
 
110
 
111
  # Generation settings
112
  parser.add_argument("--num-videos", type=int, default=1)
113
+ parser.add_argument("--video-size", nargs="+", type=int, default=[DEFAULT_HEIGHT, DEFAULT_WIDTH])
114
+ parser.add_argument("--video-length", type=int, default=DEFAULT_NB_FRAMES)
115
  parser.add_argument("--prompt", type=str, default=None)
116
  parser.add_argument("--seed-type", type=str, default="auto", choices=["file", "random", "fixed", "auto"])
117
  parser.add_argument("--seed", type=int, default=None)
 
221
  raise ValueError("No prompt provided in the 'inputs' field")
222
 
223
  # Parse resolution
224
+ resolution = data.pop("resolution", f"{DEFAULT_WIDTH}x{DEFAULT_HEIGHT}")
225
  width, height = map(int, resolution.split("x"))
226
 
227
  # Get other parameters with defaults
228
+ video_length = int(data.pop("video_length", DEFAULT_NB_FRAMES))
229
  seed = data.pop("seed", -1)
230
  seed = None if seed == -1 else int(seed)
231
+ num_inference_steps = int(data.pop("num_inference_steps", DEFAULT_NB_STEPS))
232
  guidance_scale = float(data.pop("guidance_scale", 1.0))
233
  flow_shift = float(data.pop("flow_shift", 7.0))
234
  embedded_guidance_scale = float(data.pop("embedded_guidance_scale", 6.0))
 
260
 
261
  # Save to temporary file
262
  temp_path = "/tmp/temp_video.mp4"
263
+ save_videos_grid(sample, temp_path, fps=DEFAULT_FPS)
264
 
265
  # Read video file and convert to base64
266
  with open(temp_path, "rb") as f:
 
268
  import base64
269
  video_base64 = base64.b64encode(video_bytes).decode()
270
 
271
+ # Add MP4 data URI prefix
272
+ video_data_uri = f"data:video/mp4;base64,{video_base64}"
273
+
274
  # Cleanup
275
  os.remove(temp_path)
276
 
277
  logger.info("Successfully generated and encoded video")
278
 
279
+ return video_data_uri
 
 
 
 
280
 
281
  except Exception as e:
282
  logger.error(f"Error during video generation: {str(e)}")