spookyuser
commited on
Commit
·
c6b75bd
1
Parent(s):
89b6dcb
Add types
Browse files
app.py
CHANGED
|
@@ -6,9 +6,11 @@ import subprocess
|
|
| 6 |
import uuid
|
| 7 |
import shutil
|
| 8 |
|
|
|
|
| 9 |
output_dir = Path("temp/")
|
| 10 |
output_dir.mkdir(exist_ok=True, parents=True)
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
class SpotifyApi:
|
|
@@ -18,7 +20,7 @@ class SpotifyApi:
|
|
| 18 |
def __init__(self):
|
| 19 |
self.setup_spotipy
|
| 20 |
|
| 21 |
-
def setup_spotipy(self):
|
| 22 |
# Check if the credentials file exists
|
| 23 |
if not os.path.exists("spotif.rc"):
|
| 24 |
with open("spotify.rc", "w") as f:
|
|
@@ -29,24 +31,28 @@ class SpotifyApi:
|
|
| 29 |
else:
|
| 30 |
pass
|
| 31 |
|
| 32 |
-
def download_episode(self, episode_url):
|
| 33 |
# Generate a 8 character random string
|
| 34 |
foldername = str(uuid.uuid4())[:8]
|
| 35 |
out_path = (self.spotify_directory / foldername).resolve()
|
| 36 |
subprocess.call(["spodcast", "--root-path", out_path, episode_url])
|
| 37 |
self.foldername = foldername
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
def get_final_mp3(self):
|
| 41 |
# Look in all the subdirectories of spotify for the mp3 file
|
| 42 |
-
for root, dirs, files in os.walk(
|
|
|
|
|
|
|
| 43 |
for file in files:
|
| 44 |
if file.endswith(".mp3"):
|
| 45 |
-
final_mp3 = Path(
|
| 46 |
-
|
| 47 |
-
)
|
| 48 |
-
shutil.copy(os.path.join(root, file), final_mp3)
|
| 49 |
shutil.rmtree(Path(self.spotify_directory / self.foldername))
|
|
|
|
| 50 |
return final_mp3.as_posix()
|
| 51 |
|
| 52 |
|
|
@@ -59,31 +65,23 @@ def process_inputs(prompt, audio, spotify_url):
|
|
| 59 |
return video
|
| 60 |
|
| 61 |
|
| 62 |
-
def add_static_image_to_audio(image, audio):
|
| 63 |
"""Create and save a video file to `output_path` after
|
| 64 |
combining a static image that is located in `image_path`
|
| 65 |
with an audio file in `audio_path`"""
|
| 66 |
-
# create the audio clip object
|
| 67 |
audio_clip = AudioFileClip(audio)
|
| 68 |
-
# create the image clip object
|
| 69 |
image_clip = ImageClip(image)
|
| 70 |
-
# use set_audio method from image clip to combine the audio with the image
|
| 71 |
video_clip = image_clip.set_audio(audio_clip)
|
| 72 |
-
# specify the duration of the new clip to be the duration of the audio clip
|
| 73 |
video_clip.duration = audio_clip.duration
|
| 74 |
-
# set the FPS to 1
|
| 75 |
video_clip.fps = 1
|
| 76 |
-
|
| 77 |
-
path = "out.mp4"
|
| 78 |
video_clip.write_videofile(path)
|
| 79 |
-
return path
|
| 80 |
|
| 81 |
|
| 82 |
-
def get_stable_diffusion_image(prompt):
|
| 83 |
-
path = "temp/image_out.png"
|
| 84 |
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
|
| 85 |
gallery_dir = stable_diffusion(prompt, fn_index=2)
|
| 86 |
-
# Rename gallery dir to sdout
|
| 87 |
return [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)][0]
|
| 88 |
|
| 89 |
|
|
|
|
| 6 |
import uuid
|
| 7 |
import shutil
|
| 8 |
|
| 9 |
+
|
| 10 |
output_dir = Path("temp/")
|
| 11 |
output_dir.mkdir(exist_ok=True, parents=True)
|
| 12 |
+
|
| 13 |
+
os.chdir(output_dir) # change working directory to output_dir because the hf spaces model has no option to specify output directory ¯\_(ツ)_/¯
|
| 14 |
|
| 15 |
|
| 16 |
class SpotifyApi:
|
|
|
|
| 20 |
def __init__(self):
|
| 21 |
self.setup_spotipy
|
| 22 |
|
| 23 |
+
def setup_spotipy(self) -> None:
|
| 24 |
# Check if the credentials file exists
|
| 25 |
if not os.path.exists("spotif.rc"):
|
| 26 |
with open("spotify.rc", "w") as f:
|
|
|
|
| 31 |
else:
|
| 32 |
pass
|
| 33 |
|
| 34 |
+
def download_episode(self, episode_url) -> str:
|
| 35 |
# Generate a 8 character random string
|
| 36 |
foldername = str(uuid.uuid4())[:8]
|
| 37 |
out_path = (self.spotify_directory / foldername).resolve()
|
| 38 |
subprocess.call(["spodcast", "--root-path", out_path, episode_url])
|
| 39 |
self.foldername = foldername
|
| 40 |
+
mp3_path = self.get_final_mp3()
|
| 41 |
+
assert mp3_path is not None
|
| 42 |
+
return mp3_path
|
| 43 |
|
| 44 |
+
def get_final_mp3(self) -> str | None:
|
| 45 |
# Look in all the subdirectories of spotify for the mp3 file
|
| 46 |
+
for root, dirs, files in os.walk(
|
| 47 |
+
Path(self.spotify_directory / self.foldername)
|
| 48 |
+
):
|
| 49 |
for file in files:
|
| 50 |
if file.endswith(".mp3"):
|
| 51 |
+
final_mp3 = Path(
|
| 52 |
+
self.final_directory / self.foldername
|
| 53 |
+
).with_suffix(".mp3")
|
|
|
|
| 54 |
shutil.rmtree(Path(self.spotify_directory / self.foldername))
|
| 55 |
+
shutil.copy(os.path.join(root, file), final_mp3)
|
| 56 |
return final_mp3.as_posix()
|
| 57 |
|
| 58 |
|
|
|
|
| 65 |
return video
|
| 66 |
|
| 67 |
|
| 68 |
+
def add_static_image_to_audio(image, audio) -> str:
|
| 69 |
"""Create and save a video file to `output_path` after
|
| 70 |
combining a static image that is located in `image_path`
|
| 71 |
with an audio file in `audio_path`"""
|
|
|
|
| 72 |
audio_clip = AudioFileClip(audio)
|
|
|
|
| 73 |
image_clip = ImageClip(image)
|
|
|
|
| 74 |
video_clip = image_clip.set_audio(audio_clip)
|
|
|
|
| 75 |
video_clip.duration = audio_clip.duration
|
|
|
|
| 76 |
video_clip.fps = 1
|
| 77 |
+
path = Path(output_dir / "output.mp4")
|
|
|
|
| 78 |
video_clip.write_videofile(path)
|
| 79 |
+
return path.as_posix()
|
| 80 |
|
| 81 |
|
| 82 |
+
def get_stable_diffusion_image(prompt) -> str:
|
|
|
|
| 83 |
stable_diffusion = gr.Blocks.load(name="spaces/stabilityai/stable-diffusion")
|
| 84 |
gallery_dir = stable_diffusion(prompt, fn_index=2)
|
|
|
|
| 85 |
return [os.path.join(gallery_dir, img) for img in os.listdir(gallery_dir)][0]
|
| 86 |
|
| 87 |
|