Update: Refactored the code

This commit is contained in:
Izaya Orihara
2023-10-06 15:45:33 +02:00
parent 0b1c24de2a
commit 57cc73a439
4 changed files with 165 additions and 82 deletions
+35 -16
View File
@@ -1,27 +1,46 @@
import logging
from typing import List
from src.file_manager import FileManager
from src.transcriber import Transcriber
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class TranscriptGenerator:
"""
TranscriptGenerator class that coordinates FileManager and Transcriber classes
to generate transcriptions from given input sources.
"""
def __init__(self):
self.file_manager = FileManager()
self.transcriber = Transcriber()
def generate(self, input_sources):
def generate(self, input_sources: List[str]) -> None:
"""
Generates transcriptions for the given list of input sources.
Args:
input_sources (List[str]): List of input sources which could be URLs or local file paths.
"""
for i, source in enumerate(input_sources):
if source.startswith('http'):
mp4_filename = f"audio_{i + 1}.mp4"
wav_filename = f"audio_{i + 1}.wav"
try:
if source.startswith('http'):
mp4_filename = f"audio_{i + 1}.mp4"
wav_filename = f"audio_{i + 1}.wav"
self.file_manager.download_youtube_audio(source, mp4_filename)
self.file_manager.convert_to_wav(mp4_filename, wav_filename)
else:
wav_filename = self.file_manager.load_local_file(source)
self.transcriber.transcribe(wav_filename)
json_output_file = f'result_{i + 1}.json'
txt_output_file = f'transcripts_{i + 1}.txt'
self.transcriber.save_transcript(json_output_file, txt_output_file)
except Exception as e:
logger.exception(f"Failed to generate transcript for source {source}. Error: {str(e)}")
continue
self.file_manager.download_youtube_audio(source, mp4_filename)
self.file_manager.convert_to_wav(mp4_filename, wav_filename)
else:
wav_filename = self.file_manager.load_local_file(source)
transcription = self.transcriber.transcribe(wav_filename)
json_output_file = f'result_{i + 1}.json'
txt_output_file = f'transcripts_{i + 1}.txt'
self.transcriber.save_transcript(json_output_file, txt_output_file)
print('Transcription generation completed.')
logger.info('Transcription generation completed.')