aikitty commited on
Commit
411d6c8
·
verified ·
1 Parent(s): faaecc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -1,27 +1,41 @@
 
1
  import azure.cognitiveservices.speech as speechsdk
2
 
3
  # Azure credentials
4
  speech_key = "12afe22c558a4f8d8bd28d6a67cdb9b0"
5
- service_region = "westus"
6
 
7
- def test_speech_sdk():
8
  try:
9
  speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
10
- audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
 
 
 
 
 
11
  speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
12
 
13
- print("Say something...")
14
  result = speech_recognizer.recognize_once()
15
 
16
  if result.reason == speechsdk.ResultReason.RecognizedSpeech:
17
- print(f"Recognized: {result.text}")
18
  elif result.reason == speechsdk.ResultReason.NoMatch:
19
- print("No speech could be recognized")
20
  elif result.reason == speechsdk.ResultReason.Canceled:
21
  cancellation_details = result.cancellation_details
22
- print(f"Speech Recognition canceled: {cancellation_details.reason}")
23
- print(f"Error details: {cancellation_details.error_details}")
24
  except Exception as e:
25
- print(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
26
 
27
- test_speech_sdk()
 
1
+ import gradio as gr
2
  import azure.cognitiveservices.speech as speechsdk
3
 
4
  # Azure credentials
5
  speech_key = "12afe22c558a4f8d8bd28d6a67cdb9b0"
6
+ service_region = "westus"
7
 
8
+ def recognize_speech(input_source, audio_file):
9
  try:
10
  speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
11
+
12
+ if input_source == "Microphone":
13
+ audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
14
+ else:
15
+ audio_config = speechsdk.audio.AudioConfig(filename=audio_file.name)
16
+
17
  speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
18
 
19
+ print("Say something or wait for the audio file to be processed...")
20
  result = speech_recognizer.recognize_once()
21
 
22
  if result.reason == speechsdk.ResultReason.RecognizedSpeech:
23
+ return f"Recognized: {result.text}"
24
  elif result.reason == speechsdk.ResultReason.NoMatch:
25
+ return "No speech could be recognized"
26
  elif result.reason == speechsdk.ResultReason.Canceled:
27
  cancellation_details = result.cancellation_details
28
+ return f"Speech Recognition canceled: {cancellation_details.reason}\nError details: {cancellation_details.error_details}"
 
29
  except Exception as e:
30
+ return f"An error occurred: {e}"
31
+
32
+ iface = gr.Interface(
33
+ fn=recognize_speech,
34
+ inputs=[
35
+ gr.inputs.Radio(choices=["Microphone", "Audio File"], label="Input Source", default="Microphone"),
36
+ gr.inputs.File(label="Upload Audio File", type="file")
37
+ ],
38
+ outputs="text"
39
+ )
40
 
41
+ iface.launch()