Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions backend/speech_recognition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import subprocess
import sys
import winsound
import whisper


def generate_test_audio(text, output_path="test.wav"):
# Run pyttsx3 in a subprocess to avoid COM/torch conflict
script = (
f"import pyttsx3; e = pyttsx3.init(); "
f"e.save_to_file({repr(text)}, {repr(output_path)}); e.runAndWait()"
)
subprocess.run([sys.executable, "-c", script], check=True)
print(f"Test audio saved to: {output_path}")
return output_path


def recognize_speech(wav_file):
model = whisper.load_model("base")
result = model.transcribe(wav_file)
return result["text"]


def play_audio(audio_path):
winsound.PlaySound(audio_path, winsound.SND_FILENAME)


if __name__ == "__main__":
test_text = "Hello, this is a test of the speech recognition system."
wav_file = generate_test_audio(test_text)

transcription = recognize_speech(wav_file)
print("Recognized:", transcription)

play_audio(wav_file)