script.py 961 B

123456789101112131415161718192021222324252627282930313233343536
  1. import gradio as gr
  2. import speech_recognition as sr
  3. input_hijack = {
  4. 'state': False,
  5. 'value': ["", ""]
  6. }
  7. def input_modifier(string):
  8. return string
  9. def do_stt():
  10. transcription = ""
  11. r = sr.Recognizer()
  12. with sr.Microphone() as source:
  13. r.adjust_for_ambient_noise(source)
  14. audio = r.listen(source)
  15. try:
  16. transcription = r.recognize_whisper(audio, language="english", model="tiny.en")
  17. except sr.UnknownValueError:
  18. print("Whisper could not understand audio")
  19. except sr.RequestError as e:
  20. print("Could not request results from Whisper", e)
  21. input_hijack.update({"state": True, "value": [transcription, transcription]})
  22. return transcription
  23. def ui():
  24. speech_button = gr.Button(value="🎙️")
  25. output_transcription = gr.Textbox(label="STT-Preview", placeholder="Speech Preview. Click \"Generate\" to send")
  26. speech_button.click(do_stt, outputs=[output_transcription])