-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_to_text.py
77 lines (63 loc) · 2.4 KB
/
speech_to_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from RealtimeSTT import AudioToTextRecorder
from colorama import Fore, Back, Style
import colorama
import os
if __name__ == "__main__":
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(displayed_text, end="", flush=True)
# def text_detected(text):
# global displayed_text
# clear_console()
# print(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(displayed_text, end="", flush=True)
def process_text(text):
full_sentences.append(text)
text_detected("")
recorder_config = {
"spinner": False,
"model": "large-v2",
"gpu_device_index": 0,
"language": "en",
"silero_sensitivity": 0.8,
"webrtc_sensitivity": 2,
"post_speech_silence_duration": 0.2,
"min_length_of_recording": 0,
"min_gap_between_recordings": 0,
"enable_realtime_transcription": True,
"realtime_processing_pause": 0,
"realtime_model_type": "tiny.en",
"on_realtime_transcription_update": text_detected,
#'on_realtime_transcription_stabilized': text_detected,
}
recorder = AudioToTextRecorder(**recorder_config)
clear_console()
print("Say something...", end="", flush=True)
while True:
recorder.text(process_text)