Update app.py
Browse files
app.py
CHANGED
@@ -5,11 +5,27 @@ from pydub import AudioSegment
|
|
5 |
import tempfile
|
6 |
import os
|
7 |
import io
|
|
|
8 |
from transformers import pipeline
|
9 |
import matplotlib.pyplot as plt
|
10 |
import librosa
|
11 |
import numpy as np
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Function to convert video to audio
|
14 |
def video_to_audio(video_file):
|
15 |
video = mp.VideoFileClip(video_file)
|
@@ -83,58 +99,91 @@ def plot_waveform(audio_data, duration=10):
|
|
83 |
|
84 |
# Streamlit app layout
|
85 |
st.title("Video and Audio to Text Transcription with Emotion Detection and Visualization")
|
86 |
-
st.write("Upload a video or audio file
|
87 |
-
st.write("**Note:**
|
88 |
|
89 |
tab = st.selectbox("Select file type", ["Video", "Audio"])
|
90 |
|
91 |
if tab == "Video":
|
92 |
-
|
93 |
-
if
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
st.
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
if 'wav_audio_file' in st.session_state:
|
113 |
st.audio(st.session_state.wav_audio_file, format='audio/wav')
|
114 |
-
st.download_button("Download Transcription",
|
115 |
st.download_button("Download Audio", st.session_state.wav_audio_file, "converted_audio.wav", "audio/wav")
|
116 |
|
117 |
elif tab == "Audio":
|
118 |
-
|
119 |
-
if
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
st.
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
if 'wav_audio_file_audio' in st.session_state:
|
138 |
st.audio(st.session_state.wav_audio_file_audio, format='audio/wav')
|
139 |
-
st.download_button("Download Transcription",
|
140 |
st.download_button("Download Audio", st.session_state.wav_audio_file_audio, "converted_audio_audio.wav", "audio/wav")
|
|
|
5 |
import tempfile
|
6 |
import os
|
7 |
import io
|
8 |
+
import requests
|
9 |
from transformers import pipeline
|
10 |
import matplotlib.pyplot as plt
|
11 |
import librosa
|
12 |
import numpy as np
|
13 |
|
14 |
+
# Function to download file from URL
|
15 |
+
def download_file(url):
|
16 |
+
try:
|
17 |
+
extension = os.path.splitext(url)[1]
|
18 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=extension)
|
19 |
+
with requests.get(url, stream=True) as r:
|
20 |
+
r.raise_for_status()
|
21 |
+
for chunk in r.iter_content(chunk_size=8192):
|
22 |
+
temp_file.write(chunk)
|
23 |
+
temp_file.close()
|
24 |
+
return temp_file.name
|
25 |
+
except Exception as e:
|
26 |
+
st.error(f"Failed to download file: {e}")
|
27 |
+
return None
|
28 |
+
|
29 |
# Function to convert video to audio
|
30 |
def video_to_audio(video_file):
|
31 |
video = mp.VideoFileClip(video_file)
|
|
|
99 |
|
100 |
# Streamlit app layout
|
101 |
st.title("Video and Audio to Text Transcription with Emotion Detection and Visualization")
|
102 |
+
st.write("Upload a video or audio file, or provide a URL to a large file (up to 1GB).")
|
103 |
+
st.write("**Note:** Direct file uploads are limited to 200MB. For larger files, please provide a URL.")
|
104 |
|
105 |
tab = st.selectbox("Select file type", ["Video", "Audio"])
|
106 |
|
107 |
if tab == "Video":
|
108 |
+
method = st.radio("Choose how to provide the video file:", ["Upload file", "Provide URL"])
|
109 |
+
if method == "Upload file":
|
110 |
+
uploaded_file = st.file_uploader("Upload Video", type=["mp4", "mov", "avi"])
|
111 |
+
elif method == "Provide URL":
|
112 |
+
url = st.text_input("Enter video URL")
|
113 |
+
if st.button("Analyze Video"):
|
114 |
+
if method == "Upload file" and uploaded_file:
|
115 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
|
116 |
+
tmp_file.write(uploaded_file.read())
|
117 |
+
file_path = tmp_file.name
|
118 |
+
elif method == "Provide URL" and url:
|
119 |
+
with st.spinner("Downloading video... This may take a while for large files."):
|
120 |
+
file_path = download_file(url)
|
121 |
+
if file_path is None:
|
122 |
+
st.error("Failed to download the file. Please check the URL and try again.")
|
123 |
+
st.stop()
|
124 |
+
else:
|
125 |
+
st.error("Please provide a file or URL.")
|
126 |
+
st.stop()
|
127 |
+
# Process the video file
|
128 |
+
with st.spinner("Processing video..."):
|
129 |
+
audio_file = video_to_audio(file_path)
|
130 |
+
wav_audio_file = convert_mp3_to_wav(audio_file)
|
131 |
+
transcription = transcribe_audio(wav_audio_file)
|
132 |
+
st.text_area("Transcription", transcription, height=300)
|
133 |
+
emotions = detect_emotion(transcription)
|
134 |
+
st.write(f"Detected Emotions: {emotions}")
|
135 |
+
with open(wav_audio_file, "rb") as f:
|
136 |
+
audio_data = io.BytesIO(f.read())
|
137 |
+
st.session_state.wav_audio_file = audio_data
|
138 |
+
plot_waveform(st.session_state.wav_audio_file)
|
139 |
+
# Cleanup
|
140 |
+
os.remove(file_path)
|
141 |
+
os.remove(audio_file)
|
142 |
+
os.remove(wav_audio_file)
|
143 |
if 'wav_audio_file' in st.session_state:
|
144 |
st.audio(st.session_state.wav_audio_file, format='audio/wav')
|
145 |
+
st.download_button("Download Transcription", transcription, "transcription.txt", "text/plain")
|
146 |
st.download_button("Download Audio", st.session_state.wav_audio_file, "converted_audio.wav", "audio/wav")
|
147 |
|
148 |
elif tab == "Audio":
|
149 |
+
method = st.radio("Choose how to provide the audio file:", ["Upload file", "Provide URL"])
|
150 |
+
if method == "Upload file":
|
151 |
+
uploaded_file = st.file_uploader("Upload Audio", type=["wav", "mp3"])
|
152 |
+
elif method == "Provide URL":
|
153 |
+
url = st.text_input("Enter audio URL")
|
154 |
+
if st.button("Analyze Audio"):
|
155 |
+
if method == "Upload file" and uploaded_file:
|
156 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3' if uploaded_file.type == "audio/mpeg" else '.wav') as tmp_file:
|
157 |
+
tmp_file.write(uploaded_file.read())
|
158 |
+
file_path = tmp_file.name
|
159 |
+
elif method == "Provide URL" and url:
|
160 |
+
with st.spinner("Downloading audio... This may take a while for large files."):
|
161 |
+
file_path = download_file(url)
|
162 |
+
if file_path is None:
|
163 |
+
st.error("Failed to download the file. Please check the URL and try again.")
|
164 |
+
st.stop()
|
165 |
+
else:
|
166 |
+
st.error("Please provide a file or URL.")
|
167 |
+
st.stop()
|
168 |
+
# Process the audio file
|
169 |
+
with st.spinner("Processing audio..."):
|
170 |
+
if file_path.endswith('.mp3'):
|
171 |
+
wav_audio_file = convert_mp3_to_wav(file_path)
|
172 |
+
else:
|
173 |
+
wav_audio_file = file_path
|
174 |
+
transcription = transcribe_audio(wav_audio_file)
|
175 |
+
st.text_area("Transcription", transcription, height=300)
|
176 |
+
emotions = detect_emotion(transcription)
|
177 |
+
st.write(f"Detected Emotions: {emotions}")
|
178 |
+
with open(wav_audio_file, "rb") as f:
|
179 |
+
audio_data = io.BytesIO(f.read())
|
180 |
+
st.session_state.wav_audio_file_audio = audio_data
|
181 |
+
plot_waveform(st.session_state.wav_audio_file_audio)
|
182 |
+
# Cleanup
|
183 |
+
if file_path != wav_audio_file:
|
184 |
+
os.remove(file_path)
|
185 |
+
os.remove(wav_audio_file)
|
186 |
if 'wav_audio_file_audio' in st.session_state:
|
187 |
st.audio(st.session_state.wav_audio_file_audio, format='audio/wav')
|
188 |
+
st.download_button("Download Transcription", transcription, "transcription_audio.txt", "text/plain")
|
189 |
st.download_button("Download Audio", st.session_state.wav_audio_file_audio, "converted_audio_audio.wav", "audio/wav")
|