File size: 1,648 Bytes
411d6c8
d8682b4
 
 
 
411d6c8
d8682b4
411d6c8
d8682b4
 
411d6c8
 
 
 
 
 
d8682b4
 
411d6c8
d8682b4
 
 
411d6c8
d8682b4
411d6c8
d8682b4
 
411d6c8
d8682b4
411d6c8
 
 
 
 
 
 
 
 
 
d8682b4
411d6c8
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
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()