|
| 1 | +import logging |
| 2 | +import time |
| 3 | +from os import getenv |
| 4 | + |
| 5 | +import azure.cognitiveservices.speech as speechsdk |
| 6 | +import streamlit as st |
| 7 | +from azure.cognitiveservices.speech.speech import SpeechRecognitionEventArgs |
| 8 | +from dotenv import load_dotenv |
| 9 | + |
| 10 | +load_dotenv("azure_ai_speech.env") |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | +done = False |
| 13 | + |
| 14 | + |
| 15 | +def transcript( |
| 16 | + subscription: str, |
| 17 | + region: str, |
| 18 | + speech_recognition_language: str, |
| 19 | +): |
| 20 | + speech_recognizer = speechsdk.SpeechRecognizer( |
| 21 | + speech_config=speechsdk.SpeechConfig( |
| 22 | + subscription=subscription, |
| 23 | + region=region, |
| 24 | + speech_recognition_language=speech_recognition_language, |
| 25 | + ), |
| 26 | + audio_config=speechsdk.audio.AudioConfig( |
| 27 | + use_default_microphone=True, |
| 28 | + ), |
| 29 | + ) |
| 30 | + |
| 31 | + def stop_cb(evt: SpeechRecognitionEventArgs): |
| 32 | + logger.debug(f"CLOSING on {evt}") |
| 33 | + speech_recognizer.stop_continuous_recognition() |
| 34 | + |
| 35 | + def recognized_cb(evt: SpeechRecognitionEventArgs): |
| 36 | + logger.debug(f"RECOGNIZED: {evt}") |
| 37 | + new_text = evt.result.text.strip() |
| 38 | + logger.info(new_text) |
| 39 | + # FIXME: App does not show the transcription |
| 40 | + |
| 41 | + speech_recognizer.recognizing.connect(lambda evt: logger.debug(f"RECOGNIZING: {evt}")) |
| 42 | + speech_recognizer.recognized.connect(recognized_cb) |
| 43 | + speech_recognizer.session_started.connect(lambda evt: logger.debug(f"SESSION STARTED: {evt}")) |
| 44 | + speech_recognizer.session_stopped.connect(lambda evt: logger.debug(f"SESSION STOPPED {evt}")) |
| 45 | + speech_recognizer.canceled.connect(lambda evt: logger.debug(f"CANCELED {evt}")) |
| 46 | + speech_recognizer.session_stopped.connect(stop_cb) |
| 47 | + speech_recognizer.canceled.connect(stop_cb) |
| 48 | + |
| 49 | + speech_recognizer.start_continuous_recognition() |
| 50 | + |
| 51 | + global done |
| 52 | + |
| 53 | + if st.button("Stop transcription", key="stop_transcription"): |
| 54 | + # FIXME: App does not stop transcription |
| 55 | + logger.info("Stop transcription") |
| 56 | + speech_recognizer.stop_continuous_recognition() |
| 57 | + done = True |
| 58 | + |
| 59 | + while done is False: |
| 60 | + time.sleep(0.5) |
| 61 | + |
| 62 | + |
| 63 | +def start( |
| 64 | + backend_url: str, |
| 65 | + log_level: int, |
| 66 | +): |
| 67 | + global done |
| 68 | + |
| 69 | + logger.setLevel(log_level) |
| 70 | + logger.debug(f"set log level to {log_level}") |
| 71 | + |
| 72 | + st.write("Transcription") |
| 73 | + |
| 74 | + if st.button("Start transcription", key="start_transcription"): |
| 75 | + logger.info("Start transcription...") |
| 76 | + done = False |
| 77 | + try: |
| 78 | + with st.spinner("Transcribing..."): |
| 79 | + transcript( |
| 80 | + subscription=getenv("AZURE_AI_SPEECH_SUBSCRIPTION_KEY"), |
| 81 | + region=getenv("AZURE_AI_SPEECH_REGION"), |
| 82 | + speech_recognition_language=getenv("AZURE_AI_SPEECH_RECOGNITION_LANGUAGE"), |
| 83 | + ) |
| 84 | + except Exception as e: |
| 85 | + st.write(f"Error: {e}") |
| 86 | + logger.error(f"Error: {e}") |
0 commit comments