Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,145 +1,106 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
import numpy as np
|
4 |
-
from
|
5 |
-
|
6 |
-
|
7 |
-
MusicgenForCausalLM,
|
8 |
-
set_seed,
|
9 |
-
)
|
10 |
import torch
|
11 |
-
import tempfile
|
12 |
import os
|
13 |
-
import ffmpeg
|
14 |
-
from PIL import Image
|
15 |
-
import warnings
|
16 |
-
import io
|
17 |
-
|
18 |
-
warnings.filterwarnings("ignore")
|
19 |
-
|
20 |
-
def analyze_video(video_path):
|
21 |
-
"""Analyze video with optimized BLIP processing"""
|
22 |
-
cap = cv2.VideoCapture(video_path)
|
23 |
-
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
24 |
-
sample_rate = max(frame_count // 30, 1) # Increased frame sampling
|
25 |
-
|
26 |
-
frames = []
|
27 |
-
try:
|
28 |
-
for i in range(0, frame_count, sample_rate):
|
29 |
-
cap.set(cv2.CAP_PROP_POS_FRAMES, i)
|
30 |
-
ret, frame = cap.read()
|
31 |
-
if ret:
|
32 |
-
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
33 |
-
small_frame = cv2.resize(frame_rgb, (128, 128)) # Reduced resolution
|
34 |
-
frames.append(Image.fromarray(small_frame))
|
35 |
-
finally:
|
36 |
-
cap.release()
|
37 |
-
|
38 |
-
if not frames:
|
39 |
-
return ""
|
40 |
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
""
|
48 |
-
|
49 |
-
text=[description],
|
50 |
-
padding=True,
|
51 |
-
return_tensors="pt",
|
52 |
-
).to(st.session_state.device)
|
53 |
-
|
54 |
-
set_seed(42) # For reproducibility
|
55 |
-
|
56 |
-
max_new_tokens = int(duration * 16) # 16 tokens per second
|
57 |
|
|
|
58 |
try:
|
59 |
-
|
60 |
-
|
61 |
-
do_sample=True,
|
62 |
-
top_k=250,
|
63 |
-
max_new_tokens=max_new_tokens,
|
64 |
-
)
|
65 |
-
if audio.shape[0] == 1 and len(audio.shape) == 3:
|
66 |
-
return audio[0][0].cpu().numpy()
|
67 |
-
else:
|
68 |
-
st.error(f"Unexpected audio shape: {audio.shape}")
|
69 |
-
return None
|
70 |
-
|
71 |
-
except torch.cuda.OutOfMemoryError:
|
72 |
-
st.error("Out of GPU memory. Try a shorter video or smaller model.")
|
73 |
-
return None
|
74 |
-
except Exception as e:
|
75 |
-
st.error(f"Error during audio generation: {e}")
|
76 |
-
return None
|
77 |
-
|
78 |
-
def process_video(uploaded_file):
|
79 |
-
"""Generates audio from video description"""
|
80 |
-
with tempfile.TemporaryDirectory() as tmp_dir:
|
81 |
-
video_path = os.path.join(tmp_dir, "input.mp4")
|
82 |
-
with open(video_path, "wb") as f:
|
83 |
f.write(uploaded_file.getbuffer())
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
if
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
#
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
st.download_button(
|
135 |
-
"Download
|
136 |
-
data=
|
137 |
-
file_name="
|
138 |
mime="audio/wav"
|
139 |
)
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import imageio
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import AutoProcessor, BlipForConditionalGeneration, MusicgenForConditionalGeneration
|
6 |
+
import soundfile as sf
|
|
|
|
|
|
|
7 |
import torch
|
|
|
8 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Set page title
|
11 |
+
st.title("Video Sound Effect Generator")
|
12 |
|
13 |
+
# File uploader for video
|
14 |
+
uploaded_file = st.file_uploader(
|
15 |
+
"Upload a short video (MP4, max 10 seconds, high resolution)",
|
16 |
+
type=["mp4"]
|
17 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
if uploaded_file is not None:
|
20 |
try:
|
21 |
+
# Save the uploaded video temporarily
|
22 |
+
with open("temp_video.mp4", "wb") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
f.write(uploaded_file.getbuffer())
|
24 |
+
|
25 |
+
# Check video duration
|
26 |
+
video = imageio.get_reader("temp_video.mp4")
|
27 |
+
fps = video.get_meta_data()['fps']
|
28 |
+
num_frames = len(list(video.iter_data()))
|
29 |
+
duration = num_frames / fps
|
30 |
+
|
31 |
+
if duration > 10:
|
32 |
+
st.error("Video is too long. Please upload a video of maximum 10 seconds.")
|
33 |
+
else:
|
34 |
+
st.success("Video uploaded successfully!")
|
35 |
+
|
36 |
+
# Extract 10 evenly spaced frames
|
37 |
+
num_frames_to_extract = 10
|
38 |
+
step = max(1, num_frames // num_frames_to_extract)
|
39 |
+
frames = [
|
40 |
+
Image.fromarray(video.get_data(i))
|
41 |
+
for i in range(0, num_frames, step)
|
42 |
+
][:num_frames_to_extract]
|
43 |
+
|
44 |
+
# Load BLIP model with caching
|
45 |
+
@st.cache_resource
|
46 |
+
def load_blip_model():
|
47 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
48 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
49 |
+
return processor, model
|
50 |
+
|
51 |
+
processor, model = load_blip_model()
|
52 |
+
|
53 |
+
# Generate text descriptions for each frame
|
54 |
+
descriptions = []
|
55 |
+
for frame in frames:
|
56 |
+
inputs = processor(images=frame, return_tensors="pt")
|
57 |
+
out = model.generate(**inputs)
|
58 |
+
description = processor.decode(out[0], skip_special_tokens=True)
|
59 |
+
descriptions.append(description)
|
60 |
+
|
61 |
+
# Combine descriptions into a single prompt
|
62 |
+
text_prompt = ". ".join(descriptions)
|
63 |
+
st.write("Generated text prompt:", text_prompt)
|
64 |
+
|
65 |
+
# Load MusicGen model with caching
|
66 |
+
@st.cache_resource
|
67 |
+
def load_musicgen_model():
|
68 |
+
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
|
69 |
+
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
|
70 |
+
return processor, model
|
71 |
+
|
72 |
+
musicgen_processor, musicgen_model = load_musicgen_model()
|
73 |
+
|
74 |
+
# Generate sound effect
|
75 |
+
inputs = musicgen_processor(
|
76 |
+
text=[text_prompt],
|
77 |
+
padding=True,
|
78 |
+
return_tensors="pt",
|
79 |
+
)
|
80 |
+
audio_values = musicgen_model.generate(**inputs, max_new_tokens=512)
|
81 |
+
audio_array = audio_values[0].numpy()
|
82 |
+
sample_rate = musicgen_model.config.audio_encoder.sampling_rate
|
83 |
+
|
84 |
+
# Save audio to a WAV file
|
85 |
+
sf.write("output.wav", audio_array, sample_rate)
|
86 |
+
|
87 |
+
# Provide audio playback and download options
|
88 |
+
st.audio("output.wav", format="audio/wav")
|
89 |
+
with open("output.wav", "rb") as audio_file:
|
90 |
st.download_button(
|
91 |
+
label="Download Sound Effect",
|
92 |
+
data=audio_file,
|
93 |
+
file_name="sound_effect.wav",
|
94 |
mime="audio/wav"
|
95 |
)
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
st.error(f"An error occurred: {str(e)}")
|
99 |
+
st.write("Please try uploading a different video or check your connection.")
|
100 |
+
|
101 |
+
finally:
|
102 |
+
# Clean up temporary files
|
103 |
+
if os.path.exists("temp_video.mp4"):
|
104 |
+
os.remove("temp_video.mp4")
|
105 |
+
if os.path.exists("output.wav"):
|
106 |
+
os.remove("output.wav")
|