import streamlit as st import requests from deepgram import ( DeepgramClient, DeepgramClientOptions, PrerecordedOptions, FileSource, ) from io import BytesIO import os # Page configuration st.set_page_config(page_title="Market Brief Chat", page_icon="💬", layout="wide") DG_API = os.getenv("DG_API") # Initialize session state for chat history if "messages" not in st.session_state: st.session_state.messages = [ {"role": "assistant", "content": "How can I help you today?"} ] if "um" not in st.session_state: st.session_state.um = None # Initialize other session state variables if "is_recording" not in st.session_state: st.session_state.is_recording = False def STT(buffer): config: DeepgramClientOptions = DeepgramClientOptions(api_key=DG_API) deepgram: DeepgramClient = DeepgramClient("", config) payload: FileSource = { "buffer": buffer, } # STEP 2: Configure Deepgram options for audio analysis options = PrerecordedOptions( model="nova-3", smart_format=True, ) # STEP 3: Call the transcribe_file method with the text payload and options response = deepgram.listen.rest.v("1").transcribe_file(payload, options) data = response.to_json() transcript = data["results"]["channels"][0]["alternatives"][0]["transcript"] return transcript def TTS(text): DEEPGRAM_URL = "https://api.deepgram.com/v1/speak?model=aura-2-thalia-en" DEEPGRAM_API_KEY = DG_API payload = {"text": text} headers = { "Authorization": f"Token {DEEPGRAM_API_KEY}", "Content-Type": "application/json", } # Create a BytesIO buffer to store audio audio_buffer = BytesIO() response = requests.post(DEEPGRAM_URL, headers=headers, json=payload) audio_buffer.write(response.content) # Move cursor to the beginning of the buffer audio_buffer.seek(0) return audio_buffer # App title st.markdown("