spookyuser commited on
Commit
98acae8
1 Parent(s): 0ebc3c4

Switch to inference api

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. app.py +21 -15
.gitignore CHANGED
@@ -165,3 +165,5 @@ cython_debug/
165
  # and can be added to the global gitignore or merged into this file. For a more nuclear
166
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
167
  #.idea/
 
 
 
165
  # and can be added to the global gitignore or merged into this file. For a more nuclear
166
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
167
  #.idea/
168
+
169
+ *.DS_STORE
app.py CHANGED
@@ -4,6 +4,13 @@ import os
4
  from torch import autocast
5
  from diffusers import StableDiffusionPipeline
6
  from moviepy.editor import AudioFileClip, ImageClip
 
 
 
 
 
 
 
7
 
8
  def process_inputs(prompt, audio):
9
  image = get_stable_diffusion_image(prompt)
@@ -12,8 +19,8 @@ def process_inputs(prompt, audio):
12
 
13
 
14
  def add_static_image_to_audio(image, audio):
15
- """Create and save a video file to `output_path` after
16
- combining a static image that is located in `image_path`
17
  with an audio file in `audio_path`"""
18
  # create the audio clip object
19
  audio_clip = AudioFileClip(audio)
@@ -26,21 +33,20 @@ def add_static_image_to_audio(image, audio):
26
  # set the FPS to 1
27
  video_clip.fps = 1
28
  # write the resuling video clip
29
- path = "temp/video_out.mp4"
30
  video_clip.write_videofile(path)
31
  return path
32
 
 
33
  def get_stable_diffusion_image(prompt):
34
- model_id = "CompVis/stable-diffusion-v1-4"
35
- device = "cuda"
36
- pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=os.getenv("HF_TOKEN") if os.getenv("HF_TOKEN") else True)
37
- pipe = pipe.to(device)
38
- with autocast("cuda"):
39
- image = pipe(prompt, guidance_scale=7.5)["sample"][0]
40
- path = "temp/out.jpg"
41
- image.save(path)
42
- return path
43
-
44
-
45
- iface = gr.Interface(fn=process_inputs, inputs=["text", gr.Audio(type="filepath")], outputs="video")
46
  iface.launch()
 
4
  from torch import autocast
5
  from diffusers import StableDiffusionPipeline
6
  from moviepy.editor import AudioFileClip, ImageClip
7
+ from pathlib import Path
8
+
9
+
10
+ output_dir = Path("temp/")
11
+ output_dir.mkdir(exist_ok=True, parents=True)
12
+ os.chdir(output_dir)
13
+
14
 
15
  def process_inputs(prompt, audio):
16
  image = get_stable_diffusion_image(prompt)
 
19
 
20
 
21
  def add_static_image_to_audio(image, audio):
22
+ """Create and save a video file to `output_path` after
23
+ combining a static image that is located in `image_path`
24
  with an audio file in `audio_path`"""
25
  # create the audio clip object
26
  audio_clip = AudioFileClip(audio)
 
33
  # set the FPS to 1
34
  video_clip.fps = 1
35
  # write the resuling video clip
36
+ path = "out.mp4"
37
  video_clip.write_videofile(path)
38
  return path
39
 
40
+
41
  def get_stable_diffusion_image(prompt):
42
+ path = "temp/image_out.png"
43
+ stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
44
+ gallery_dir = stable_diffusion(prompt, fn_index=2)
45
+ # Rename gallery dir to sdout
46
+ return [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)][0]
47
+
48
+
49
+ iface = gr.Interface(
50
+ fn=process_inputs, inputs=["text", gr.Audio(type="filepath")], outputs="video"
51
+ )
 
 
52
  iface.launch()