File size: 11,915 Bytes
617da91
 
 
 
 
 
 
 
 
 
 
 
 
1bf74ad
 
 
617da91
1bf74ad
 
617da91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bf74ad
617da91
 
1c40674
617da91
 
 
 
1bf74ad
 
617da91
1bf74ad
617da91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bf74ad
617da91
 
 
 
 
 
 
 
1bf74ad
617da91
 
 
 
1bf74ad
 
617da91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1bf74ad
 
617da91
 
 
 
 
e2058e2
1c40674
617da91
 
e2058e2
617da91
 
 
 
 
 
 
 
 
 
1c40674
 
 
 
 
 
 
 
 
 
 
 
 
 
617da91
1c40674
617da91
 
 
 
 
 
 
 
1c40674
 
617da91
 
 
 
 
 
 
 
 
 
 
 
 
ec2c0ad
617da91
 
 
 
 
 
 
 
 
ec2c0ad
 
617da91
 
ec2c0ad
617da91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec2c0ad
 
 
617da91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c40674
1bf74ad
617da91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c40674
617da91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c40674
617da91
 
 
 
ec2c0ad
617da91
1bf74ad
 
 
617da91
 
 
cd7c661
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"""
Chatbot App for Cognitive Debriefing Interview

Author: Dr Musashi Hinck

Version Log:
- 02.04.24: Initial demo with passed values from Qualtrics survey
- 07.04.24: Added configurations for survey edition

Notes:
- Need to call Request from start state
- Example URL: localhost:7860/?user=123&session=456&questionid=0&response=0

TODO:
- Test interview ending behavior: does it get triggered reliably?
- Add password protection

Pre-flight:
- Check dotenv values match Gradio secrets
"""

from __future__ import annotations

import os
import json
import logging
import gradio as gr
from uuid import uuid4
from typing import Generator, Any

from pathlib import Path


from utils import (
    PromptTemplate,
    convert_gradio_to_openai,
    initialize_client,
    load_dotenv,
    upload_azure,
    record_chat,
    ChatLoggerHandler
)


# %% Initialize common assets
base_logger = logging.getLogger(__name__)
chat_logger = ChatLoggerHandler()
if os.environ.get("AZURE_ENDPOINT") is None:  # Set Azure credentials from local files
    load_dotenv()
client = initialize_client()  # Shared across sessions
question_mapping: dict[str, str] = json.loads(Path("assets/question_mapping.json").read_text())


# %% (functions)

# Initialization
# - Record user and session id
# - Record question and response
# - Build system message
# - Build initial message
# - Wrapper - start_survey


def initialize_interview(request: gr.Request) -> tuple:
    """
    Read: Request
    Set: values of userId, sessionId, questionWording, initialMessage, systemMessage
    """
    # Parse request
    request_params = request.query_params
    user_id: str = request_params.get("user", "testUser")
    session_id: str = request_params.get("session", "testSession")
    base_logger.info(f"User: {user_id} (Session: {session_id})")

    # Parse question
    question_id: str = request_params.get("questionid", "0")
    response_id: str = request_params.get("response", "0")
    question_data: dict = json.loads(Path(f"./assets/questions/{question_mapping[question_id]}").read_text())
    question_wording: str = question_data["question"]
    question_choices: str = question_data["choices"]
    response_text: str = question_choices[int(response_id)]
    base_logger.info(f"Question: {question_wording} ({response_text})")

    # Load initial and system messages
    initial_message: str = PromptTemplate.from_file("assets/initial_message.txt").format(surveyQuestion=question_wording)
    system_message: str = PromptTemplate.from_file("assets/system_message.txt").format(surveyQuestion=question_wording, responseVal=response_text)
    base_logger.info(f"Initial message: {initial_message}")
    base_logger.info(f"System message: {system_message}")

    # Return all
    return (
        user_id,
        session_id,
        question_wording,
        initial_message,
        system_message
    )

