kikopubisher commited on
Commit
7d89612
โ€ข
1 Parent(s): 5b062dc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import DiffusionPipeline
4
+ from PIL import Image
5
+ import numpy as np
6
+ import io
7
+
8
+ # ุชุญู…ูŠู„ ุงู„ู†ู…ูˆุฐุฌ
9
+ model_id = "stabilityai/stable-video-diffusion-img2vid"
10
+ pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
11
+
12
+ def generate_video(image):
13
+ # ุชุญูˆูŠู„ ุงู„ุตูˆุฑุฉ ุฅู„ู‰ ุชู†ุณูˆุฑ
14
+ image_tensor = torch.tensor(np.array(image)).float().unsqueeze(0).permute(0, 3, 1, 2).to("cuda") / 255.0
15
+
16
+ # ุชูˆู„ูŠุฏ ุงู„ููŠุฏูŠูˆ
17
+ video_frames = pipe(image_tensor, num_frames=25).images # ุงูุชุฑุถู†ุง 25 ุฅุทุงุฑ ูƒุนุฏุฏ ุงูุชุฑุงุถูŠ
18
+
19
+ # ุญูุธ ุงู„ููŠุฏูŠูˆ ุงู„ู†ุงุชุฌ ุฅู„ู‰ ู…ู„ู
20
+ video_bytes = io.BytesIO()
21
+ video_frames[0].save(video_bytes, format="mp4") # ุงุญูุธ ุงู„ุฅุทุงุฑ ุงู„ุฃูˆู„ ูƒููŠุฏูŠูˆ ู„ู„ุฅุดุงุฑุฉ
22
+
23
+ return video_bytes.getvalue()
24
+
25
+ # ุฅุนุฏุงุฏ ูˆุงุฌู‡ุฉ Gradio
26
+ interface = gr.Interface(
27
+ fn=generate_video,
28
+ inputs=gr.Image(type="pil", label="Upload an Image"),
29
+ outputs=gr.File(label="Download Video"),
30
+ title="Stable Video Diffusion - Image to Video",
31
+ description="Upload an image to generate a video using the Stable Video Diffusion model.",
32
+ )
33
+
34
+ # ุชุดุบูŠู„ ุงู„ูˆุงุฌู‡ุฉ
35
+ if __name__ == "__main__":
36
+ interface.launch()