|
| 1 | +import tempfile |
| 2 | +from os import getenv |
| 3 | + |
| 4 | +import streamlit as st |
| 5 | +from audio_recorder_streamlit import audio_recorder |
| 6 | +from dotenv import load_dotenv |
| 7 | +from langchain_community.document_loaders.parsers.audio import AzureOpenAIWhisperParser |
| 8 | +from langchain_core.documents.base import Blob |
| 9 | + |
| 10 | +from template_langgraph.loggers import get_logger |
| 11 | + |
| 12 | +load_dotenv(override=True) |
| 13 | +logger = get_logger(__name__) |
| 14 | +logger.setLevel("DEBUG") |
| 15 | + |
| 16 | +with st.sidebar: |
| 17 | + "# Common Settings" |
| 18 | + azure_openai_endpoint = st.text_input( |
| 19 | + label="AZURE_OPENAI_ENDPOINT", |
| 20 | + value=getenv("AZURE_OPENAI_ENDPOINT"), |
| 21 | + key="AZURE_OPENAI_ENDPOINT", |
| 22 | + type="default", |
| 23 | + ) |
| 24 | + azure_openai_api_key = st.text_input( |
| 25 | + label="AZURE_OPENAI_API_KEY", |
| 26 | + value=getenv("AZURE_OPENAI_API_KEY"), |
| 27 | + key="AZURE_OPENAI_API_KEY", |
| 28 | + type="password", |
| 29 | + ) |
| 30 | + azure_openai_api_version = st.text_input( |
| 31 | + label="AZURE_OPENAI_API_VERSION", |
| 32 | + value=getenv("AZURE_OPENAI_API_VERSION"), |
| 33 | + key="AZURE_OPENAI_API_VERSION", |
| 34 | + type="default", |
| 35 | + ) |
| 36 | + azure_openai_model_stt = st.text_input( |
| 37 | + label="AZURE_OPENAI_MODEL_STT", |
| 38 | + value=getenv("AZURE_OPENAI_MODEL_STT"), |
| 39 | + key="AZURE_OPENAI_MODEL_STT", |
| 40 | + type="default", |
| 41 | + ) |
| 42 | + "### Documents" |
| 43 | + "[Azure OpenAI Whisper Parser](https://python.langchain.com/docs/integrations/document_loaders/parsers/azure_openai_whisper_parser/)" |
| 44 | + |
| 45 | +st.title("🎤 Batch Transcription Playground") |
| 46 | + |
| 47 | +audio_bytes = audio_recorder( |
| 48 | + text="Click to record", |
| 49 | + recording_color="red", |
| 50 | + neutral_color="gray", |
| 51 | + icon_name="microphone", |
| 52 | + icon_size="6x", |
| 53 | +) |
| 54 | + |
| 55 | +if audio_bytes: |
| 56 | + st.audio(audio_bytes, format="audio/wav") |
| 57 | + with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file: |
| 58 | + temp_audio_file.write(audio_bytes) |
| 59 | + temp_audio_file_path = temp_audio_file.name |
| 60 | + st.write(f"Audio saved to temporary file: {temp_audio_file_path}") |
| 61 | + |
| 62 | +if st.button("Transcribe", disabled=audio_bytes is None): |
| 63 | + with st.spinner("Transcribing..."): |
| 64 | + try: |
| 65 | + audio_blob = Blob(path=temp_audio_file_path) |
| 66 | + parser = AzureOpenAIWhisperParser( |
| 67 | + api_key=azure_openai_api_key, |
| 68 | + azure_endpoint=azure_openai_endpoint, |
| 69 | + api_version=azure_openai_api_version, |
| 70 | + deployment_name=azure_openai_model_stt, |
| 71 | + ) |
| 72 | + documents = parser.lazy_parse( |
| 73 | + blob=audio_blob, |
| 74 | + ) |
| 75 | + results = [doc.page_content for doc in documents] |
| 76 | + st.success("Transcription completed!") |
| 77 | + st.text_area("Transcription Result", value="\n".join(results), height=200) |
| 78 | + except Exception as e: |
| 79 | + logger.error(f"Error during transcription: {e}") |
| 80 | + st.error(f"Error during transcription: {e}") |
0 commit comments