File size: 7,151 Bytes
e9402b5
8573823
 
 
 
 
89ed0ae
e9402b5
 
8573823
 
 
e9402b5
 
8573823
 
 
 
 
 
 
 
 
 
 
 
40973e9
8573823
 
 
 
 
e9402b5
 
 
8573823
 
 
 
e9402b5
 
 
 
 
8573823
 
 
e9402b5
8573823
 
 
 
 
 
 
 
 
 
 
e9402b5
8573823
 
e9402b5
8573823
89ed0ae
8573823
 
 
e9402b5
 
 
 
 
 
8573823
 
 
 
 
e9402b5
 
 
 
8573823
 
 
e9402b5
 
 
 
 
 
 
 
 
 
 
 
8573823
e9402b5
 
 
 
 
 
8573823
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9402b5
 
8573823
e9402b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8573823
e9402b5
 
 
8573823
e9402b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8573823
e9402b5
 
 
 
 
 
 
 
 
8573823
 
 
 
 
 
 
 
 
 
 
 
 
e9402b5
 
8573823
 
 
 
 
e9402b5
 
 
 
 
 
 
8573823
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import numpy as np
import streamlit as st

from src.content.common import (
    MODEL_NAMES,
    AUDIO_SAMPLES_W_INSTRUCT, 
    PLAYGROUND_DIALOGUE_STATES,
    reset_states,
    update_voice_instruction_state,
    init_state_section,
    header_section,
    sidebar_fragment,
    successful_example_section,
    audio_attach_dialogue,
    retrive_response_with_ui
)


QUICK_ACTIONS = [
    {
        "name": "**Summary**",
        "instruction": "Please summarise this speech.",
        "width": 10, 
    },
    {
        "name": "**Transcript**",
        "instruction": "Please transcribe the speech",
        "width": 9.5,
    }
]


PG_CONVERSATION_STATES = dict(
    pg_messages=[],
)


@st.fragment
def select_model_variants_fradment():
    display_mapper = {
        value["vllm_name"]: value["ui_name"] 
        for key, value in MODEL_NAMES.items()
        if "audiollm" in key
        }

    st.selectbox(
        label=":fire: Explore more MERaLiON-AudioLLM variants!",
        options=list(display_mapper.keys()),
        index=0,
        format_func=lambda o: display_mapper[o],
        key="pg_model_name",
        placeholder=":fire: Explore more MERaLiON-AudioLLM variants!",
        disabled=st.session_state.disprompt,
    )


def bottom_input_section():
    select_model_variants_fradment()

    bottom_cols = st.columns([0.03, 0.03, 0.91, 0.03])
    with bottom_cols[0]:
        st.button(
            ':material/delete:', 
            disabled=st.session_state.disprompt,
            on_click=lambda: reset_states(PLAYGROUND_DIALOGUE_STATES)
        )

    with bottom_cols[1]:
        if st.button(":material/add:", disabled=st.session_state.disprompt):
            audio_attach_dialogue(
                audio_array_state="pg_audio_array",
                audio_base64_state="pg_audio_base64",
                restore_state=PG_CONVERSATION_STATES
            )

    with bottom_cols[2]:
        if chat_input := st.chat_input(
            placeholder="Instruction...", 
            disabled=st.session_state.disprompt, 
            on_submit=lambda: st.session_state.update(
                disprompt=True, 
                **PG_CONVERSATION_STATES
            )
        ):
            st.session_state.new_prompt = chat_input

    with bottom_cols[3]:
        uploaded_voice = st.audio_input(
            label="voice_instruction",
            label_visibility="collapsed", 
            disabled=st.session_state.disprompt, 
            on_change=lambda: st.session_state.update(
                disprompt=True,
                on_record_voice_instruction=True,
                **PG_CONVERSATION_STATES
                ),
            key='voice_instruction'  
        )

        if uploaded_voice and st.session_state.on_record_voice_instruction:
            voice_bytes = uploaded_voice.read()
            update_voice_instruction_state(voice_bytes)
            st.session_state.on_record_voice_instruction = False

        
