Spaces:
Sleeping
Sleeping
jukebox gradio
Browse files- app.py +103 -0
- audio/elephant.mp4 +0 -0
- audio/rabbit.mp4 +0 -0
- audio/snail.mp4 +0 -0
- images/elephant.png +0 -0
- images/pause.png +0 -0
- images/rabbit.png +0 -0
- images/snail.png +0 -0
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from interactive_pipe import Control
|
| 2 |
+
from interactive_pipe.headless.pipeline import HeadlessPipeline
|
| 3 |
+
from interactive_pipe.graphical.gradio_gui import InteractivePipeGradio
|
| 4 |
+
from interactive_pipe import interactive
|
| 5 |
+
from interactive_pipe.data_objects.image import Image
|
| 6 |
+
from interactive_pipe.thirdparty.images_openai_api import ImageFromPrompt
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import cv2
|
| 9 |
+
|
| 10 |
+
root = Path(__file__).parent
|
| 11 |
+
img_folder = root/"images"
|
| 12 |
+
audio_folder = root/"audio"
|
| 13 |
+
TRACK = "track"
|
| 14 |
+
IMAGE = ICON = "image"
|
| 15 |
+
CAPTION = "caption"
|
| 16 |
+
PROMPT = "prompt"
|
| 17 |
+
PROMPT_STYLE = "a cute crayon drawing of "
|
| 18 |
+
PROMPT_EXTRA = " on a white background, using a style from a cartoon book for baby kids"
|
| 19 |
+
|
| 20 |
+
# Audio generated by https://huggingface.co/spaces/facebook/MusicGen
|
| 21 |
+
# Elephant : a short savanah jungle music like the lion's king akuna matata with elephants barking
|
| 22 |
+
# Snail: a short instrumental country song with banjo without lyrics
|
| 23 |
+
# Rabbit: we will rock you by queens but played with ukulele without lyrics
|
| 24 |
+
TRACK_DICT = {
|
| 25 |
+
"elephant": {
|
| 26 |
+
TRACK: audio_folder/"elephant.mp4",
|
| 27 |
+
CAPTION: "ELEPHANT",
|
| 28 |
+
PROMPT: "a smiling elephant walking in the sunny yellow savana",
|
| 29 |
+
},
|
| 30 |
+
"snail": {
|
| 31 |
+
TRACK: audio_folder/"snail.mp4",
|
| 32 |
+
CAPTION: "SNAIL",
|
| 33 |
+
PROMPT: "a cute yellow and orange snail with two eyes slowly walking on the green grass"
|
| 34 |
+
},
|
| 35 |
+
"rabbit": {
|
| 36 |
+
TRACK: audio_folder/"rabbit.mp4",
|
| 37 |
+
CAPTION: "RABBIT",
|
| 38 |
+
PROMPT: "a smiling funny little rabbit in the green grass",
|
| 39 |
+
},
|
| 40 |
+
"pause": {
|
| 41 |
+
TRACK: None,
|
| 42 |
+
PROMPT: "a cute sleeping cat",
|
| 43 |
+
CAPTION: "...zzzz"
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
for item_name, element in TRACK_DICT.items():
|
| 48 |
+
TRACK_DICT[item_name][IMAGE] = ImageFromPrompt.generate_image(
|
| 49 |
+
PROMPT_STYLE + element[PROMPT] + PROMPT_EXTRA,
|
| 50 |
+
img_folder/(item_name+".png"),
|
| 51 |
+
size=(512, 512)
|
| 52 |
+
)
|
| 53 |
+
ICONS = [it[ICON] for key, it in TRACK_DICT.items()]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@interactive(
|
| 57 |
+
song=Control("elephant", list(TRACK_DICT.keys()), icons=ICONS)
|
| 58 |
+
)
|
| 59 |
+
def song_choice(global_params={}, song="elephant"):
|
| 60 |
+
global_params[TRACK] = song
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def play_song(global_params={}):
|
| 64 |
+
song = global_params.get(TRACK, None)
|
| 65 |
+
first_exec = global_params.get("first_exec", True)
|
| 66 |
+
if not first_exec:
|
| 67 |
+
audio_track = TRACK_DICT[song][TRACK]
|
| 68 |
+
if audio_track is None:
|
| 69 |
+
global_params["__stop"]()
|
| 70 |
+
else:
|
| 71 |
+
global_params["__set_audio"](audio_track)
|
| 72 |
+
global_params["__play"]()
|
| 73 |
+
else:
|
| 74 |
+
global_params["first_exec"] = False
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def image_choice(global_params={}):
|
| 78 |
+
song = global_params.get(TRACK, list(TRACK_DICT.keys())[0])
|
| 79 |
+
img = Image.from_file(TRACK_DICT[song][IMAGE]).data
|
| 80 |
+
max_height = 300 # Raspberry pi with a 7" touchscreen
|
| 81 |
+
h, w, _c = img.shape
|
| 82 |
+
if h > max_height:
|
| 83 |
+
img = cv2.resize(img, (w*max_height//h, max_height))
|
| 84 |
+
h, w, _c = img.shape
|
| 85 |
+
caption = TRACK_DICT[song][CAPTION]
|
| 86 |
+
global_params["__output_styles"]["img_out"] = {
|
| 87 |
+
"title": caption
|
| 88 |
+
} # discard auto titling
|
| 89 |
+
return img
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def sample_pipeline():
|
| 93 |
+
song_choice()
|
| 94 |
+
play_song()
|
| 95 |
+
img_out = image_choice()
|
| 96 |
+
return [img_out,]
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
if __name__ == '__main__':
|
| 100 |
+
pip = HeadlessPipeline.from_function(sample_pipeline, cache=False)
|
| 101 |
+
app = InteractivePipeGradio(
|
| 102 |
+
pipeline=pip, name="music", size=None, audio=True)
|
| 103 |
+
app()
|
audio/elephant.mp4
ADDED
|
Binary file (190 kB). View file
|
|
|
audio/rabbit.mp4
ADDED
|
Binary file (188 kB). View file
|
|
|
audio/snail.mp4
ADDED
|
Binary file (187 kB). View file
|
|
|
images/elephant.png
ADDED
|
images/pause.png
ADDED
|
images/rabbit.png
ADDED
|
images/snail.png
ADDED
|