bluenevus commited on
Commit
836768f
·
verified ·
1 Parent(s): f01f2d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -20,10 +20,6 @@ import base64
20
  import threading
21
  from pytube import YouTube
22
 
23
- # Initialize the speaker diarization pipeline
24
- pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization", use_auth_token="YOUR_HF_AUTH_TOKEN")
25
- print("Speaker diarization pipeline initialized successfully")
26
-
27
  # Check if CUDA is available and set the device
28
  device = "cuda" if torch.cuda.is_available() else "cpu"
29
  print(f"Using device: {device}")
@@ -89,7 +85,7 @@ def format_transcript_with_speakers(transcript, diarization):
89
  formatted_transcript.append(f"{segment_text}\n")
90
  return "".join(formatted_transcript)
91
 
92
- def transcribe_audio(audio_file):
93
  try:
94
  print("Loading audio file...")
95
  audio_input, sr = librosa.load(audio_file, sr=16000)
@@ -125,7 +121,7 @@ def transcribe_audio(audio_file):
125
  print(f"Error in transcribe_audio: {str(e)}")
126
  raise
127
 
128
- def transcribe_video(url):
129
  try:
130
  print(f"Attempting to download audio from URL: {url}")
131
  audio_bytes = download_audio_from_url(url)
@@ -142,7 +138,7 @@ def transcribe_video(url):
142
  temp_audio_path = temp_audio.name
143
 
144
  print("Starting audio transcription...")
145
- transcript = transcribe_audio(temp_audio_path)
146
  print(f"Transcription completed. Transcript length: {len(transcript)} characters")
147
 
148
  # Clean up the temporary file
@@ -165,6 +161,7 @@ app.layout = dbc.Container([
165
  html.H1("Video Transcription", className="text-center mb-4"),
166
  dbc.Card([
167
  dbc.CardBody([
 
168
  dbc.Input(id="video-url", type="text", placeholder="Enter video URL"),
169
  dbc.Button("Transcribe", id="transcribe-button", color="primary", className="mt-3"),
170
  dbc.Spinner(html.Div(id="transcription-output", className="mt-3")),
@@ -179,16 +176,24 @@ app.layout = dbc.Container([
179
  Output("transcription-output", "children"),
180
  Output("download-transcript", "data"),
181
  Input("transcribe-button", "n_clicks"),
 
182
  State("video-url", "value"),
183
  prevent_initial_call=True
184
  )
185
- def update_transcription(n_clicks, url):
186
- if not url:
187
  raise PreventUpdate
188
 
189
  def transcribe():
190
- transcript = transcribe_video(url)
191
- return transcript
 
 
 
 
 
 
 
192
 
193
  # Run transcription in a separate thread
194
  thread = threading.Thread(target=transcribe)
@@ -197,7 +202,7 @@ def update_transcription(n_clicks, url):
197
 
198
  transcript = transcribe()
199
 
200
- if transcript:
201
  download_data = dict(content=transcript, filename="transcript.txt")
202
  return dbc.Card([
203
  dbc.CardBody([
@@ -207,7 +212,7 @@ def update_transcription(n_clicks, url):
207
  ])
208
  ]), download_data
209
  else:
210
- return "Failed to transcribe video.", None
211
 
212
  if __name__ == '__main__':
213
  app.run(debug=True, host='0.0.0.0', port=7860)
 
20
  import threading
21
  from pytube import YouTube
22
 
 
 
 
 
23
  # Check if CUDA is available and set the device
24
  device = "cuda" if torch.cuda.is_available() else "cpu"
25
  print(f"Using device: {device}")
 
85
  formatted_transcript.append(f"{segment_text}\n")
86
  return "".join(formatted_transcript)
87
 
88
+ def transcribe_audio(audio_file, pipeline):
89
  try:
90
  print("Loading audio file...")
91
  audio_input, sr = librosa.load(audio_file, sr=16000)
 
121
  print(f"Error in transcribe_audio: {str(e)}")
122
  raise
123
 
124
+ def transcribe_video(url, pipeline):
125
  try:
126
  print(f"Attempting to download audio from URL: {url}")
127
  audio_bytes = download_audio_from_url(url)
 
138
  temp_audio_path = temp_audio.name
139
 
140
  print("Starting audio transcription...")
141
+ transcript = transcribe_audio(temp_audio_path, pipeline)
142
  print(f"Transcription completed. Transcript length: {len(transcript)} characters")
143
 
144
  # Clean up the temporary file
 
161
  html.H1("Video Transcription", className="text-center mb-4"),
162
  dbc.Card([
163
  dbc.CardBody([
164
+ dbc.Input(id="hf-token", type="password", placeholder="Enter Hugging Face Token", className="mb-3"),
165
  dbc.Input(id="video-url", type="text", placeholder="Enter video URL"),
166
  dbc.Button("Transcribe", id="transcribe-button", color="primary", className="mt-3"),
167
  dbc.Spinner(html.Div(id="transcription-output", className="mt-3")),
 
176
  Output("transcription-output", "children"),
177
  Output("download-transcript", "data"),
178
  Input("transcribe-button", "n_clicks"),
179
+ State("hf-token", "value"),
180
  State("video-url", "value"),
181
  prevent_initial_call=True
182
  )
183
+ def update_transcription(n_clicks, hf_token, url):
184
+ if not url or not hf_token:
185
  raise PreventUpdate
186
 
187
  def transcribe():
188
+ try:
189
+ # Initialize the speaker diarization pipeline with the provided token
190
+ pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization", use_auth_token=hf_token)
191
+ print("Speaker diarization pipeline initialized successfully")
192
+
193
+ transcript = transcribe_video(url, pipeline)
194
+ return transcript
195
+ except Exception as e:
196
+ return f"An error occurred: {str(e)}"
197
 
198
  # Run transcription in a separate thread
199
  thread = threading.Thread(target=transcribe)
 
202
 
203
  transcript = transcribe()
204
 
205
+ if transcript and not transcript.startswith("An error occurred"):
206
  download_data = dict(content=transcript, filename="transcript.txt")
207
  return dbc.Card([
208
  dbc.CardBody([
 
212
  ])
213
  ]), download_data
214
  else:
215
+ return transcript, None
216
 
217
  if __name__ == '__main__':
218
  app.run(debug=True, host='0.0.0.0', port=7860)