Spaces:
Runtime error

fugam commited on
Commit
d7de041
·
verified ·
1 Parent(s): 6df43e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -1,33 +1,35 @@
 
1
  import torch
2
- from diffusers import StableDiffusionPipeline
3
  import cv2
4
  import numpy as np
5
  import gradio as gr
6
  from PIL import Image
7
- import io
8
 
9
- # Charger le modèle pour GPU ou CPU
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
  model_id = "runwayml/stable-diffusion-v1-5"
12
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32)
 
 
 
13
  pipe = pipe.to(device)
14
 
15
  def generate_video_from_image(image, prompt, num_frames=10):
16
  frames = []
17
- image = image.resize((512, 512))
18
 
19
- # Génération des frames à partir du prompt et de l'image
20
  for i in range(num_frames):
21
  generator = torch.Generator(device).manual_seed(42 + i)
22
  prompt_variation = f"{prompt}, variation {i}"
23
- generated_image = pipe(prompt_variation, init_image=image, generator=generator).images[0]
24
  frames.append(generated_image)
25
 
26
- # Dimensions de la vidéo
27
- height, width = frames[0].size
28
- video_path = "/tmp/generated_video.mp4" # Enregistrement dans un dossier temporaire
29
 
30
- # Création du fichier vidéo
31
  video = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*"mp4v"), 24, (width, height))
32
 
33
  for frame in frames:
@@ -35,10 +37,10 @@ def generate_video_from_image(image, prompt, num_frames=10):
35
 
36
  video.release()
37
 
38
- # Retourner le fichier vidéo généré pour l'afficher dans Gradio
39
  return video_path
40
 
41
- # CSS personnalisé pour le titre
42
  css = """
43
  h1#title {
44
  font-size: 3em;
@@ -52,7 +54,7 @@ h1#title {
52
  }
53
  """
54
 
55
- # Interface Gradio avec champ d'upload d'image et titre stylisé
56
  iface = gr.Interface(
57
  fn=generate_video_from_image,
58
  inputs=[
@@ -60,9 +62,10 @@ iface = gr.Interface(
60
  gr.Textbox(label="Prompt", placeholder="Décris la scène ou l'effet désiré (ex: 'effet artistique, coucher de soleil')"),
61
  gr.Slider(1, 30, value=10, step=1, label="Nombre de Frames")
62
  ],
63
- outputs=gr.File(label="Vidéo générée"), # Afficher le fichier vidéo en sortie
64
  title="Stable-FX05",
65
  css=css
66
  )
67
 
68
  iface.launch()
 
 
1
+ from diffusers import StableDiffusionImg2ImgPipeline
2
  import torch
 
3
  import cv2
4
  import numpy as np
5
  import gradio as gr
6
  from PIL import Image
 
7
 
8
+ # Load model for GPU or CPU
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  model_id = "runwayml/stable-diffusion-v1-5"
11
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
12
+ model_id,
13
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
14
+ )
15
  pipe = pipe.to(device)
16
 
17
  def generate_video_from_image(image, prompt, num_frames=10):
18
  frames = []
19
+ image = image.resize((512, 512)) # Resize input image
20
 
21
+ # Generate frames
22
  for i in range(num_frames):
23
  generator = torch.Generator(device).manual_seed(42 + i)
24
  prompt_variation = f"{prompt}, variation {i}"
25
+ generated_image = pipe(prompt=prompt_variation, image=image, generator=generator).images[0]
26
  frames.append(generated_image)
27
 
28
+ # Video dimensions
29
+ width, height = frames[0].size
30
+ video_path = "generated_video.mp4" # Save in current working directory
31
 
32
+ # Create video file
33
  video = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*"mp4v"), 24, (width, height))
34
 
35
  for frame in frames:
 
37
 
38
  video.release()
39
 
40
+ # Return video file path
41
  return video_path
42
 
43
+ # Custom CSS for title
44
  css = """
45
  h1#title {
46
  font-size: 3em;
 
54
  }
55
  """
56
 
57
+ # Gradio Interface
58
  iface = gr.Interface(
59
  fn=generate_video_from_image,
60
  inputs=[
 
62
  gr.Textbox(label="Prompt", placeholder="Décris la scène ou l'effet désiré (ex: 'effet artistique, coucher de soleil')"),
63
  gr.Slider(1, 30, value=10, step=1, label="Nombre de Frames")
64
  ],
65
+ outputs=gr.File(label="Vidéo générée"),
66
  title="Stable-FX05",
67
  css=css
68
  )
69
 
70
  iface.launch()
71
+