Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,33 @@
|
|
| 1 |
-
import
|
| 2 |
import azure.cognitiveservices.speech as speechsdk
|
| 3 |
|
| 4 |
-
# Azure credentials
|
| 5 |
speech_key = "12afe22c558a4f8d8bd28d6a67cdb9b0"
|
| 6 |
service_region = "westus"
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import azure.cognitiveservices.speech as speechsdk
|
| 3 |
|
| 4 |
+
# Azure Speech Service credentials
|
| 5 |
speech_key = "12afe22c558a4f8d8bd28d6a67cdb9b0"
|
| 6 |
service_region = "westus"
|
| 7 |
|
| 8 |
+
def transcribe_audio(file_path):
|
| 9 |
+
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
|
| 10 |
+
audio_input = speechsdk.AudioConfig(filename=file_path)
|
| 11 |
+
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
result = speech_recognizer.recognize_once()
|
| 14 |
+
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
|
| 15 |
+
return result.text
|
| 16 |
+
elif result.reason == speechsdk.ResultReason.NoMatch:
|
| 17 |
+
return "No speech could be recognized"
|
| 18 |
+
else:
|
| 19 |
+
return "Speech recognition canceled: {}".format(result.cancellation_details.reason)
|
|
|
|
| 20 |
|
| 21 |
+
st.title("Chinese Pronunciation Checker")
|
| 22 |
+
|
| 23 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3"])
|
| 24 |
+
|
| 25 |
+
if uploaded_file is not None:
|
| 26 |
+
st.audio(uploaded_file, format="audio/wav")
|
| 27 |
+
file_path = f"temp_audio.{uploaded_file.type.split('/')[-1]}"
|
| 28 |
+
|
| 29 |
+
with open(file_path, "wb") as f:
|
| 30 |
+
f.write(uploaded_file.getbuffer())
|
| 31 |
+
|
| 32 |
+
transcript = transcribe_audio(file_path)
|
| 33 |
+
st.write("Transcribed Text: ", transcript)
|