File size: 2,359 Bytes
a616221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv
import os
from vosk import Model, KaldiRecognizer
import pyaudio
import json

# Load environment variables from .env file
load_dotenv()

# Get the Vosk model path from the environment variable
vosk_model_path = os.getenv("vosk_model_path")

if not vosk_model_path:
    print("Error: vosk_model_path is not set in the .env file.")
    exit()

# Initialize the Vosk model
try:
    model = Model(vosk_model_path)
    print("Vosk model loaded successfully.")
except Exception as e:
    print(f"Failed to load Vosk model: {e}")
    exit()

# Initialize recognizer and audio input
recognizer = KaldiRecognizer(model, 16000)
audio = pyaudio.PyAudio()

# Open audio stream
stream = audio.open(format=pyaudio.paInt16, 
                    channels=1, 
                    rate=16000, 
                    input=True, 
                    frames_per_buffer=4000)
stream.start_stream()

print("Say 'start listening' to begin transcription and 'stop listening' to stop.")

# State management
is_listening = False

try:
    while True:
        data = stream.read(4000, exception_on_overflow=False)

        if recognizer.AcceptWaveform(data):
            result = recognizer.Result()
            text = json.loads(result)["text"]
            
            # Check for commands to start or stop listening
            if "start listening" in text.lower():
                is_listening = True
                print("Listening started. Speak into the microphone.")
                continue
            elif "stop listening" in text.lower():
                is_listening = False
                print("Listening stopped. Say 'start listening' to resume.")
                continue

            # Transcribe if actively listening
            if is_listening:
                print(f"Transcription: {text}")
        else:
            # Handle partial results if needed
            chunk_result = recognizer.PartialResult()
            chunk_text = json.loads(chunk_result)["partial"]

            # Display partial transcription only if actively listening
            if is_listening and chunk_text:
                print(f"chunk: {chunk_text}", end="\r")

except KeyboardInterrupt:
    print("\nExiting...")
    stream.stop_stream()
    stream.close()
    audio.terminate()