【问题标题】:How to integrate Azure text to speech with streamlit?如何将 Azure 文本与 streamlit 集成到语音?
【发布时间】:2022-08-08 09:43:51
【问题描述】:

我正在尝试将 azure text 与 streamlit 集成到语音中。

import azure.cognitiveservices.speech as speechsdk
import streamlit as st

st.title(\"Let\'s learn Math!\")

def recognize_from_microphone():
    speech_config = speechsdk.SpeechConfig(subscription=\"743ae1f5555f49f9a5de4457d4e91b2d\", region=\"australiaeast\")
    speech_config.speech_recognition_language=\"en-US\"

    #To recognize speech from an audio file, use `filename` instead of `use_default_microphone`:
    #audio_config = speechsdk.audio.AudioConfig(filename=\"YourAudioFile.wav\")
    audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

    st.text(\"Speak into your microphone.\")
    speech_recognition_result = speech_recognizer.recognize_once_async().get()

    if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
        st.text(\"Recognized: {}\".format(speech_recognition_result.text))
    elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
        st.text(\"No speech could be recognized: {}\".format(speech_recognition_result.no_match_details))
    elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = speech_recognition_result.cancellation_details
        st.text(\"Speech Recognition canceled: {}\".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            st.text(\"Error details: {}\".format(cancellation_details.error_details))
            st.text(\"Did you set the speech resource key and region values?\")

text = st.text_input(\"Enter text\", value=\"Hi\", max_chars=5)

def audio_output(text):

    speech_config = speechsdk.SpeechConfig(subscription=\"743ae1f5555f49f9a5de4457d4e91b2d\", region=\"australiaeast\")
    audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)

    # The language of the voice that speaks.
    speech_config.speech_synthesis_voice_name=\'en-US-JennyNeural\'

    speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

    # Get text from the console and synthesize to the default speaker.
    st.write(\"Enter some text that you want to speak >\")

    speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()

    if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
        st.write(\"Speech synthesized for text [{}]\".format(text))
    elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = speech_synthesis_result.cancellation_details
        st.write(\"Speech synthesis canceled: {}\".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            if cancellation_details.error_details:
                st.write(\"Error details: {}\".format(cancellation_details.error_details))
                st.write(\"Did you set the speech resource key and region values?\")

    recognize_from_microphone()
    audio_output(text)             

这是我的代码,但 streamlit 根本不加载函数。有什么解决办法吗?我是流线型和天蓝色的新手。

    标签: python


    【解决方案1】:

    您声明了函数但没有调用它们。

    【讨论】:

      猜你喜欢
      • 2019-03-30
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2021-02-26
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多