Update app.py
Browse files
app.py
CHANGED
|
@@ -79,20 +79,15 @@ def format_transcript(transcript):
|
|
| 79 |
|
| 80 |
def transcribe_audio(audio_file):
|
| 81 |
try:
|
| 82 |
-
# Load the entire audio file
|
| 83 |
print("Loading audio file...")
|
| 84 |
audio_input, sr = librosa.load(audio_file, sr=16000)
|
| 85 |
-
|
| 86 |
-
# Convert to float32 numpy array
|
| 87 |
audio_input = audio_input.astype(np.float32)
|
| 88 |
-
|
| 89 |
print(f"Audio duration: {len(audio_input) / sr:.2f} seconds")
|
| 90 |
|
| 91 |
-
# Process in chunks of 30 seconds with overlap
|
| 92 |
chunk_length = 30 * sr
|
| 93 |
-
overlap = 5 * sr
|
| 94 |
transcriptions = []
|
| 95 |
-
|
| 96 |
print("Starting transcription...")
|
| 97 |
for i in range(0, len(audio_input), chunk_length - overlap):
|
| 98 |
chunk = audio_input[i:i+chunk_length]
|
|
@@ -102,21 +97,34 @@ def transcribe_audio(audio_file):
|
|
| 102 |
transcriptions.extend(transcription)
|
| 103 |
print(f"Processed {i / sr:.2f} to {(i + chunk_length) / sr:.2f} seconds")
|
| 104 |
|
| 105 |
-
# Join all transcriptions
|
| 106 |
full_transcription = " ".join(transcriptions)
|
| 107 |
-
|
| 108 |
print(f"Transcription complete. Full transcription length: {len(full_transcription)} characters")
|
| 109 |
-
|
| 110 |
-
# Apply spelling correction and formatting
|
| 111 |
-
print("Applying spelling correction and formatting...")
|
| 112 |
-
full_transcription = correct_spelling(full_transcription)
|
| 113 |
-
full_transcription = format_transcript(full_transcription)
|
| 114 |
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
print(f"Error in transcribe_audio: {str(e)}")
|
| 118 |
raise
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
def transcribe_video(url):
|
| 121 |
try:
|
| 122 |
print(f"Attempting to download audio from URL: {url}")
|
|
|
|
| 79 |
|
| 80 |
def transcribe_audio(audio_file):
|
| 81 |
try:
|
|
|
|
| 82 |
print("Loading audio file...")
|
| 83 |
audio_input, sr = librosa.load(audio_file, sr=16000)
|
|
|
|
|
|
|
| 84 |
audio_input = audio_input.astype(np.float32)
|
|
|
|
| 85 |
print(f"Audio duration: {len(audio_input) / sr:.2f} seconds")
|
| 86 |
|
|
|
|
| 87 |
chunk_length = 30 * sr
|
| 88 |
+
overlap = 5 * sr
|
| 89 |
transcriptions = []
|
| 90 |
+
|
| 91 |
print("Starting transcription...")
|
| 92 |
for i in range(0, len(audio_input), chunk_length - overlap):
|
| 93 |
chunk = audio_input[i:i+chunk_length]
|
|
|
|
| 97 |
transcriptions.extend(transcription)
|
| 98 |
print(f"Processed {i / sr:.2f} to {(i + chunk_length) / sr:.2f} seconds")
|
| 99 |
|
|
|
|
| 100 |
full_transcription = " ".join(transcriptions)
|
|
|
|
| 101 |
print(f"Transcription complete. Full transcription length: {len(full_transcription)} characters")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
print("Applying formatting and paragraph breaks...")
|
| 104 |
+
formatted_transcription = format_transcript_with_breaks(full_transcription)
|
| 105 |
+
|
| 106 |
+
return formatted_transcription
|
| 107 |
except Exception as e:
|
| 108 |
print(f"Error in transcribe_audio: {str(e)}")
|
| 109 |
raise
|
| 110 |
|
| 111 |
+
def format_transcript_with_breaks(transcript):
|
| 112 |
+
# Split into sentences
|
| 113 |
+
sentences = re.split('(?<=[.!?]) +', transcript)
|
| 114 |
+
paragraphs = []
|
| 115 |
+
current_paragraph = []
|
| 116 |
+
|
| 117 |
+
for sentence in sentences:
|
| 118 |
+
current_paragraph.append(sentence)
|
| 119 |
+
if len(current_paragraph) >= 3: # Adjust this number to control paragraph size
|
| 120 |
+
paragraphs.append(' '.join(current_paragraph))
|
| 121 |
+
current_paragraph = []
|
| 122 |
+
|
| 123 |
+
if current_paragraph:
|
| 124 |
+
paragraphs.append(' '.join(current_paragraph))
|
| 125 |
+
|
| 126 |
+
return '\n\n'.join(paragraphs)
|
| 127 |
+
|
| 128 |
def transcribe_video(url):
|
| 129 |
try:
|
| 130 |
print(f"Attempting to download audio from URL: {url}")
|