@st.fragment
def quick_actions_fragment():
    action_cols_spec = [_["width"] for _ in QUICK_ACTIONS]
    action_cols = st.columns(action_cols_spec)

    for idx, action in enumerate(QUICK_ACTIONS):
        action_cols[idx].button(
            action["name"], 
            args=(action["instruction"],),
            disabled=st.session_state.disprompt, 
            on_click=lambda p: st.session_state.update(
                disprompt=True, 
                pg_messages=[],
                new_prompt=p, 
                on_select_quick_action=True
            )
        )
    
    if st.session_state.on_select_quick_action:
        st.session_state.on_select_quick_action = False
        st.rerun(scope="app")


def conversation_section():
    if st.session_state.pg_audio_array.size:
        with st.chat_message("user"):
            st.audio(st.session_state.pg_audio_array, format="audio/wav", sample_rate=16000)
            quick_actions_fragment()

    for message in st.session_state.pg_messages:
        with st.chat_message(message["role"]):
            if message.get("error"):
                st.error(message["error"])
            for warning_msg in message.get("warnings", []):
                st.warning(warning_msg)
            if message.get("content"):
                st.write(message["content"])
    
    with st._bottom:
        bottom_input_section()

    if (not st.session_state.new_prompt) and (not st.session_state.new_vi_base64):
        return
    
    one_time_prompt = st.session_state.new_prompt
    one_time_vi_array = st.session_state.new_vi_array
    one_time_vi_base64 = st.session_state.new_vi_base64

    st.session_state.update(
        new_prompt="", 
        new_vi_array=np.array([]),
        new_vi_base64="",
        pg_messages=[]
    )

    with st.chat_message("user"):
        if one_time_vi_base64:
            with st.spinner("Transcribing..."):
                error_msg, warnings, one_time_prompt = retrive_response_with_ui(
                    model_name=MODEL_NAMES["audiollm"]["vllm_name"],
                    text_input="Write out the dialogue as text.", 
                    array_audio_input=one_time_vi_array,
                    base64_audio_input=one_time_vi_base64,
                    stream=False,
                    normalise_response=True
                )
        else:
            error_msg, warnings = "", []
            st.write(one_time_prompt)

    st.session_state.pg_messages.append({
        "role": "user", 
        "error": error_msg,
        "warnings": warnings, 
        "content": one_time_prompt
    })

    with st.chat_message("assistant"):
        with st.spinner("Thinking..."):
            error_msg, warnings, response = retrive_response_with_ui(
                model_name=st.session_state.pg_model_name,
                text_input=one_time_prompt, 
                array_audio_input=st.session_state.pg_audio_array,
                base64_audio_input=st.session_state.pg_audio_base64, 
                stream=True
            )

    st.session_state.pg_messages.append({
        "role": "assistant", 
        "error": error_msg,
        "warnings": warnings, 
        "content": response
    })

    st.session_state.disprompt=False
    st.rerun(scope="app")


def playground_page():
    init_state_section()
    header_section(
        component_name="Playground",
        description=""" It is tailored for Singapore’s multilingual and multicultural landscape.
        MERaLiON-AudioLLM supports 
        <strong>Automatic Speech Recognition</strong>, 
        <strong>Speech Translation</strong>, 
        <strong>Spoken Question Answering</strong>,
        <strong>Spoken Dialogue Summarization</strong>, 
        <strong>Speech Instruction</strong>, and 
        <strong>Paralinguistics</strong> tasks.""",
        concise_description=""
        )

    with st.sidebar:
        sidebar_fragment()

    audio_sample_names = [name for name in AUDIO_SAMPLES_W_INSTRUCT.keys()]
    successful_example_section(
        audio_sample_names, 
        audio_array_state="pg_audio_array",
        audio_base64_state="pg_audio_base64",
        restore_state=PG_CONVERSATION_STATES
    )
    conversation_section()