bluenevus commited on
Commit
8af57a0
·
verified ·
1 Parent(s): e0a7ef4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -40
app.py CHANGED
@@ -18,14 +18,11 @@ import dash_bootstrap_components as dbc
18
  from dash.exceptions import PreventUpdate
19
  import base64
20
  import threading
 
21
 
22
  # Initialize the speaker diarization pipeline
23
- try:
24
- pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization")
25
- print("Speaker diarization pipeline initialized successfully")
26
- except Exception as e:
27
- print(f"Error initializing speaker diarization pipeline: {str(e)}")
28
- pipeline = None
29
 
30
  # Check if CUDA is available and set the device
31
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -40,7 +37,15 @@ spell = SpellChecker()
40
 
41
  def download_audio_from_url(url):
42
  try:
43
- if "share" in url:
 
 
 
 
 
 
 
 
44
  print("Processing shareable link...")
45
  response = requests.get(url)
46
  soup = BeautifulSoup(response.content, 'html.parser')
@@ -50,12 +55,13 @@ def download_audio_from_url(url):
50
  print(f"Extracted video URL: {video_url}")
51
  else:
52
  raise ValueError("Direct video URL not found in the shareable link.")
 
 
53
  else:
54
- video_url = url
 
 
55
 
56
- print(f"Downloading video from URL: {video_url}")
57
- response = requests.get(video_url)
58
- audio_bytes = response.content
59
  print(f"Successfully downloaded {len(audio_bytes)} bytes of data")
60
  return audio_bytes
61
  except Exception as e:
@@ -91,12 +97,9 @@ def transcribe_audio(audio_file):
91
  print(f"Audio duration: {len(audio_input) / sr:.2f} seconds")
92
 
93
  # Apply speaker diarization
94
- if pipeline:
95
- print("Applying speaker diarization...")
96
- diarization = pipeline(audio_file)
97
- print("Speaker diarization complete.")
98
- else:
99
- diarization = None
100
 
101
  chunk_length = 30 * sr
102
  overlap = 5 * sr
@@ -114,34 +117,14 @@ def transcribe_audio(audio_file):
114
  full_transcription = " ".join(transcriptions)
115
  print(f"Transcription complete. Full transcription length: {len(full_transcription)} characters")
116
 
117
- if diarization:
118
- print("Applying formatting with speaker diarization...")
119
- formatted_transcription = format_transcript_with_speakers(full_transcription, diarization)
120
- else:
121
- print("Applying formatting without speaker diarization...")
122
- formatted_transcription = format_transcript_with_breaks(full_transcription)
123
 
124
  return formatted_transcription
125
  except Exception as e:
126
  print(f"Error in transcribe_audio: {str(e)}")
127
  raise
128
 
129
- def format_transcript_with_breaks(transcript):
130
- sentences = re.split('(?<=[.!?]) +', transcript)
131
- paragraphs = []
132
- current_paragraph = []
133
-
134
- for sentence in sentences:
135
- current_paragraph.append(sentence)
136
- if len(current_paragraph) >= 3: # Adjust this number to control paragraph size
137
- paragraphs.append(' '.join(current_paragraph))
138
- current_paragraph = []
139
-
140
- if current_paragraph:
141
- paragraphs.append(' '.join(current_paragraph))
142
-
143
- return '\n\n'.join(paragraphs)
144
-
145
  def transcribe_video(url):
146
  try:
147
  print(f"Attempting to download audio from URL: {url}")
@@ -219,7 +202,7 @@ def update_transcription(n_clicks, url):
219
  return dbc.Card([
220
  dbc.CardBody([
221
  html.H5("Transcription Result"),
222
- html.Pre(transcript),
223
  dbc.Button("Download Transcript", id="btn-download", color="secondary", className="mt-3")
224
  ])
225
  ]), download_data
 
18
  from dash.exceptions import PreventUpdate
19
  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"
 
37
 
38
  def download_audio_from_url(url):
39
  try:
40
+ if "youtube.com" in url or "youtu.be" in url:
41
+ print("Processing YouTube URL...")
42
+ yt = YouTube(url)
43
+ audio_stream = yt.streams.filter(only_audio=True).first()
44
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file:
45
+ audio_stream.download(output_path=temp_file.name)
46
+ audio_bytes = open(temp_file.name, "rb").read()
47
+ os.unlink(temp_file.name)
48
+ elif "share" in url:
49
  print("Processing shareable link...")
50
  response = requests.get(url)
51
  soup = BeautifulSoup(response.content, 'html.parser')
 
55
  print(f"Extracted video URL: {video_url}")
56
  else:
57
  raise ValueError("Direct video URL not found in the shareable link.")
58
+ response = requests.get(video_url)
59
+ audio_bytes = response.content
60
  else:
61
+ print(f"Downloading video from URL: {url}")
62
+ response = requests.get(url)
63
+ audio_bytes = response.content
64
 
 
 
 
65
  print(f"Successfully downloaded {len(audio_bytes)} bytes of data")
66
  return audio_bytes
67
  except Exception as e:
 
97
  print(f"Audio duration: {len(audio_input) / sr:.2f} seconds")
98
 
99
  # Apply speaker diarization
100
+ print("Applying speaker diarization...")
101
+ diarization = pipeline(audio_file)
102
+ print("Speaker diarization complete.")
 
 
 
103
 
104
  chunk_length = 30 * sr
105
  overlap = 5 * sr
 
117
  full_transcription = " ".join(transcriptions)
118
  print(f"Transcription complete. Full transcription length: {len(full_transcription)} characters")
119
 
120
+ print("Applying formatting with speaker diarization...")
121
+ formatted_transcription = format_transcript_with_speakers(full_transcription, diarization)
 
 
 
 
122
 
123
  return formatted_transcription
124
  except Exception as e:
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}")
 
202
  return dbc.Card([
203
  dbc.CardBody([
204
  html.H5("Transcription Result"),
205
+ html.Pre(transcript, style={"white-space": "pre-wrap", "word-wrap": "break-word"}),
206
  dbc.Button("Download Transcript", id="btn-download", color="secondary", className="mt-3")
207
  ])
208
  ]), download_data