File size: 1,289 Bytes
7d89612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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()