Spaces:
Runtime error
Runtime error
Commit
·
701788b
1
Parent(s):
9cbd0ff
Update app.py
Browse files
app.py
CHANGED
@@ -32,74 +32,76 @@ st.set_page_config(
|
|
32 |
page_icon="📷 🎵",
|
33 |
)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
st.image(image_path, caption=f"Description : {descriptions[i]}", use_column_width=True)
|
74 |
-
|
75 |
-
# Créer une vidéo à partir des images
|
76 |
-
if image_paths:
|
77 |
-
output_video_path = os.path.join(temp_dir, "slideshow.mp4")
|
78 |
-
|
79 |
-
# Débit d'images par seconde (calculé en fonction de la durée de chaque image)
|
80 |
-
frame_rate = 1 / image_duration
|
81 |
-
|
82 |
-
image_clips = [ImageSequenceClip([image_path], fps=frame_rate, durations=[image_duration]) for image_path in image_paths]
|
83 |
-
|
84 |
-
final_clip = concatenate_videoclips(image_clips, method="compose")
|
85 |
-
|
86 |
-
final_clip.write_videofile(output_video_path, codec='libx264', fps=frame_rate)
|
87 |
-
|
88 |
-
# Afficher la vidéo
|
89 |
-
st.video(open(output_video_path, 'rb').read())
|
90 |
-
|
91 |
-
# Supprimer le répertoire temporaire
|
92 |
-
for image_path in image_paths:
|
93 |
-
os.remove(image_path)
|
94 |
-
os.remove(output_video_path)
|
95 |
-
os.rmdir(temp_dir)
|
96 |
-
|
97 |
-
elif selected_option == "Générer de la musique":
|
98 |
-
st.title("Générateur de Musique à partir de Texte")
|
99 |
-
|
100 |
-
text_input = st.text_input("Input text", "A hammer is hitting a wooden surface")
|
101 |
-
negative_prompt = st.text_input("Negative prompt", "low quality, average quality")
|
102 |
-
|
103 |
-
st.markdown("### Configuration")
|
104 |
seed = st.number_input("Seed", value=45)
|
105 |
-
duration = st.slider("Duration (seconds)", 2.5, 10.0, 5.0, 2.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
page_icon="📷 🎵",
|
33 |
)
|
34 |
|
35 |
+
st.title("Générateur de Diaporama Vidéo et Musique")
|
36 |
+
|
37 |
+
# Sélectionnez les images
|
38 |
+
uploaded_files = st.file_uploader("Sélectionnez des images (PNG, JPG, JPEG)", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
39 |
+
|
40 |
+
if uploaded_files:
|
41 |
+
# Créez un répertoire temporaire pour stocker les images
|
42 |
+
temp_dir = tempfile.mkdtemp()
|
43 |
+
|
44 |
+
# Enregistrez les images téléchargées dans le répertoire temporaire
|
45 |
+
image_paths = []
|
46 |
+
descriptions = [] # Pour stocker les descriptions générées
|
47 |
+
|
48 |
+
for i, uploaded_file in enumerate(uploaded_files):
|
49 |
+
image_path = os.path.join(temp_dir, uploaded_file.name)
|
50 |
+
with open(image_path, 'wb') as f:
|
51 |
+
f.write(uploaded_file.read())
|
52 |
+
image_paths.append(image_path)
|
53 |
|
54 |
+
# Générez la légende pour chaque image
|
55 |
+
try:
|
56 |
+
image = Image.open(image_path).convert("RGB")
|
57 |
+
inputs = processor(image, return_tensors="pt")
|
58 |
+
out = model.generate(**inputs)
|
59 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
60 |
+
descriptions.append(caption)
|
61 |
+
except Exception as e:
|
62 |
+
descriptions.append("Erreur lors de la génération de la légende")
|
63 |
+
|
64 |
+
# Affichez les images avec leurs descriptions
|
65 |
+
for i, image_path in enumerate(image_paths):
|
66 |
+
st.image(image_path, caption=f"Description : {descriptions[i]}", use_column_width=True)
|
67 |
+
|
68 |
+
# Générez de la musique à partir des descriptions
|
69 |
+
st.header("Génération de Musique à partir des Descriptions")
|
70 |
+
|
71 |
+
# Utilisez les descriptions générées pour la musique
|
72 |
+
music_input = "\n".join(descriptions)
|
73 |
+
st.text_area("Descriptions pour la musique", music_input, height=200)
|
74 |
+
|
75 |
+
# Configuration de la musique
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
seed = st.number_input("Seed", value=45)
|
77 |
+
duration = st.slider("Duration (seconds)", 2.5, 10.0, 5.0, 2.5)
|
78 |
+
guidance_scale = st.slider("Guidance scale", 0.0, 4.0, 2.5, 0.5)
|
79 |
+
n_candidates = st.slider("Number waveforms to generate", 1, 3, 3, 1)
|
80 |
+
|
81 |
+
def score_waveforms(text, waveforms):
|
82 |
+
inputs = processor(text=text, audios=list(waveforms), return_tensors="pt", padding=True)
|
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
|
86 |
+
probs = logits_per_text.softmax(dim=-1) # we can take the softmax to get the label probabilities
|
87 |
+
most_probable = torch.argmax(probs) # and now select the most likely audio waveform
|
88 |
+
waveform = waveforms[most_probable]
|
89 |
+
return waveform
|
90 |
+
|
91 |
+
if st.button("Générer de la musique"):
|
92 |
+
waveforms = pipe(
|
93 |
+
music_input,
|
94 |
+
audio_length_in_s=duration,
|
95 |
+
guidance_scale=guidance_scale,
|
96 |
+
num_inference_steps=100,
|
97 |
+
num_waveforms_per_prompt=n_candidates if n_candidates else 1,
|
98 |
+
generator=generator.manual_seed(int(seed)),
|
99 |
+
)["audios"]
|
100 |
+
|
101 |
+
if waveforms.shape[0] > 1:
|
102 |
+
waveform = score_waveforms(music_input, waveforms)
|
103 |
+
else:
|
104 |
+
waveform = waveforms[0]
|
105 |
+
|
106 |
+
# Afficher le lecteur audio
|
107 |
+
st.audio(waveform, format="audio/wav", sample_rate=16000)
|