Spaces:
Running
Running
Fix errors
Browse files8 unmodified lines910111213142 unmodified lines1718192021222324254 unmodified lines30313233343536373839404 unmodified lines454647484950515253545556575810 unmodified lines697071727374752 unmodified lines787980818283848586878889901 unmodified line9293949596979847 unmodified lines1461471481491501511528 unmodified linesimport torchfrom hyvideo.utils.file_utils import save_videos_gridfrom hyvideo.config import parse_argsfrom hyvideo.inference import HunyuanVideoSamplerfrom hyvideo.constants import NEGATIVE_PROMPT2 unmodified linesif torch.cuda.device_count() > 0:snapshot_download(repo_id="tencent/HunyuanVideo", repo_type="model", local_dir="ckpts", force_download=True)def initialize_model(model_path):print('initialize_model: ' + model_path)if torch.cuda.device_count() == 0:return None4 unmodified linesprint(f"`models_root` exists: {models_root_path}")hunyuan_video_sampler = HunyuanVideoSampler.from_pretrained(models_root_path, args=args)print('Model initialized: ' + model_path)return hunyuan_video_sampler@spaces.GPU(duration=120)def generate_video(model,prompt,resolution,4 unmodified linesflow_shift,embedded_guidance_scale):if torch.cuda.device_count() == 0:gr.Warning('Set this space to GPU config to make it work.')return Noneseed = None if seed == -1 else seedwidth, height = resolution.split("x")width, height = int(width), int(height)negative_prompt = "" # not applicable in the inferenceoutputs = model.predict(prompt=prompt,10 unmodified linesembedded_guidance_scale=embedded_guidance_scale)samples = outputs['samples']sample = samples[0].unsqueeze(0)save_path = "./gradio_outputs"2 unmodified linestime_flag = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d-%H:%M:%S")video_path = f"{save_path}/{time_flag}_seed{outputs['seeds'][0]}_{outputs['prompts'][0][:100].replace('/','')}.mp4"save_videos_grid(sample, video_path, fps=24)logger.info(f'Sample saved to: {video_path}')return video_pathdef create_demo(model_path):model = initialize_model(model_path)with gr.Blocks() as demo:if torch.cuda.device_count() == 0:with gr.Row():1 unmodified line<p style="background-color: red;"><big><big><big><b>⚠️To use <i>Hunyuan Video</i>, <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/HunyuanVideo?duplicate=true">duplicate this space</a> and set a GPU with 80 GB VRAM.</b>You can't use <i>Hunyuan Video</i> directly here because this space runs on a CPU, which is not enough for <i>Hunyuan Video</i>. Please provide <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/HunyuanVideo/discussions/new">feedback</a> if you have issues.</big></big></big></p>""")gr.Markdown("# Hunyuan Video Generation")47 unmodified lines""")generate_btn.click(fn=lambda *inputs: generate_video(model, *inputs),inputs=[prompt,resolution,8 unmodified lines91011121314152 unmodified lines18192021222324252627282930313233343536374 unmodified lines4243444546474849505152535455565758596061626364656667686970717273747576774 unmodified lines8283848586878889909192939495969710 unmodified lines1081091101111121131141152 unmodified lines1181191201211221231241251261271281291 unmodified line13113213313413513613713847 unmodified lines1861871881891901911928 unmodified linesimport torchfrom hyvideo.utils.file_utils import save_videos_gridfrom hyvideo.utils.preprocess_text_encoder_tokenizer_utils import preprocess_text_encoder_tokenizerfrom hyvideo.config import parse_argsfrom hyvideo.inference import HunyuanVideoSamplerfrom hyvideo.constants import NEGATIVE_PROMPT2 unmodified linesif torch.cuda.device_count() > 0:snapshot_download(repo_id="tencent/HunyuanVideo", repo_type="model", local_dir="ckpts", force_download=True)snapshot_download(repo_id="xtuner/llava-llama-3-8b-v1_1-transformers", repo_type="model", local_dir="ckpts/llava-llama-3-8b-v1_1-transformers", force_download=True)class Args:def __init__(self, input_dir, output_dir):self.input_dir = input_dirself.output_dir = output_dir# Create the objectargs = Args("ckpts/llava-llama-3-8b-v1_1-transformers", "ckpts/text_encoder")preprocess_text_encoder_tokenizer(args)snapshot_download(repo_id="openai/clip-vit-large-patch14", repo_type="model", local_dir="ckpts/text_encoder_2", force_download=True)def initialize_model(model_path):print("initialize_model: " + model_path)if torch.cuda.device_count() == 0:return None4 unmodified linesprint(f"`models_root` exists: {models_root_path}")hunyuan_video_sampler = HunyuanVideoSampler.from_pretrained(models_root_path, args=args)print("Model initialized: " + model_path)return hunyuan_video_samplermodel = initialize_model("ckpts")def generate_video(prompt,resolution,video_length,seed,num_inference_steps,guidance_scale,flow_shift,embedded_guidance_scale):print("generate_video (prompt: " + prompt + ")")return generate_video_gpu(model,prompt,resolution,video_length,seed,num_inference_steps,guidance_scale,flow_shift,embedded_guidance_scale)@spaces.GPU(duration=120)def generate_video_gpu(model,prompt,resolution,4 unmodified linesflow_shift,embedded_guidance_scale):print("generate_video_gpu (prompt: " + prompt + ")")if torch.cuda.device_count() == 0:gr.Warning("Set this space to GPU config to make it work.")return Noneseed = None if seed == -1 else seedwidth, height = resolution.split("x")width, height = int(width), int(height)negative_prompt = "" # not applicable in the inferenceprint("Predicting video...")outputs = model.predict(prompt=prompt,10 unmodified linesembedded_guidance_scale=embedded_guidance_scale)print("Video predicted")samples = outputs["samples"]sample = samples[0].unsqueeze(0)save_path = "./gradio_outputs"2 unmodified linestime_flag = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d-%H:%M:%S")video_path = f"{save_path}/{time_flag}_seed{outputs['seeds'][0]}_{outputs['prompts'][0][:100].replace('/','')}.mp4"save_videos_grid(sample, video_path, fps=24)logger.info(f"Sample saved to: {video_path}")print("Return the video")return video_pathdef create_demo(model_path):with gr.Blocks() as demo:if torch.cuda.device_count() == 0:with gr.Row():1 unmodified line<p style="background-color: red;"><big><big><big><b>⚠️To use <i>Hunyuan Video</i>, <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/HunyuanVideo?duplicate=true">duplicate this space</a> and set a GPU with 80 GB VRAM.</b>You can't use <i>Hunyuan Video</i> directly here because this space runs on a CPU, which is not enough for <i>Hunyuan Video</i>. Please provide <a href="https://huggingface.co/spaces/Fabrice-TIERCELIN/HunyuanVideo/discussions/new">feedback</a> if you have issues.</big></big></big></p><br/><p style="background-color: light-green;"><big>The space has been successfully deployed on A100 space on 2025-01-23. Synchronize your space to fix the errors.</big></p>""")gr.Markdown("# Hunyuan Video Generation")47 unmodified lines""")generate_btn.click(fn=generate_video,inputs=[prompt,resolution,
app.py+50-10