text-generation-webui/extensions/whisper_stt/script.py

38 lines
1.1 KiB
Python
Raw Normal View History

2023-03-09 06:46:50 -05:00
import gradio as gr
import speech_recognition as sr
2023-03-09 14:33:00 -05:00
2023-03-09 06:46:50 -05:00
input_hijack = {
'state': False,
'value': ["", ""]
}
def do_stt():
transcription = ""
r = sr.Recognizer()
with sr.Microphone() as source:
2023-03-09 15:18:46 -05:00
r.adjust_for_ambient_noise(source, 0.2)
2023-03-09 06:46:50 -05:00
audio = r.listen(source)
try:
2023-03-09 15:18:46 -05:00
transcription = r.recognize_whisper(audio, language="english", model="base.en")
2023-03-09 06:46:50 -05:00
except sr.UnknownValueError:
print("Whisper could not understand audio")
except sr.RequestError as e:
2023-03-09 14:33:00 -05:00
print("Could not request results from Whisper", e)
2023-03-09 06:46:50 -05:00
input_hijack.update({"state": True, "value": [transcription, transcription]})
return transcription
2023-03-09 15:03:49 -05:00
def update_hijack(val):
input_hijack.update({"state": True, "value": [val, val]})
return val
2023-03-09 06:46:50 -05:00
def ui():
2023-03-09 14:33:00 -05:00
speech_button = gr.Button(value="🎙️")
2023-03-09 15:03:49 -05:00
output_transcription = gr.Textbox(label="STT-Input", placeholder="Speech Preview. Click \"Generate\" to send", interactive=True)
output_transcription.change(fn=update_hijack, inputs=[output_transcription])
2023-03-09 06:46:50 -05:00
speech_button.click(do_stt, outputs=[output_transcription])