Spaces:
Runtime error
Runtime error
Commit
·
97cac2d
1
Parent(s):
07eb64a
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,10 +7,6 @@ import torch
|
|
| 7 |
from diffusers import AudioLDMPipeline
|
| 8 |
from transformers import AutoProcessor, ClapModel, BlipProcessor, BlipForConditionalGeneration
|
| 9 |
|
| 10 |
-
# Charger le modèle et le processeur Blip pour la description d'images
|
| 11 |
-
image_caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
-
image_caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 13 |
-
|
| 14 |
# make Space compatible with CPU duplicates
|
| 15 |
if torch.cuda.is_available():
|
| 16 |
device = "cuda"
|
|
@@ -30,6 +26,10 @@ processor = AutoProcessor.from_pretrained("sanchit-gandhi/clap-htsat-unfused-m-f
|
|
| 30 |
|
| 31 |
generator = torch.Generator(device)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Streamlit app setup
|
| 34 |
st.set_page_config(
|
| 35 |
page_title="Text to Media",
|
|
@@ -41,11 +41,8 @@ st.title("Générateur de Diaporama Vidéo et Musique")
|
|
| 41 |
# Sélectionnez les images
|
| 42 |
uploaded_files = st.file_uploader("Sélectionnez des images (PNG, JPG, JPEG)", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 43 |
|
| 44 |
-
# Sélection de la durée d'affichage de chaque image avec une barre horizontale (en secondes)
|
| 45 |
-
image_duration = st.slider("Sélectionnez la durée d'affichage de chaque image (en secondes)", 1, 10, 4)
|
| 46 |
-
|
| 47 |
if uploaded_files:
|
| 48 |
-
#
|
| 49 |
temp_dir = tempfile.mkdtemp()
|
| 50 |
|
| 51 |
# Enregistrez les images téléchargées dans le répertoire temporaire
|
|
@@ -58,7 +55,7 @@ if uploaded_files:
|
|
| 58 |
f.write(uploaded_file.read())
|
| 59 |
image_paths.append(image_path)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
try:
|
| 63 |
image = Image.open(image_path).convert("RGB")
|
| 64 |
inputs = image_caption_processor(image, return_tensors="pt")
|
|
@@ -68,31 +65,28 @@ if uploaded_files:
|
|
| 68 |
except Exception as e:
|
| 69 |
descriptions.append("Erreur lors de la génération de la légende")
|
| 70 |
|
| 71 |
-
#
|
| 72 |
for i, image_path in enumerate(image_paths):
|
| 73 |
st.image(image_path, caption=f"Description : {descriptions[i]}", use_column_width=True)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
output_video_path = os.path.join(temp_dir, "slideshow.mp4")
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
os.remove(image_path)
|
| 94 |
-
os.remove(output_video_path)
|
| 95 |
-
os.rmdir(temp_dir)
|
| 96 |
|
| 97 |
# Générez de la musique à partir des descriptions
|
| 98 |
st.header("Génération de Musique à partir des Descriptions")
|
|
@@ -103,24 +97,24 @@ if uploaded_files:
|
|
| 103 |
|
| 104 |
# Configuration de la musique
|
| 105 |
seed = st.number_input("Seed", value=45)
|
| 106 |
-
|
| 107 |
guidance_scale = st.slider("Guidance scale", 0.0, 4.0, 2.5, 0.5)
|
| 108 |
-
n_candidates = st.slider("
|
| 109 |
|
| 110 |
def score_waveforms(text, waveforms):
|
| 111 |
inputs = processor(text=text, audios=list(waveforms), return_tensors="pt", padding=True)
|
| 112 |
inputs = {key: inputs[key].to(device) for key in inputs}
|
| 113 |
with torch.no_grad():
|
| 114 |
-
logits_per_text = clap_model(**inputs).logits_per_text
|
| 115 |
-
probs = logits_per_text.softmax(dim=-1)
|
| 116 |
-
most_probable = torch.argmax(probs)
|
| 117 |
waveform = waveforms[most_probable]
|
| 118 |
return waveform
|
| 119 |
|
| 120 |
if st.button("Générer de la musique"):
|
| 121 |
waveforms = pipe(
|
| 122 |
music_input,
|
| 123 |
-
audio_length_in_s=
|
| 124 |
guidance_scale=guidance_scale,
|
| 125 |
num_inference_steps=100,
|
| 126 |
num_waveforms_per_prompt=n_candidates if n_candidates else 1,
|
|
@@ -132,17 +126,5 @@ if uploaded_files:
|
|
| 132 |
else:
|
| 133 |
waveform = waveforms[0]
|
| 134 |
|
| 135 |
-
# Sauvegardez la musique générée dans un fichier temporaire
|
| 136 |
-
music_temp_path = os.path.join(temp_dir, "generated_music.wav")
|
| 137 |
-
waveform.save(music_temp_path)
|
| 138 |
-
|
| 139 |
# Afficher le lecteur audio
|
| 140 |
-
st.audio(
|
| 141 |
-
|
| 142 |
-
# Supprimer le répertoire temporaire
|
| 143 |
-
for image_path in image_paths:
|
| 144 |
-
os.remove(image_path)
|
| 145 |
-
os.remove(output_video_path)
|
| 146 |
-
os.remove(music_temp_path)
|
| 147 |
-
os.rmdir(temp_dir)
|
| 148 |
-
|
|
|
|
| 7 |
from diffusers import AudioLDMPipeline
|
| 8 |
from transformers import AutoProcessor, ClapModel, BlipProcessor, BlipForConditionalGeneration
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# make Space compatible with CPU duplicates
|
| 11 |
if torch.cuda.is_available():
|
| 12 |
device = "cuda"
|
|
|
|
| 26 |
|
| 27 |
generator = torch.Generator(device)
|
| 28 |
|
| 29 |
+
# Charger le modèle et le processeur Blip pour la description d'images
|
| 30 |
+
image_caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 31 |
+
image_caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 32 |
+
|
| 33 |
# Streamlit app setup
|
| 34 |
st.set_page_config(
|
| 35 |
page_title="Text to Media",
|
|
|
|
| 41 |
# Sélectionnez les images
|
| 42 |
uploaded_files = st.file_uploader("Sélectionnez des images (PNG, JPG, JPEG)", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
if uploaded_files:
|
| 45 |
+
# Créez un répertoire temporaire pour stocker les images
|
| 46 |
temp_dir = tempfile.mkdtemp()
|
| 47 |
|
| 48 |
# Enregistrez les images téléchargées dans le répertoire temporaire
|
|
|
|
| 55 |
f.write(uploaded_file.read())
|
| 56 |
image_paths.append(image_path)
|
| 57 |
|
| 58 |
+
# Générez la légende pour chaque image
|
| 59 |
try:
|
| 60 |
image = Image.open(image_path).convert("RGB")
|
| 61 |
inputs = image_caption_processor(image, return_tensors="pt")
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
descriptions.append("Erreur lors de la génération de la légende")
|
| 67 |
|
| 68 |
+
# Affichez les images avec leurs descriptions
|
| 69 |
for i, image_path in enumerate(image_paths):
|
| 70 |
st.image(image_path, caption=f"Description : {descriptions[i]}", use_column_width=True)
|
| 71 |
|
| 72 |
+
# Créez une vidéo à partir des images
|
| 73 |
+
st.header("Création d'une Diapositive Vidéo")
|
|
|
|
| 74 |
|
| 75 |
+
# Sélectionnez la durée d'affichage de chaque image avec une barre horizontale (en secondes)
|
| 76 |
+
image_duration = st.slider("Sélectionnez la durée d'affichage de chaque image (en secondes)", 1, 10, 4)
|
| 77 |
|
| 78 |
+
# Débit d'images par seconde (calculé en fonction de la durée de chaque image)
|
| 79 |
+
frame_rate = 1 / image_duration
|
| 80 |
|
| 81 |
+
image_clips = [ImageSequenceClip([image_path], fps=frame_rate, durations=[image_duration]) for image_path in image_paths]
|
| 82 |
|
| 83 |
+
final_clip = concatenate_videoclips(image_clips, method="compose")
|
| 84 |
|
| 85 |
+
final_clip_path = os.path.join(temp_dir, "slideshow.mp4")
|
| 86 |
+
final_clip.write_videofile(final_clip_path, codec='libx264', fps=frame_rate)
|
| 87 |
|
| 88 |
+
# Afficher la vidéo
|
| 89 |
+
st.video(open(final_clip_path, 'rb').read())
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Générez de la musique à partir des descriptions
|
| 92 |
st.header("Génération de Musique à partir des Descriptions")
|
|
|
|
| 97 |
|
| 98 |
# Configuration de la musique
|
| 99 |
seed = st.number_input("Seed", value=45)
|
| 100 |
+
duration = st.slider("Duration (seconds)", 2.5, 10.0, 5.0, 2.5)
|
| 101 |
guidance_scale = st.slider("Guidance scale", 0.0, 4.0, 2.5, 0.5)
|
| 102 |
+
n_candidates = st.slider("Number waveforms to generate", 1, 3, 3, 1)
|
| 103 |
|
| 104 |
def score_waveforms(text, waveforms):
|
| 105 |
inputs = processor(text=text, audios=list(waveforms), return_tensors="pt", padding=True)
|
| 106 |
inputs = {key: inputs[key].to(device) for key in inputs}
|
| 107 |
with torch.no_grad():
|
| 108 |
+
logits_per_text = clap_model(**inputs).logits_per_text # this is the audio-text similarity score
|
| 109 |
+
probs = logits_per_text.softmax(dim=-1) # we can take the softmax to get the label probabilities
|
| 110 |
+
most_probable = torch.argmax(probs) # and now select the most likely audio waveform
|
| 111 |
waveform = waveforms[most_probable]
|
| 112 |
return waveform
|
| 113 |
|
| 114 |
if st.button("Générer de la musique"):
|
| 115 |
waveforms = pipe(
|
| 116 |
music_input,
|
| 117 |
+
audio_length_in_s=duration,
|
| 118 |
guidance_scale=guidance_scale,
|
| 119 |
num_inference_steps=100,
|
| 120 |
num_waveforms_per_prompt=n_candidates if n_candidates else 1,
|
|
|
|
| 126 |
else:
|
| 127 |
waveform = waveforms[0]
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
# Afficher le lecteur audio
|
| 130 |
+
st.audio(waveform, format="audio/wav", sample_rate=16000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|