Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,86 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            import os
         | 
| 3 | 
            +
            import torch
         | 
| 4 | 
            +
            from diffusers import CogVideoXImageToVideoPipeline
         | 
| 5 | 
            +
            from diffusers.utils import export_to_video, load_image
         | 
| 6 | 
            +
            from datetime import datetime
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            from huggingface_hub import hf_hub_download
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            # Ensure 'checkpoint' directory exists
         | 
| 11 | 
            +
            os.makedirs("checkpoints", exist_ok=True)
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            hf_hub_download(
         | 
| 14 | 
            +
                repo_id="wenqsun/DimensionX",
         | 
| 15 | 
            +
                filename="orbit_left_lora_weights.safetensors",
         | 
| 16 | 
            +
                local_dir="checkpoints"
         | 
| 17 | 
            +
            )
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            hf_hub_download(
         | 
| 20 | 
            +
                repo_id="wenqsun/DimensionX",
         | 
| 21 | 
            +
                filename="orbit_up_lora_weights.safetensors",
         | 
| 22 | 
            +
                local_dir="checkpoints"
         | 
| 23 | 
            +
            )
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            pipe = CogVideoXImageToVideoPipeline.from_pretrained("THUDM/CogVideoX-5b-I2V", torch_dtype=torch.bfloat16)
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            def infer(prompt, image_path, orbit_type):
         | 
| 28 | 
            +
                lora_path = None
         | 
| 29 | 
            +
                if orbit_type == "Left":
         | 
| 30 | 
            +
                    lora_path = "checkpoints/orbit_left_lora_weights.safetensors"
         | 
| 31 | 
            +
                elif orbit_type == "Up":
         | 
| 32 | 
            +
                    lora_path = "checkpoints/orbit_up_lora_weights.safetensors"
         | 
| 33 | 
            +
                lora_rank = 256
         | 
| 34 | 
            +
                pipe.load_lora_weights(lora_path, weight_name="pytorch_lora_weights.safetensors", adapter_name="test_1")
         | 
| 35 | 
            +
                pipe.fuse_lora(lora_scale=1 / lora_rank)
         | 
| 36 | 
            +
                pipe.to("cuda")
         | 
| 37 | 
            +
             | 
| 38 | 
            +
             | 
| 39 | 
            +
                prompt = f"A{prompt}. High quality, ultrarealistic detail and breath-taking movie-like camera shot."
         | 
| 40 | 
            +
                image = load_image(image_path)
         | 
| 41 | 
            +
                seed = random.randint(0, 2**8 - 1)
         | 
| 42 | 
            +
                
         | 
| 43 | 
            +
                video = pipe(
         | 
| 44 | 
            +
                    image, 
         | 
| 45 | 
            +
                    prompt, 
         | 
| 46 | 
            +
                    num_inference_steps=50,  # NOT Changed
         | 
| 47 | 
            +
                    guidance_scale=7.0,  # NOT Changed
         | 
| 48 | 
            +
                    use_dynamic_cfg=True,
         | 
| 49 | 
            +
                    generator=torch.Generator(device="cpu").manual_seed(seed)
         | 
| 50 | 
            +
                )
         | 
| 51 | 
            +
                
         | 
| 52 | 
            +
                # Generate a timestamp for the output filename
         | 
| 53 | 
            +
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
         | 
| 54 | 
            +
                export_to_video(video.frames[0], f"output_{timestamp}.mp4", fps=8)
         | 
| 55 | 
            +
                return f"output_{timestamp}.mp4"
         | 
| 56 | 
            +
             | 
| 57 | 
            +
            with gr.Blocks() as demo:
         | 
| 58 | 
            +
                with gr.Column(elem_id="col-container"):
         | 
| 59 | 
            +
                    gr.Markdown("# DimensionX")
         | 
| 60 | 
            +
                    gr.Markdown("### Create Any 3D and 4D Scenes from a Single Image with Controllable Video Diffusion")
         | 
| 61 | 
            +
                    with gr.Row():
         | 
| 62 | 
            +
                        with gr.Column():
         | 
| 63 | 
            +
                            image_in = gr.Image(label="Image Input", type="filepath")
         | 
| 64 | 
            +
                            prompt = gr.Textbox(label="Prompt")
         | 
| 65 | 
            +
                            orbit_type = gr.Radio(label="Orbit type", choices=["Left", "Up"], value="Left")
         | 
| 66 | 
            +
                            submit_btn = gr.Button("Submit")
         | 
| 67 | 
            +
                        with gr.Column():
         | 
| 68 | 
            +
                            video_out = gr.Video(label="Video output")
         | 
| 69 | 
            +
                            examples = gr.Examples(
         | 
| 70 | 
            +
                                examples = [
         | 
| 71 | 
            +
                                    [
         | 
| 72 | 
            +
                                        "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg",
         | 
| 73 | 
            +
                                        "An astronaut hatching from an egg, on the surface of the moon, the darkness and depth of space realised in the background.",
         | 
| 74 | 
            +
                                        "Left"
         | 
| 75 | 
            +
                                    ]
         | 
| 76 | 
            +
                                ],
         | 
| 77 | 
            +
                                inputs=[image_in, prompt, orbit_type]
         | 
| 78 | 
            +
                            )
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                submit_btn.click(
         | 
| 81 | 
            +
                    fn=infer,
         | 
| 82 | 
            +
                    inputs=[image_in, prompt, orbit_type],
         | 
| 83 | 
            +
                    outputs=[video_out]
         | 
| 84 | 
            +
                )
         | 
| 85 | 
            +
             | 
| 86 | 
            +
            demo.queue().launch(show_error=True, show_api=False)
         | 