def initialize_interface(initial_message: str) -> tuple:
    """
    Change interface to interactive mode.
    Read: initial_message
    Set:
        instruction_text: modify (to empty)
        chat_display: set initial_message
        chat_input: update placeholder, make interactive
        chat_submit: make interactive
        start_button: hide
    """
    instruction_text = gr.Markdown("")
    chat_display = gr.Chatbot(
        value=[[None, initial_message]],
        elem_id="chatDisplay",
        show_label=False,
        visible=True,
    )
    chat_input = gr.Textbox(
        placeholder="Type response here. Hit `Enter` or click the arrow to submit.",
        visible=True,
        interactive=True,
        show_label=False,
        scale=10,
    )
    chat_submit = gr.Button(
        "",
        variant="primary",
        interactive=True,
        icon="./arrow_icon.svg",
        visible=True,
    )
    start_button = gr.Button("Start Interview", visible=False, variant="primary")
    return (instruction_text, chat_display, chat_input, chat_submit, start_button)


# Interaction
# - User message
# - Bot message
# - Check if interview finished
# - Record interaction (local log)


def user_message(
    message: str, chat_history: list[list[str | None]]
) -> tuple[str, list[list[str | None]]]:
    "Display user message immediately"
    return "", chat_history + [[message, None]]


def bot_message(
    chat_history: list[list[str | None]],
    system_message: str,
    model_args: dict = {"model": "gpt-4o-default", "temperature": 0.0},
) -> Generator[Any, Any, Any]:
    "Streams response from OpenAI API to chat interface."
    # Prep messages
    user_msg = chat_history[-1][0]
    messages = convert_gradio_to_openai(chat_history[:-1])
    messages = (
        [{"role": "system", "content": system_message}]
        + messages
        + [{"role": "user", "content": user_msg}]
    )
    # API call
    response = client.chat.completions.create(
        messages=messages, stream=True, **model_args
    )
    # Streaming
    chat_history[-1][1] = ""
    for chunk in response:
        delta = chunk.choices[0].delta.content
        if delta:
            chat_history[-1][1] += delta
            yield chat_history


def log_interaction(
    chat_history: list[list[str | None]],
    session_id: str,
) -> None:
    "Record last pair of interactions"
    record_chat(chat_logger, session_id, "user", chat_history[-1][0])
    record_chat(chat_logger, session_id, "bot", chat_history[-1][1])


def interview_end_check(
    chat_history: list[list[str | None]],
    limit: int = 20,
    end_of_interview: str = "<end_of_survey>",
) -> tuple[list[list[str | None]], gr.Button, gr.Textbox, gr.Button]:
    """
    Checks if interview has completed using two conditions:
    1. If the last bot message contains `end_of_interview` (default: "<end_of_survey>". Replaced "<end_interview>" with this new default token by Kentaro)
    2. Conversation length has reached `limit` (default: 10)

    If either condition is met, the end of interview button is displayed.
    """
    flag = False
    if len(chat_history) >= limit:
        flag = True
    if end_of_interview in chat_history[-1][1]:
        chat_history[-1][1] = chat_history[-1][1].replace(end_of_interview, "")
        flag = True
    input_button = gr.Textbox(
        placeholder="Type response here. Hit `Enter` or click the arrow to submit.",
        visible= not flag,
        interactive=True,
        show_label=False,
        scale=10,
    )
    submit_button = gr.Button(
        "",
        variant="primary",
        interactive=True,
        icon="./arrow_icon.svg",
        visible= not flag,
    )
    button = gr.Button("Save and Exit", visible=flag, variant="stop")
    return chat_history, button, input_button, submit_button


# Completion
# - Create completion code
# - Append to message history
# - Display completion code


def generate_completion_code(prefix: str = "cd-") -> str:
    return prefix + str(uuid4())


def upload_interview(
    session_id: str,
    chat_history: list[list[str | None]],
) -> None:
    "Upload chat history to Azure blob storage"
    upload_azure(session_id, chat_history)


def end_interview(
    session_id: str,
    chat_history: list[list[str | None]],
) -> tuple[list[list[str | None]], gr.Text]:
    """Create completion code and display in chat interface."""
    completion_message = (
        "Thank you for participating.\n\n"
        "Your completion code is: {}\n\n"
        "Please now return to the Qualtrics survey "
        "and paste this code into the  completion "
        "code box.".format(generate_completion_code())
    )
    upload_interview(session_id, chat_history)
    EndMessage = gr.Text(completion_message, visible=True, show_label=False, scale=10)
    return chat_history, EndMessage

