Files
encoach_backend/helper/speech_to_text_helper.py
2023-09-05 20:31:30 +01:00

24 lines
718 B
Python

import whisper
import os
import gtts
import nltk
nltk.download('words')
from nltk.corpus import words
def speech_to_text(file_path):
if os.path.exists(file_path):
model = whisper.load_model("base")
result = model.transcribe(file_path, fp16=False, language='English', verbose=False)
return result["text"]
else:
print("File not found:", file_path)
raise Exception("File " + file_path + " not found.")
def text_to_speech(text: str, file_name: str):
tts = gtts.gTTS(text)
tts.save(file_name)
def has_words(text: str):
english_words = set(words.words())
words_in_input = text.split()
return any(word.lower() in english_words for word in words_in_input)