Spaces:
Runtime error
Runtime error
Commit ·
1915b8d
1
Parent(s): baa52ab
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
|
| 11 |
-
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
-
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"
|
|
@@ -26,10 +22,14 @@ pipe.unet = torch.compile(pipe.unet)
|
|
| 26 |
|
| 27 |
# CLAP model (only required for automatic scoring)
|
| 28 |
clap_model = ClapModel.from_pretrained("sanchit-gandhi/clap-htsat-unfused-m-full").to(device)
|
| 29 |
-
|
| 30 |
|
| 31 |
generator = torch.Generator(device)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# Streamlit app setup
|
| 34 |
st.set_page_config(
|
| 35 |
page_title="Text to Media",
|
|
@@ -55,16 +55,20 @@ if uploaded_files:
|
|
| 55 |
f.write(uploaded_file.read())
|
| 56 |
image_paths.append(image_path)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
try:
|
| 60 |
image = Image.open(image_path).convert("RGB")
|
| 61 |
-
inputs =
|
| 62 |
-
out =
|
| 63 |
-
caption =
|
| 64 |
descriptions.append(caption)
|
| 65 |
except Exception as e:
|
| 66 |
descriptions.append("Erreur lors de la génération de la légende")
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Générez de la musique à partir des descriptions
|
| 69 |
st.header("Génération de Musique à partir des Descriptions")
|
| 70 |
|
|
@@ -79,7 +83,7 @@ if uploaded_files:
|
|
| 79 |
n_candidates = st.slider("Number waveforms to generate", 1, 3, 3, 1)
|
| 80 |
|
| 81 |
def score_waveforms(text, waveforms):
|
| 82 |
-
inputs =
|
| 83 |
inputs = {key: inputs[key].to(device) for key in inputs}
|
| 84 |
with torch.no_grad():
|
| 85 |
logits_per_text = clap_model(**inputs).logits_per_text # this is the audio-text similarity score
|
|
@@ -103,25 +107,31 @@ if uploaded_files:
|
|
| 103 |
else:
|
| 104 |
waveform = waveforms[0]
|
| 105 |
|
| 106 |
-
|
| 107 |
st.audio(waveform, format="audio/wav", sample_rate=16000)
|
| 108 |
-
|
| 109 |
# Créer une vidéo à partir des images
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
|
| 120 |
-
if st.button("Créer la vidéo"):
|
| 121 |
-
create_video(image_paths, "slideshow.mp4")
|
| 122 |
-
st.video("slideshow.mp4")
|
| 123 |
|
| 124 |
-
# Supprimer le répertoire temporaire
|
| 125 |
-
for image_path in image_paths:
|
| 126 |
-
os.remove(image_path)
|
| 127 |
-
os.rmdir(temp_dir)
|
|
|
|
| 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"
|
|
|
|
| 22 |
|
| 23 |
# CLAP model (only required for automatic scoring)
|
| 24 |
clap_model = ClapModel.from_pretrained("sanchit-gandhi/clap-htsat-unfused-m-full").to(device)
|
| 25 |
+
processor = AutoProcessor.from_pretrained("sanchit-gandhi/clap-htsat-unfused-m-full")
|
| 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",
|
|
|
|
| 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")
|
| 62 |
+
out = image_caption_model.generate(**inputs)
|
| 63 |
+
caption = image_caption_processor.decode(out[0], skip_special_tokens=True)
|
| 64 |
descriptions.append(caption)
|
| 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 |
# Générez de la musique à partir des descriptions
|
| 73 |
st.header("Génération de Musique à partir des Descriptions")
|
| 74 |
|
|
|
|
| 83 |
n_candidates = st.slider("Number waveforms to generate", 1, 3, 3, 1)
|
| 84 |
|
| 85 |
def score_waveforms(text, waveforms):
|
| 86 |
+
inputs = processor(text=text, audios=list(waveforms), return_tensors="pt", padding=True)
|
| 87 |
inputs = {key: inputs[key].to(device) for key in inputs}
|
| 88 |
with torch.no_grad():
|
| 89 |
logits_per_text = clap_model(**inputs).logits_per_text # this is the audio-text similarity score
|
|
|
|
| 107 |
else:
|
| 108 |
waveform = waveforms[0]
|
| 109 |
|
| 110 |
+
# Afficher le lecteur audio
|
| 111 |
st.audio(waveform, format="audio/wav", sample_rate=16000)
|
| 112 |
+
|
| 113 |
# Créer une vidéo à partir des images
|
| 114 |
+
if image_paths:
|
| 115 |
+
st.header("Génération du Diaporama Vidéo")
|
| 116 |
+
|
| 117 |
+
output_video_path = os.path.join(temp_dir, "slideshow.mp4")
|
| 118 |
+
|
| 119 |
+
# Débit d'images par seconde (calculé en fonction de la durée de chaque image)
|
| 120 |
+
frame_rate = 1 / image_duration
|
| 121 |
+
|
| 122 |
+
image_clips = [ImageSequenceClip([image_path], fps=frame_rate, durations=[image_duration]) for image_path in image_paths]
|
| 123 |
+
|
| 124 |
+
final_clip = concatenate_videoclips(image_clips, method="compose")
|
| 125 |
+
|
| 126 |
+
final_clip.write_videofile(output_video_path, codec='libx264', fps=frame_rate)
|
| 127 |
|
| 128 |
+
# Afficher la vidéo
|
| 129 |
+
st.video(open(output_video_path, 'rb').read())
|
| 130 |
|
| 131 |
+
# Supprimer le répertoire temporaire
|
| 132 |
+
for image_path in image_paths:
|
| 133 |
+
os.remove(image_path)
|
| 134 |
+
os.remove(output_video_path)
|
| 135 |
+
os.rmdir(temp_dir)
|
| 136 |
|
|
|
|
|
|
|
|
|
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|