DanLeBossDeESGI commited on
Commit
5bb0a67
·
1 Parent(s): 00c92a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -42
app.py CHANGED
@@ -1,4 +1,8 @@
1
  import streamlit as st
 
 
 
 
2
  import torch
3
  from diffusers import AudioLDMPipeline
4
  from transformers import AutoProcessor, ClapModel
@@ -24,48 +28,78 @@ generator = torch.Generator(device)
24
 
25
  # Streamlit app setup
26
  st.set_page_config(
27
- page_title="Text to Music",
28
- page_icon="🎵",
29
  )
30
 
31
- text_input = st.text_input("Input text", "A hammer is hitting a wooden surface")
32
- negative_prompt = st.text_input("Negative prompt", "low quality, average quality")
33
-
34
- st.markdown("### Configuration")
35
- seed = st.number_input("Seed", value=45)
36
- duration = st.slider("Duration (seconds)", 2.5, 10.0, 5.0, 2.5)
37
- guidance_scale = st.slider("Guidance scale", 0.0, 4.0, 2.5, 0.5)
38
- n_candidates = st.slider("Number waveforms to generate", 1, 3, 3, 1)
39
-
40
- def score_waveforms(text, waveforms):
41
- inputs = processor(text=text, audios=list(waveforms), return_tensors="pt", padding=True)
42
- inputs = {key: inputs[key].to(device) for key in inputs}
43
- with torch.no_grad():
44
- logits_per_text = clap_model(**inputs).logits_per_text # this is the audio-text similarity score
45
- probs = logits_per_text.softmax(dim=-1) # we can take the softmax to get the label probabilities
46
- most_probable = torch.argmax(probs) # and now select the most likely audio waveform
47
- waveform = waveforms[most_probable]
48
- return waveform
49
-
50
- if st.button("Submit"):
51
- if text_input is None:
52
- st.error("Please provide a text input.")
53
- else:
54
- waveforms = pipe(
55
- text_input,
56
- audio_length_in_s=duration,
57
- guidance_scale=guidance_scale,
58
- num_inference_steps=100,
59
- negative_prompt=negative_prompt,
60
- num_waveforms_per_prompt=n_candidates if n_candidates else 1,
61
- generator=generator.manual_seed(int(seed)),
62
- )["audios"]
63
-
64
- if waveforms.shape[0] > 1:
65
- waveform = score_waveforms(text_input, waveforms)
66
- else:
67
- waveform = waveforms[0]
68
-
69
- # Spécifiez le taux d'échantillonnage (sample_rate) et le format audio
70
- st.audio(waveform, format="audio/wav", sample_rate=16000)
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ import tempfile
4
+ from moviepy.editor import ImageSequenceClip, concatenate_videoclips
5
+ from PIL import Image
6
  import torch
7
  from diffusers import AudioLDMPipeline
8
  from transformers import AutoProcessor, ClapModel
 
28
 
29
  # Streamlit app setup
30
  st.set_page_config(
31
+ page_title="Text to Media",
32
+ page_icon="📷 🎵",
33
  )
34
 
35
+ # Créer des onglets pour choisir l'option
36
+ selected_option = st.selectbox("Sélectionnez l'option", ("Générer un diaporama vidéo", "Générer de la musique"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ if selected_option == "Générer un diaporama vidéo":
39
+ st.title("Diaporama Vidéo à partir d'Images avec Descriptions")
40
+
41
+ # Sélection de plusieurs fichiers image
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
+ # Créer un répertoire temporaire pour stocker les images
49
+ temp_dir = tempfile.mkdtemp()
50
+
51
+ # Enregistrez les images téléchargées dans le répertoire temporaire
52
+ image_paths = []
53
+ descriptions = [] # Pour stocker les descriptions générées
54
+
55
+ for i, uploaded_file in enumerate(uploaded_files):
56
+ image_path = os.path.join(temp_dir, uploaded_file.name)
57
+ with open(image_path, 'wb') as f:
58
+ f.write(uploaded_file.read())
59
+ image_paths.append(image_path)
60
+
61
+ # Générer la légende pour chaque image
62
+ try:
63
+ image = Image.open(image_path).convert("RGB")
64
+ inputs = processor(image, return_tensors="pt")
65
+ out = model.generate(**inputs)
66
+ caption = processor.decode(out[0], skip_special_tokens=True)
67
+ descriptions.append(caption)
68
+ except Exception as e:
69
+ descriptions.append("Erreur lors de la génération de la légende")
70
+
71
+ # Afficher les images avec leurs descriptions
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
+ # 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