[Python] Python streamlit realtime speech-to-text with azure SDK

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    Hello I'm trying to create a real-time speech to text using streamlit and azure speech SDK. I can easilly transcribe audio/video files with no issues, but I want to integrate realtime transcription (from browser)

    In the code I've tryed when I speak in the micropghone no sentence is transcribed

    I've also tryed to reuse the function I've created that uses files, pass the AudioStream and turn it async but didn't work it either also the guided path: https://microsoft.github.io/TechExc...cs/04_implement_audio_transcription/0402.html Only works on local machine because it uses host's microphone

    I've tryed with the code posted blow, search on google and ask AI. I want the user to be able to start live speech-to-text with live transcription in the chat (speaker recognition must stay)

    Code:

    webrtc_ctx = webrtc_streamer(key="speech-to-text", mode=WebRtcMode.SENDONLY,
    media_stream_constraints={"video": False, "audio": True})

    stream= PushAudioInputStream()

    audio_input = speechsdk.AudioConfig(stream=stream)
    speech_config = speechsdk.SpeechConfig(env["SPEECH_KEY"], env["SPEECH_REGION"])
    #other settings
    conversation_transcriber = speechsdk.transcription.ConversationTranscriber(speech_config, audio_input)

    stream_placeholder = None; done = False; r = []

    def stop_cb(evt: ConversationTranscriptionEventArgs):
    conversation_transcriber.stop_transcribing_async()
    logger.info(evt)
    if evt.result.reason == speechsdk.ResultReason.Canceled:
    canc = evt.result.cancellation_details
    logger.warning(canc)
    if canc.reason == speechsdk.CancellationReason.Error:
    logger.error("Error details: {}".format(canc.error_details))
    raise Exception("Error details: {}".format(canc.error_details))

    global done
    done = True

    def addsentence(evt: ConversationTranscriptionEventArgs):
    global r
    if evt.result.speaker_id == "Unknown":
    logger.debug("Unknown speaker: " + str(evt))
    return
    logger.debug(f"Detected **{evt.result.speaker_id}**: {evt.result.text}")
    r.append(f"**{evt.result.speaker_id}**: {evt.result.text}")

    conversation_transcriber.transcribed.connect(addsentence)
    conversation_transcriber.session_stopped.connect(stop_cb)
    conversation_transcriber.canceled.connect(stop_cb)

    while webrtc_ctx.state.playing:
    if not st.session_state["recording"]:
    with st.chat_message("assistant"):
    with st.spinner("Starting transcription..."):
    stream_placeholder = st.expander("Transcription", icon="").empty()

    conversation_transcriber.start_transcribing_async()
    logger.info("Transcribing started!")
    st.session_state["recording"] = True

    logger.debug("Receiving frame from browser...")
    try:
    audio_frames = webrtc_ctx.audio_receiver.get_frames(timeout=1)
    except queue.Empty:
    time.sleep(0.1)
    logger.debug("No frame arrived.")
    continue

    stream_placeholder.markdown("## Transcription:\n\n" + "\\\n".join(r))

    for audio_frame in audio_frames:
    stream.write(audio_frame.to_ndarray().tobytes())

    Continue reading...

Compartilhe esta Página