Spaces:
Sleeping
Sleeping
import gradio as gr | |
import azure.cognitiveservices.speech as speechsdk | |
# Azure credentials | |
speech_key = "12afe22c558a4f8d8bd28d6a67cdb9b0" | |
service_region = "westus" | |
def recognize_speech(input_source, audio_file): | |
try: | |
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region) | |
if input_source == "Microphone": | |
audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True) | |
else: | |
audio_config = speechsdk.audio.AudioConfig(filename=audio_file.name) | |
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config) | |
print("Say something or wait for the audio file to be processed...") | |
result = speech_recognizer.recognize_once() | |
if result.reason == speechsdk.ResultReason.RecognizedSpeech: | |
return f"Recognized: {result.text}" | |
elif result.reason == speechsdk.ResultReason.NoMatch: | |
return "No speech could be recognized" | |
elif result.reason == speechsdk.ResultReason.Canceled: | |
cancellation_details = result.cancellation_details | |
return f"Speech Recognition canceled: {cancellation_details.reason}\nError details: {cancellation_details.error_details}" | |
except Exception as e: | |
return f"An error occurred: {e}" | |
iface = gr.Interface( | |
fn=recognize_speech, | |
inputs=[ | |
gr.inputs.Radio(choices=["Microphone", "Audio File"], label="Input Source", default="Microphone"), | |
gr.inputs.File(label="Upload Audio File", type="file") | |
], | |
outputs="text" | |
) | |
iface.launch() | |