shukdevdatta123 commited on
Commit
0064167
·
verified ·
1 Parent(s): c4a62d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -43
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 to transcribe it, detect emotions, and visualize the audio waveform.")
87
- st.write("**Note:** To upload files up to 1GB, run the app with: `streamlit run app.py --server.maxUploadSize=1024`")
88
 
89
  tab = st.selectbox("Select file type", ["Video", "Audio"])
90
 
91
  if tab == "Video":
92
- uploaded_video = st.file_uploader("Upload Video", type=["mp4", "mov", "avi"])
93
- if uploaded_video:
94
- with tempfile.NamedTemporaryFile(delete=False) as tmp_video:
95
- tmp_video.write(uploaded_video.read())
96
- tmp_video_path = tmp_video.name
97
- if st.button("Analyze Video"):
98
- with st.spinner("Processing video..."):
99
- audio_file = video_to_audio(tmp_video_path)
100
- wav_audio_file = convert_mp3_to_wav(audio_file)
101
- transcription = transcribe_audio(wav_audio_file)
102
- st.text_area("Transcription", transcription, height=300)
103
- emotions = detect_emotion(transcription)
104
- st.write(f"Detected Emotions: {emotions}")
105
- with open(wav_audio_file, "rb") as f:
106
- audio_data = io.BytesIO(f.read())
107
- st.session_state.wav_audio_file = audio_data
108
- plot_waveform(st.session_state.wav_audio_file)
109
- os.remove(tmp_video_path)
110
- os.remove(audio_file)
111
- os.remove(wav_audio_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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", st.session_state.transcription, "transcription.txt", "text/plain")
115
  st.download_button("Download Audio", st.session_state.wav_audio_file, "converted_audio.wav", "audio/wav")
116
 
117
  elif tab == "Audio":
118
- uploaded_audio = st.file_uploader("Upload Audio", type=["wav", "mp3"])
119
- if uploaded_audio:
120
- with tempfile.NamedTemporaryFile(delete=False) as tmp_audio:
121
- tmp_audio.write(uploaded_audio.read())
122
- tmp_audio_path = tmp_audio.name
123
- if st.button("Analyze Audio"):
124
- with st.spinner("Processing audio..."):
125
- wav_audio_file = convert_mp3_to_wav(tmp_audio_path) if uploaded_audio.type == "audio/mpeg" else tmp_audio_path
126
- transcription = transcribe_audio(wav_audio_file)
127
- st.text_area("Transcription", transcription, height=300)
128
- emotions = detect_emotion(transcription)
129
- st.write(f"Detected Emotions: {emotions}")
130
- with open(wav_audio_file, "rb") as f:
131
- audio_data = io.BytesIO(f.read())
132
- st.session_state.wav_audio_file_audio = audio_data
133
- plot_waveform(st.session_state.wav_audio_file_audio)
134
- if uploaded_audio.type == "audio/mpeg":
135
- os.remove(wav_audio_file)
136
- os.remove(tmp_audio_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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", st.session_state.transcription_audio, "transcription_audio.txt", "text/plain")
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")