File size: 3,993 Bytes
6c7823c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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("<h1>πŸ’¬ Market Brief Chat</h1>", unsafe_allow_html=True)
st.markdown("---")


# Display chat history
chat_col, audio_col = st.columns([0.85, 0.15])
with chat_col:
    c = st.container(height=400, border=True)
    with c:
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.write(message["content"])

with audio_col:
    # Voice input buttonif
    data = st.audio_input(label="🎀 Record")
    if data:
        st.session_state.um = STT(data)
    if st.button("πŸ”Š listen"):
        text = st.session_state.messages[-1]["content"]
        buffer = TTS(text)
        st.audio(data=buffer)
        st.success("Playing")

# Voice input button (beside text input conceptually)
# Handle text input
user_input = st.chat_input("Ask me about market trends...")
st.session_state.um = user_input
if st.session_state.um:
    with c:
        with st.chat_message("user"):
            st.markdown(user_input)
        st.session_state.messages.append(
            {"role": "user", "content": st.session_state.um}
        )

        data = {"query": user_input, "history": []}
        or_response = requests.post(
            url="http://127.0.0.1:8000/orchestrator/orchestrator_decision", json=data
        ).json()

        print(or_response)

        agent_response = ""
        data = {"query": user_input, "context": or_response["response"]}
        full_response = requests.post(
            url="http://127.0.0.1:8000/orchestrator/final_response",
            json=data,
            stream=True,
        )
        with st.chat_message("assistant"):
            placeholder = st.empty()
            for chunk in full_response.iter_content(
                decode_unicode=True, chunk_size=None
            ):
                agent_response += chunk
                placeholder.markdown(agent_response + "β–Œ")

        st.session_state.messages.append(
            {"role": "assistant", "content": agent_response}
        )
        st.session_state.um = None
        st.rerun()