1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- if __name__ == '__main__':
- EXTENDED_LOGGING = False
- import os
- import sys
- if os.name == "nt" and (3, 8) <= sys.version_info < (3, 99):
- from torchaudio._extension.utils import _init_dll_path
- _init_dll_path()
- if EXTENDED_LOGGING:
- import logging
- logging.basicConfig(level=logging.DEBUG)
- from RealtimeSTT import AudioToTextRecorder
- from colorama import Fore, Back, Style
- import colorama
- print("Initializing RealtimeSTT test...")
- colorama.init()
- full_sentences = []
- displayed_text = ""
- def clear_console():
- os.system('clear' if os.name == 'posix' else 'cls')
- def text_detected(text):
- global displayed_text
- sentences_with_style = [
- f"{Fore.YELLOW + sentence + Style.RESET_ALL if i % 2 == 0 else Fore.CYAN + sentence + Style.RESET_ALL} "
- for i, sentence in enumerate(full_sentences)
- ]
- new_text = "".join(sentences_with_style).strip() + " " + text if len(sentences_with_style) > 0 else text
- if new_text != displayed_text:
- displayed_text = new_text
- clear_console()
- print(f"Language: {recorder.detected_language} (realtime: {recorder.detected_realtime_language})")
- print(displayed_text, end="", flush=True)
- def process_text(text):
- full_sentences.append(text)
- text_detected("")
- recorder_config = {
- 'spinner': False,
- 'model': 'large-v2',
- 'realtime_model_type': 'tiny',
- 'language': 'en',
- 'silero_sensitivity': 0.05,
- 'webrtc_sensitivity': 3,
- 'post_speech_silence_duration': 0.4,
- 'min_length_of_recording': 0,
- 'min_gap_between_recordings': 0,
- 'enable_realtime_transcription': False,
- 'realtime_processing_pause': 0,
- 'on_realtime_transcription_update': text_detected,
- 'silero_deactivity_detection': True,
- 'min_length_of_recording': 0.5,
- 'early_transcription_on_silence': False
- }
- # Conditionally add logging level if EXTENDED_LOGGING is True
- if EXTENDED_LOGGING:
- recorder_config['level'] = logging.DEBUG
- recorder = AudioToTextRecorder(**recorder_config)
- clear_console()
- print("Say something...", end="", flush=True)
- try:
- while (True):
- recorder.text(process_text)
- except KeyboardInterrupt:
- print("Exiting application due to keyboard interrupt")
|