Spaces:
Sleeping
Sleeping
Download app.py from caokhoi91/stable-video-api: direct link, hf CLI and curl.
- Browser
- Download file 985 Bytes
-
https://huggingface.co/spaces/caokhoi91/stable-video-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/caokhoi91/stable-video-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/caokhoi91/stable-video-api/resolve/main/app.py
985 Bytes
| import gradio as gr | |
| import torch | |
| from diffusers import StableVideoDiffusionPipeline | |
| from PIL import Image | |
| import requests | |
| from io import BytesIO | |
| import tempfile | |
| pipe = StableVideoDiffusionPipeline.from_pretrained( | |
| "stabilityai/stable-video-diffusion-img2vid-xt-1-1", | |
| torch_dtype=torch.float32, # dùng float32 để tương thích CPU | |
| variant="fp16" | |
| ).to("cpu") # chuyển sang CPU | |
| pipe.enable_model_cpu_offload() | |
| def generate_video(image_url): | |
| image = Image.open(BytesIO(requests.get(image_url).content)).resize((1024, 576)) | |
| result = pipe(image, decode_chunk_size=8, generator=torch.manual_seed(42)).frames[0] | |
| temp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) | |
| result.save(temp.name, fps=7) | |
| return temp.name | |
| demo = gr.Interface( | |
| fn=generate_video, | |
| inputs=gr.Text(label="Image URL"), | |
| outputs=gr.Video() | |
| ) | |
| from huggingface_hub import login | |
| import os | |
| login(token=os.environ.get("HF_TOKEN")) | |
| demo.launch() | |
| # trigger rebuild |