File size: 4,657 Bytes
c5db237
 
 
 
b1bfc93
959499b
63eb699
c5db237
adb5430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
591bd65
959499b
0010262
 
 
 
2ccbe74
 
 
 
 
 
 
 
 
4845420
0010262
 
 
8439fac
4845420
40e6d96
1e4655b
 
 
 
959499b
1e4655b
 
 
959499b
1e4655b
 
959499b
 
 
 
 
1e4655b
0010262
 
 
4845420
3302ff9
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
import gradio as gr
import pandas as pd
import gspread
from google.auth import default
import requests
import time 
import json

# 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']

    # Calculate the elapsed time
    elapsed_time = time.time() - start_time
    elapsed_time = round(elapsed_time, 2)
    response_time_message = f"Response time: {elapsed_time} seconds"

    return message, response_time_message  # Return the initial_response and elapsed_time

# Function to collect feedback and update the spreadsheet
def feedback_response(input_message, feedback, add_feedback, comments):
    # Get the initial response and elapsed_time
    initial_response, elapsed_time = get_initial_response(input_message)

    # Collect feedback
    response = ''
    if feedback == 'Good':
        response = 'Good'
    elif feedback == 'Bad':
        response = 'Bad'
    elif feedback == 'Inappropriate':
        response = 'Inappropriate'
    else:
        response = 'NA'
    
    # More feedback
    # Collect feedback
    add_response = ''
    if add_feedback == 'Informative':
        add_response = 'Informative'
    elif add_feedback == 'Inaccurate':
        add_response = 'Inaccurate'
    elif add_feedback == 'Nonsense':
        add_response = 'Nonsense'
    else:
        add_response = 'NA'

    # Update Google Sheets
    sh = gc.open_by_key(sheet_id)
    worksheet = sh.worksheet(worksheet_name)
    
    thankyou = "Thank you for your feedback. Your response and feedback has been recorded!"
    # Append the data to the worksheet only if comments have a value
    if comments:
        # Create a DataFrame from the response and additional comments
        df = pd.DataFrame({'Input Message': [input_message], 'Output': [initial_response], 'Time took in Seconds': [elapsed_time],'Feedback': [response], 'add_feedback': [add_response], 'Additional Comments': [comments]})

        # Append the data to the worksheet
        worksheet.append_rows(df.values.tolist())
        initial_response = thankyou
    
    return initial_response, elapsed_time  # Return the initial_response and elapsed_time

# Set up the Gradio Interface
feedback_interface = gr.Interface(
    fn=feedback_response,
    inputs=[
        gr.Textbox(label="Input Message"),
        gr.Row(
            gr.Radio("Good", label="Good"),
            gr.Radio("Bad", label="Bad"),
            gr.Radio("Inappropriate", label="Inappropriate")
        ),
        gr.Row(
            gr.Radio("Informative", label="Informative"),
            gr.Radio("Inaccurate", label="Inaccurate"),
            gr.Radio("Nonsense", label="Nonsense")
        ),
        gr.Textbox(label="Additional Comments")
    ],
    outputs=[
        gr.Textbox(label="Output", type="text"),
        gr.Textbox(label="Elapsed Time (s)", type="text")
    ],
    title="Beta: Itell Guide Response Bot",
    description="""
    # 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 roughly 10 ~ 20 seconds for the response and elapsed time to come out on the right
    3. After looking at the results, click on the feedback options that best describes the output: Informative, Inaccurate, Nonsense
    4. After choosing the best option, write down additional comments for more feedback
    5. Press submit again and wait for the output to come out again for your feedback to be submitted
    6. Once the "Thank you for your feedback. Your response and feedback has been recorded!" message is shown, you are done

    ** DISCLAIMER: You have to type a input message, click on the feedback buttons, and type in additional comments for your responses to be recorded

    ** For further questions contact LEAR Labs!
    """
)

# Launch the interface
feedback_interface.launch()