# LAYOUT
with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
    # Header and instructions
    gr.Markdown("# SurveyGPT Interview")
    instructionText = gr.Markdown(
        "Use this chat interface to talk to SurveyGPT.\n"
        "To start, click 'Start Interview' and follow the instructions.\n\n"
        "You can type your answer into the box below and hit 'Enter' or click the arrow to submit.\n\n"
        "The interview will end either after 2 minutes, or if the chatbot decides the interview is done.\n"
        "At this point, you will see a 'Save and Exit' button. Click this to save your responses and receive a completion code."
    )
    # Initialize empty hidden values.
    userId = gr.State()
    sessionId = gr.State()
    questionWording = gr.State()
    initialMessage = gr.State()
    systemMessage = gr.State()
    modelArgs = gr.State(value={"model": "gpt-4o-default", "temperature": 0.0})

    # Chat app (display, input, submit button)
    startButton = gr.Button("Start Interview", visible=True, variant="primary")
    chatDisplay = gr.Chatbot(
        value=None,
        elem_id="chatDisplay",
        show_label=False,
        visible=True,
    )

    EndMessage = gr.Text("", visible=False, show_label=False, scale=10)

    with gr.Row():  # Interaction
        chatInput = gr.Textbox(
            placeholder="Click 'Start Interview' to begin.",
            visible=False,
            interactive=False,
            show_label=False,
            scale=10,
        )
        chatSubmit = gr.Button(
            "",
            variant="primary",
            visible=False,
            interactive=False,
            icon="./arrow_icon.svg",
        )
    exitButton = gr.Button("Generate Completion Code", visible=False, variant="stop")
    # testExitButton = gr.Button("Save and Exit", visible=True, variant="stop")
    # Footer
    disclaimer = gr.HTML(
        """
        <div
        style='font-size: 1em;
               font-style: italic;   
               position: fixed;
               left: 50%;
               bottom: 20px;
               transform: translate(-50%, -50%);
               margin: 0 auto;
               '
        >{}</div>
        """.format(
            "Statements by the chatbot may contain factual inaccuracies."
        )
    )

    # INTERACTIONS
    # Initialization
    startButton.click(
        initialize_interview, # Reads in request params
        inputs=None,
        outputs=[
            userId,
            sessionId,
            questionWording,
            initialMessage,
            systemMessage,
        ],
    ).then(
        initialize_interface, # Changes interface to interactive mode
        inputs=[initialMessage],
        outputs=[
            instructionText,
            chatDisplay,
            chatInput,
            chatSubmit,
            startButton,
        ],
    )
    # Chat interaction
    # "Enter"
    chatInput.submit(
        user_message,
        inputs=[chatInput, chatDisplay],
        outputs=[chatInput, chatDisplay],
        queue=False,
    ).then(
        bot_message,
        inputs=[chatDisplay, systemMessage, modelArgs],
        outputs=[chatDisplay],
    ).then(
        log_interaction,
        inputs=[chatDisplay, sessionId],
    ).then(
        interview_end_check, inputs=[chatDisplay], outputs=[chatDisplay, exitButton, chatInput, chatSubmit]
    )

    # Button
    chatSubmit.click(
        user_message,
        inputs=[chatInput, chatDisplay],
        outputs=[chatInput, chatDisplay],
        queue=False,
    ).then(
        bot_message,
        inputs=[chatDisplay, systemMessage, modelArgs],
        outputs=[chatDisplay],
    ).then(
        log_interaction,
        inputs=[chatDisplay, sessionId],
    ).then(
        interview_end_check, inputs=[chatDisplay], outputs=[chatDisplay, exitButton, chatInput, chatSubmit]
    )

    # Reset button
    exitButton.click(
        end_interview, inputs=[sessionId, chatDisplay], outputs=[chatDisplay, EndMessage]
    )
    # testExitButton.click(
    #     end_interview, inputs=[sessionId, chatDisplay], outputs=[chatDisplay]
    # )


if __name__ == "__main__":
    demo.launch()#auth=auth_no_user)