photo-to-video / app.py
kikopubisher's picture
Create app.py
7d89612 verified
import gradio as gr
import torch
from diffusers import DiffusionPipeline
from PIL import Image
import numpy as np
import io
# ุชุญู…ูŠู„ ุงู„ู†ู…ูˆุฐุฌ
model_id = "stabilityai/stable-video-diffusion-img2vid"
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
def generate_video(image):
# ุชุญูˆูŠู„ ุงู„ุตูˆุฑุฉ ุฅู„ู‰ ุชู†ุณูˆุฑ
image_tensor = torch.tensor(np.array(image)).float().unsqueeze(0).permute(0, 3, 1, 2).to("cuda") / 255.0
# ุชูˆู„ูŠุฏ ุงู„ููŠุฏูŠูˆ
video_frames = pipe(image_tensor, num_frames=25).images # ุงูุชุฑุถู†ุง 25 ุฅุทุงุฑ ูƒุนุฏุฏ ุงูุชุฑุงุถูŠ
# ุญูุธ ุงู„ููŠุฏูŠูˆ ุงู„ู†ุงุชุฌ ุฅู„ู‰ ู…ู„ู
video_bytes = io.BytesIO()
video_frames[0].save(video_bytes, format="mp4") # ุงุญูุธ ุงู„ุฅุทุงุฑ ุงู„ุฃูˆู„ ูƒููŠุฏูŠูˆ ู„ู„ุฅุดุงุฑุฉ
return video_bytes.getvalue()
# ุฅุนุฏุงุฏ ูˆุงุฌู‡ุฉ Gradio
interface = gr.Interface(
fn=generate_video,
inputs=gr.Image(type="pil", label="Upload an Image"),
outputs=gr.File(label="Download Video"),
title="Stable Video Diffusion - Image to Video",
description="Upload an image to generate a video using the Stable Video Diffusion model.",
)
# ุชุดุบูŠู„ ุงู„ูˆุงุฌู‡ุฉ
if __name__ == "__main__":
interface.launch()