Update app.py
Browse files68 unmodified lines697071727273747576777877787980818283848584858687888990918687888990919293949596979899100101102103939495961041059810610710810968 unmodified lines
def transcribe_audio(audio_file): try: print("Loading audio file...") logger.info("Loading audio file...") audio = AudioSegment.from_file(audio_file) audio = audio.set_channels(1).set_frame_rate(16000) audio_array = torch.tensor(audio.get_array_of_samples()).float() print(f"Audio duration: {len(audio) / 1000:.2f} seconds") print("Starting transcription...") logger.info(f"Audio duration: {len(audio) / 1000:.2f} seconds") logger.info("Starting transcription...") input_features = whisper_processor(audio_array, sampling_rate=16000, return_tensors="pt").input_features.to(device) # Create attention mask attention_mask = torch.ones_like(input_features) max_retries = 3 for attempt in range(max_retries): # Generate with specific parameters predicted_ids = whisper_model.generate( input_features, attention_mask=attention_mask, language='en', task='translate' ) transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True) # Generate with specific parameters predicted_ids = whisper_model.generate( input_features, attention_mask=attention_mask, language='en', task='translate', temperature=0.7, # Adjust temperature for potentially better results num_beams=5, # Increase beam search for potentially better results max_length=448, # Increase max length to allow for longer transcriptions ) transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True) logger.info(f"Transcription attempt {attempt + 1} complete. Length: {len(transcription[0])} characters") if len(transcription[0]) >= 10: return transcription[0] else: logger.warning(f"Transcription too short on attempt {attempt + 1}: {transcription[0]}") print(f"Transcription complete. Length: {len(transcription[0])} characters") if len(transcription[0]) < 10: raise ValueError(f"Transcription too short: {transcription[0]}") return transcription[0] raise ValueError(f"Failed to generate a valid transcription after {max_retries} attempts") except Exception as e: print(f"Error in transcribe_audio: {str(e)}") logger.error(f"Error in transcribe_audio: {str(e)}") raise
def separate_speakers(transcription):app.py+24-16