File size: 4,971 Bytes
6554910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import gspread
from google.auth import default
import requests
import time
import json
import re

# Authenticate and authorize with Google Sheets
# creds, _ = default()
# gc = gspread.authorize(creds)
gc = gspread.service_account(filename="botresponse-6f1a8c749aa0.json")

# Specify your Google Sheets credentials, sheet_id, and worksheet_name
sheet_id = "18hnoTsEaGMWMael42MXubb-FzAe5jJB5RpaSolIXyb0"
worksheet_name = "Sheet1"


# Function to get the initial response
def get_initial_response(input_message):
    url = "https://itell-api.learlab.vanderbilt.edu/chat"

    payload = {"textbook_name": "think-python-2e", "message": input_message}
    headers = {"Content-Type": "application/json"}

    # Measure the start time
    start_time = time.time()

    response = requests.post(url, json=payload, headers=headers)
    data = json.loads(response.text)
    message = data["message"]
    message = message.encode("utf-8").decode("unicode_escape")
    message = re.sub(r"^[\s'\"]*(bot|cite sources)?[\s'\"]*", "", message)
    message = message.strip(" ':\"")

    # Calculate the elapsed time
    elapsed_time = time.time() - start_time
    elapsed_time = round(elapsed_time, 2)

    return {
        bot_resp: message,
        response_time: elapsed_time,
    }


# Function to collect feedback and update the spreadsheet
def feedback_response(
    input_message,
    bot_message,
    response_time,
    feedback_primary,
    feedback_secondary,
    comments,
):
    # Update Google Sheets
    sh = gc.open_by_key(sheet_id)
    worksheet = sh.worksheet(worksheet_name)

    # Create a DataFrame from the response and additional comments
    df = pd.DataFrame(
        {
            "Input Message": [input_message],
            "Output": [bot_message],
            "Time took in Seconds": [response_time],
            "Initial Feedback": [feedback_primary],
            "Secondary Feedback": [str(feedback_secondary)],
            "Additional Comments": [comments],
        }
    )

    # Append the data to the worksheet
    worksheet.append_rows(df.values.tolist())
    gr.Info("Feedback Submitted")
    return {
        primary_feedback: None,
        secondary_feedback: None,
        additional_comments: None,
    }


with gr.Blocks(title="iTELL Chat Feedback") as feedback_interface:
    title = "Itell Guide Response Bot"
    gr.components.Markdown(
        f"<h1 style='text-align: center; margin-bottom: 1rem'>{title}</h1>"
    )
    gr.Markdown(
        """
    # Introduction
    This is an interface to test iTELL's guide on the side. Please be aware that responses can take up to 20 seconds.
    # Step by Step Introduction
    1. Place a question in the input message textbox.
    2. Wait 10 ~ 20 seconds for the response to appear on the right.
    3. After looking at the results, provide primary feedback on the response.
    4. If desired, add secondary feedback selections: Informative, Inaccurate, Nonsense.
    4. Write down additional comments for more feedback.
    5. Press "Submit Feedback".
    """
    )
    elapsed_time = gr.State(0)
    with gr.Row():
        with gr.Column():
            usr_msg = gr.Textbox(interactive=True, label="Input Message", lines=7)
            response_time = gr.Number(label="Response Time", step=0.01)
            bot_resp = gr.Textbox(label="Output", type="text", lines=10)
            with gr.Row():
                clear_btn = gr.ClearButton(usr_msg, value="Clear")
                submit_btn = gr.Button("Submit")
                # submit_btn.click(get_initial_response, inputs=usr_msg, outputs=bot_resp)
        with gr.Column():
            primary_feedback = gr.Radio(
                ["πŸ‘ good", "πŸ‘Ž bad", "❌ inappropriate"], label="Primary Feedback"
            )
            secondary_feedback = gr.CheckboxGroup(
                [
                    "🧠 Informative",
                    "πŸ™… Inaccurate",
                    "❓ Nonsense",
                    "🩢 Character Encoding Error",
                ],
                label="Secondary Feedback",
            )
            additional_comments = gr.Textbox(
                label="Additional Comments", interactive=True, lines=7
            )
            feedback_btn = gr.Button("Submit Feedback")
            feedback_btn.click(
                feedback_response,
                inputs=[
                    usr_msg,
                    bot_resp,
                    response_time,
                    primary_feedback,
                    secondary_feedback,
                    additional_comments,
                ],
                outputs=[
                    primary_feedback,
                    secondary_feedback,
                    additional_comments
                    ],
            )
    submit_btn.click(
        fn=get_initial_response,
        inputs=usr_msg,
        outputs=[bot_resp, response_time],
    )

# Launch the interface
feedback_interface.launch()