Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
之前的错误会导致合并字幕 word 出现问题
  • Loading branch information
Huanshere committed Sep 12, 2024
1 parent f019913 commit c6617f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
14 changes: 9 additions & 5 deletions core/all_whisper_methods/whisperX.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,32 @@ def transcribe_audio(audio_file: str) -> Dict:
raise Exception(f"WhisperX processing error: {e}")

def process_transcription(result: Dict) -> pd.DataFrame:
from config import get_joiner, WHISPER_LANGUAGE
language = result['language'] if WHISPER_LANGUAGE == 'auto' else WHISPER_LANGUAGE # consider force english case
joiner = get_joiner(language)

all_words = []
for segment in result['segments']:
for word in segment['words']:
# ! For French, we need to convert guillemets to empty strings
word["word"] = word["word"].replace('»', '').replace('«', '')

if 'start' not in word and 'end' not in word:
if all_words:
# Merge with the previous word
all_words[-1]['text'] = f'{all_words[-1]["text"][:-1]}{word["word"]}"'
all_words[-1]['text'] = f'{all_words[-1]["text"]}{joiner}{word["word"]}'
else:
# If it's the first word, temporarily save it and wait for the next word with a timestamp
temp_word = word["word"]
else:
# Normal case, with start and end times
word_dict = {
'text': f'"{temp_word}{word["word"]}"' if 'temp_word' in locals() else f'"{word["word"]}"',
'text': f'{temp_word}{word["word"]}' if 'temp_word' in locals() else f'{word["word"]}',
'start': word.get('start', all_words[-1]['end'] if all_words else 0),
'end': word['end'],
'score': word.get('score', 0)
}

# ! For French, we need to convert guillemets to empty strings
word_dict['text'] = word_dict['text'].replace('»', '').replace('«', '')

all_words.append(word_dict)
if 'temp_word' in locals():
del temp_word
Expand Down
7 changes: 5 additions & 2 deletions core/all_whisper_methods/whisperXapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ def transcribe_audio(audio_base64: str) -> Dict:
except Exception as e:
raise Exception(f"Error accessing whisperX API: {e} Please check your Replicate API key and internet connection.\n")


def process_transcription(result: Dict) -> pd.DataFrame:
from config import get_joiner, WHISPER_LANGUAGE
language = result['detected_language'] if WHISPER_LANGUAGE == 'auto' else WHISPER_LANGUAGE # consider force english case
joiner = get_joiner(language)

all_words = []
for segment in result['segments']:
for word in segment['words']:
Expand All @@ -77,7 +80,7 @@ def process_transcription(result: Dict) -> pd.DataFrame:
if 'start' not in word and 'end' not in word:
if all_words:
# Merge with the previous word
all_words[-1]['text'] = f'{all_words[-1]["text"][:-1]}{word["word"]}"'
all_words[-1]['text'] = f'{all_words[-1]["text"]}{joiner}{word["word"]}'
else:
# If it's the first word, temporarily save it and wait for the next word with a timestamp
temp_word = word["word"]
Expand Down

0 comments on commit c6617f4

Please sign in to comment.