Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

stt_tts.py 2.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. from tempfile import NamedTemporaryFile
  2. from gtts import gTTS
  3. import speech_recognition as sr
  4. class STT_TTS:
  5. def __init__(self):
  6. """
  7. Initializes the Moderator with a speech recognizer.
  8. """
  9. self.speech_recognizer = sr.Recognizer()
  10. def text_to_speech(self, text: str) -> str:
  11. """
  12. Converts text to speech and saves it to a temporary WAV file.
  13. Parameters
  14. ----------
  15. text : str
  16. The text to convert into audio.
  17. Returns
  18. -------
  19. str
  20. Filename of the temporary WAV file.
  21. """
  22. # Create a gTTS (Google Text-to-Speech) object for the given text
  23. speech = gTTS(text)
  24. # Create a named temporary file with a ".wav" extension for audio
  25. with NamedTemporaryFile(suffix=".wav", delete=False) as audio_temp_file:
  26. # Write the speech into the temporary file
  27. speech.write_to_fp(audio_temp_file)
  28. # Return the filename of the temporary WAV file
  29. return audio_temp_file.name
  30. def speech_to_text(self, audio_filename: str) -> str:
  31. """
  32. Converts speech from an audio file to text.
  33. Parameters
  34. ----------
  35. audio_filename : str
  36. The filename of the audio file.
  37. Returns
  38. -------
  39. str
  40. The recognized text from the audio.
  41. """
  42. # Open the audio file for recognition
  43. with sr.AudioFile(audio_filename) as audio_file:
  44. # Record the audio data from the file
  45. audio_data = self.speech_recognizer.record(audio_file)
  46. recognized_text = ""
  47. try:
  48. # Recognize the text from the recorded audio using Google Speech Recognition
  49. recognized_text = self.speech_recognizer.recognize_google(audio_data)
  50. except sr.UnknownValueError:
  51. print("Speech Recognition could not understand the audio.")
  52. except sr.RequestError as e:
  53. print(f"Error in requesting results from Google Speech Recognition service: {e}")
  54. return recognized_text
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...