bluenevus commited on
Commit
256795b
·
verified ·
1 Parent(s): b0c825b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -76,11 +76,12 @@ def transcribe_audio(audio_file):
76
  # Convert to float32 numpy array
77
  audio_input = audio_input.astype(np.float32)
78
 
79
- # Process in chunks of 30 seconds
80
  chunk_length = 30 * sr
 
81
  transcriptions = []
82
 
83
- for i in range(0, len(audio_input), chunk_length):
84
  chunk = audio_input[i:i+chunk_length]
85
  input_features = processor(chunk, sampling_rate=16000, return_tensors="pt").input_features.to(device)
86
  predicted_ids = model.generate(input_features)
@@ -90,6 +91,7 @@ def transcribe_audio(audio_file):
90
  # Join all transcriptions
91
  full_transcription = " ".join(transcriptions)
92
 
 
93
  return full_transcription
94
  except Exception as e:
95
  print(f"Error in transcribe_audio: {str(e)}")
@@ -104,6 +106,8 @@ def transcribe_video(url):
104
  # Convert audio bytes to AudioSegment
105
  audio = AudioSegment.from_file(io.BytesIO(audio_bytes))
106
 
 
 
107
  # Save as WAV file
108
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
109
  audio.export(temp_audio.name, format="wav")
@@ -111,7 +115,7 @@ def transcribe_video(url):
111
 
112
  print("Starting audio transcription...")
113
  transcript = transcribe_audio(temp_audio_path)
114
- print("Transcription completed successfully")
115
 
116
  # Clean up the temporary file
117
  os.unlink(temp_audio_path)
@@ -123,6 +127,8 @@ def transcribe_video(url):
123
  return transcript
124
  except Exception as e:
125
  error_message = f"An error occurred: {str(e)}"
 
 
126
 
127
  def download_transcript(transcript):
128
  with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as temp_file:
 
76
  # Convert to float32 numpy array
77
  audio_input = audio_input.astype(np.float32)
78
 
79
+ # Process in chunks of 30 seconds with overlap
80
  chunk_length = 30 * sr
81
+ overlap = 5 * sr # 5 seconds overlap
82
  transcriptions = []
83
 
84
+ for i in range(0, len(audio_input), chunk_length - overlap):
85
  chunk = audio_input[i:i+chunk_length]
86
  input_features = processor(chunk, sampling_rate=16000, return_tensors="pt").input_features.to(device)
87
  predicted_ids = model.generate(input_features)
 
91
  # Join all transcriptions
92
  full_transcription = " ".join(transcriptions)
93
 
94
+ print(f"Full transcription length: {len(full_transcription)} characters")
95
  return full_transcription
96
  except Exception as e:
97
  print(f"Error in transcribe_audio: {str(e)}")
 
106
  # Convert audio bytes to AudioSegment
107
  audio = AudioSegment.from_file(io.BytesIO(audio_bytes))
108
 
109
+ print(f"Audio duration: {len(audio) / 1000} seconds")
110
+
111
  # Save as WAV file
112
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
113
  audio.export(temp_audio.name, format="wav")
 
115
 
116
  print("Starting audio transcription...")
117
  transcript = transcribe_audio(temp_audio_path)
118
+ print(f"Transcription completed. Transcript length: {len(transcript)} characters")
119
 
120
  # Clean up the temporary file
121
  os.unlink(temp_audio_path)
 
127
  return transcript
128
  except Exception as e:
129
  error_message = f"An error occurred: {str(e)}"
130
+ print(error_message)
131
+ return error_message
132
 
133
  def download_transcript(transcript):
134
  with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as temp_file: