script.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import gradio as gr
  2. import speech_recognition as sr
  3. import modules.shared as shared
  4. input_hijack = {
  5. 'state': False,
  6. 'value': ["", ""]
  7. }
  8. def input_modifier(string):
  9. return string
  10. def do_stt():
  11. transcription = ""
  12. r = sr.Recognizer()
  13. with sr.Microphone() as source:
  14. print("Say something!")
  15. r.adjust_for_ambient_noise(source)
  16. audio = r.listen(source)
  17. # recognize speech using whisper
  18. try:
  19. transcription = r.recognize_whisper(audio, language="english", model="tiny.en")
  20. print("Whisper thinks you said " + transcription)
  21. except sr.UnknownValueError:
  22. print("Whisper could not understand audio")
  23. except sr.RequestError as e:
  24. print("Could not request results from Whisper")
  25. # input_modifier(transcription)
  26. input_hijack.update({"state": True, "value": [transcription, transcription]})
  27. return transcription
  28. def ui():
  29. speech_button = gr.Button(value="STT")
  30. output_transcription = gr.Textbox(label="Speech Preview")
  31. speech_button.click(do_stt, outputs=[output_transcription])