Langdon commited on
Commit
9f60a77
β€’
1 Parent(s): 35d6db8
Files changed (2) hide show
  1. .devcontainer/devcontainer.json +26 -0
  2. app.py +115 -71
.devcontainer/devcontainer.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the
2
+ // README at: https://github.com/devcontainers/templates/tree/main/src/python
3
+ {
4
+ "name": "Python 3",
5
+ // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6
+ "image": "mcr.microsoft.com/devcontainers/python:1-3.10-bookworm",
7
+ "features": {
8
+ "ghcr.io/devcontainers/features/git:1": {},
9
+ "ghcr.io/devcontainers/features/github-cli:1": {}
10
+ }
11
+
12
+ // Features to add to the dev container. More info: https://containers.dev/features.
13
+ // "features": {},
14
+
15
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
16
+ // "forwardPorts": [],
17
+
18
+ // Use 'postCreateCommand' to run commands after the container is created.
19
+ // "postCreateCommand": "pip3 install --user -r requirements.txt",
20
+
21
+ // Configure tool-specific properties.
22
+ // "customizations": {},
23
+
24
+ // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
25
+ // "remoteUser": "root"
26
+ }
app.py CHANGED
@@ -3,26 +3,25 @@ import pandas as pd
3
  import gspread
4
  from google.auth import default
5
  import requests
6
- import time
7
  import json
 
8
 
9
  # Authenticate and authorize with Google Sheets
10
- #creds, _ = default()
11
- #gc = gspread.authorize(creds)
12
- gc = gspread.service_account(filename='botresponse-6f1a8c749aa0.json')
13
-
14
  # Specify your Google Sheets credentials, sheet_id, and worksheet_name
15
  sheet_id = "18hnoTsEaGMWMael42MXubb-FzAe5jJB5RpaSolIXyb0"
16
  worksheet_name = "Sheet1"
17
 
 
18
  # Function to get the initial response
19
  def get_initial_response(input_message):
20
  url = "https://itell-api.learlab.vanderbilt.edu/chat"
21
 
22
- payload = {
23
- "textbook_name": "think-python-2e",
24
- "message": input_message
25
- }
26
  headers = {"Content-Type": "application/json"}
27
 
28
  # Measure the start time
@@ -30,81 +29,126 @@ def get_initial_response(input_message):
30
 
31
  response = requests.post(url, json=payload, headers=headers)
32
  data = json.loads(response.text)
33
- message = data['message']
 
 
 
34
 
35
  # Calculate the elapsed time
36
  elapsed_time = time.time() - start_time
37
  elapsed_time = round(elapsed_time, 2)
38
- response_time_message = f"Response time: {elapsed_time} seconds"
39
 
40
- return message, response_time_message # Return the initial_response and elapsed_time
 
 
 
 
41
 
42
  # Function to collect feedback and update the spreadsheet
43
- def feedback_response(input_message, feedback, comments):
44
- # Get the initial response and elapsed_time
45
- initial_response, elapsed_time = get_initial_response(input_message)
46
-
47
- # Collect feedback
48
- response = ''
49
- if feedback == 'Informative':
50
- response = 'Informative'
51
- elif feedback == 'Inaccurate':
52
- response = 'Inaccurate'
53
- elif feedback == 'Nonsense':
54
- response = 'Nonsense'
55
- else:
56
- response = 'NA'
57
 
58
  # Update Google Sheets
59
  sh = gc.open_by_key(sheet_id)
60
  worksheet = sh.worksheet(worksheet_name)
61
-
62
- thankyou = "Thank you for your feedback. Your response and feedback has been recorded!"
63
- # Append the data to the worksheet only if comments have a value
64
- if comments:
65
- # Create a DataFrame from the response and additional comments
66
- df = pd.DataFrame({'Input Message': [input_message], 'Output': [initial_response], 'Time took in Seconds': [elapsed_time],'Feedback': [response], 'Additional Comments': [comments]})
67
-
68
- # Append the data to the worksheet
69
- worksheet.append_rows(df.values.tolist())
70
- initial_response = thankyou
71
-
72
-
73
- return initial_response, elapsed_time # Return the initial_response and elapsed_time
74
-
75
- # Set up the Gradio Interface
76
- feedback_interface = gr.Interface(
77
- fn=feedback_response,
78
- inputs=[
79
- gr.Textbox(label="Input Message"),
80
- gr.Radio(
81
- choices=[
82
- "Informative",
83
- "Inaccurate",
84
- "Nonsense"
85
- ],
86
- label="Feedback"),
87
- gr.Textbox(label="Additional Comments")
88
- ],
89
- outputs=[
90
- gr.Textbox(label="Output", type="text"),
91
- gr.Textbox(label="Elapsed Time (s)", type="text") # Change the type to "text" for two decimal places
92
- ],
93
- title="Beta: Itell Guide Response Bot",
94
- description="""
95
  # Introduction
96
- This is an interface to test iTELL's guide on the side. Please be aware that responses can take up to 20 seconds
97
  # Step by Step Introduction
98
- 1. Place a question in the input message textbox
99
- 2. Wait roughly 10 ~ 20 seconds for the response and elapsed time to come out on the right
100
- 3. After looking at the results, click on the feedback options that best describes the output: Informative, Inaccurate, Nonsense
101
- 4. After choosing the best option, write down additional comments for more feedback
102
- 5. Press submit again and wait for the output to come out again for your feedback to be submitted
103
- 6. Once the "Thank you for your feedback. Your response and feedback has been recorded!" message is shown, you are done
104
- ** DISCLAIMER: You have to type a input message, click on the feedback buttons, and type in additional comments for your responses to be recorded
105
- ** For further questions contact LEAR Labs!
106
  """
107
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  # Launch the interface
110
- feedback_interface.launch()
 
3
  import gspread
4
  from google.auth import default
5
  import requests
6
+ import time
7
  import json
8
+ import re
9
 
10
  # Authenticate and authorize with Google Sheets
11
+ # creds, _ = default()
12
+ # gc = gspread.authorize(creds)
13
+ gc = gspread.service_account(filename="botresponse-6f1a8c749aa0.json")
14
+
15
  # Specify your Google Sheets credentials, sheet_id, and worksheet_name
16
  sheet_id = "18hnoTsEaGMWMael42MXubb-FzAe5jJB5RpaSolIXyb0"
17
  worksheet_name = "Sheet1"
18
 
19
+
20
  # Function to get the initial response
21
  def get_initial_response(input_message):
22
  url = "https://itell-api.learlab.vanderbilt.edu/chat"
23
 
24
+ payload = {"textbook_name": "think-python-2e", "message": input_message}
 
 
 
25
  headers = {"Content-Type": "application/json"}
26
 
27
  # Measure the start time
 
29
 
30
  response = requests.post(url, json=payload, headers=headers)
31
  data = json.loads(response.text)
32
+ message = data["message"]
33
+ message = message.encode("utf-8").decode("unicode_escape")
34
+ message = re.sub(r"^[\s'\"]*(bot|cite sources)?[\s'\"]*", "", message)
35
+ message = message.strip(" ':\"")
36
 
37
  # Calculate the elapsed time
38
  elapsed_time = time.time() - start_time
39
  elapsed_time = round(elapsed_time, 2)
 
40
 
41
+ return {
42
+ bot_resp: message,
43
+ response_time: elapsed_time,
44
+ }
45
+
46
 
47
  # Function to collect feedback and update the spreadsheet
48
+ def feedback_response(
49
+ input_message,
50
+ bot_message,
51
+ response_time,
52
+ feedback_primary,
53
+ feedback_secondary,
54
+ comments,
55
+ ):
56
+ feedback = []
57
+ feedback.append(feedback_primary)
58
+ feedback.extend(feedback_secondary)
 
 
 
59
 
60
  # Update Google Sheets
61
  sh = gc.open_by_key(sheet_id)
62
  worksheet = sh.worksheet(worksheet_name)
63
+
64
+ # Create a DataFrame from the response and additional comments
65
+ df = pd.DataFrame(
66
+ {
67
+ "Input Message": [input_message],
68
+ "Output": [bot_message],
69
+ "Time took in Seconds": [response_time],
70
+ "Feedback": [str(feedback)],
71
+ "Additional Comments": [comments],
72
+ }
73
+ )
74
+
75
+ # Append the data to the worksheet
76
+ worksheet.append_rows(df.values.tolist())
77
+ gr.Info("Feedback Submitted")
78
+ return {
79
+ primary_feedback: None,
80
+ secondary_feedback: None,
81
+ additional_comments: None,
82
+ }
83
+
84
+
85
+ with gr.Blocks(title="iTELL Chat Feedback") as feedback_interface:
86
+ title = "iTELL Chat Safety Testing"
87
+ gr.components.Markdown(
88
+ f"<h1 style='text-align: center; margin-bottom: 1rem'>{title}</h1>"
89
+ )
90
+ gr.Markdown(
91
+ """
 
 
 
 
 
92
  # Introduction
93
+ This is an interface to test iTELL's guide on the side. Please be aware that responses can take up to 20 seconds.
94
  # Step by Step Introduction
95
+ 1. Place a question in the input message textbox.
96
+ 2. Wait 10 ~ 20 seconds for the response to appear on the right.
97
+ 3. After looking at the results, provide primary feedback on the response.
98
+ 4. If desired, add secondary feedback selections: Informative, Inaccurate, Nonsense.
99
+ 4. Write down additional comments for more feedback.
100
+ 5. Press "Submit Feedback".
 
 
101
  """
102
+ )
103
+ elapsed_time = gr.State(0)
104
+ with gr.Row():
105
+ with gr.Column():
106
+ usr_msg = gr.Textbox(interactive=True, label="Input Message", lines=7)
107
+ response_time = gr.Number(label="Response Time", step=0.01)
108
+ bot_resp = gr.Textbox(label="Output", type="text", lines=10)
109
+ with gr.Row():
110
+ clear_btn = gr.ClearButton(usr_msg, value="Clear")
111
+ submit_btn = gr.Button("Submit")
112
+ # submit_btn.click(get_initial_response, inputs=usr_msg, outputs=bot_resp)
113
+ with gr.Column():
114
+ primary_feedback = gr.Radio(
115
+ ["πŸ‘ good", "πŸ‘Ž bad", "❌ inappropriate"], label="Primary Feedback"
116
+ )
117
+ secondary_feedback = gr.CheckboxGroup(
118
+ [
119
+ "🧠 Informative",
120
+ "πŸ™… Inaccurate",
121
+ "❓ Nonsense",
122
+ "🩢 Character Encoding Error",
123
+ ],
124
+ label="Secondary Feedback",
125
+ )
126
+ additional_comments = gr.Textbox(
127
+ label="Additional Comments", interactive=True, lines=7
128
+ )
129
+ feedback_btn = gr.Button("Submit Feedback")
130
+ feedback_btn.click(
131
+ feedback_response,
132
+ inputs=[
133
+ usr_msg,
134
+ bot_resp,
135
+ response_time,
136
+ primary_feedback,
137
+ secondary_feedback,
138
+ additional_comments,
139
+ ],
140
+ outputs=[
141
+ primary_feedback,
142
+ secondary_feedback,
143
+ additional_comments
144
+ ],
145
+ )
146
+ gr.on(
147
+ triggers=[submit_btn.click, usr_msg.submit],
148
+ fn=get_initial_response,
149
+ inputs=usr_msg,
150
+ outputs=[bot_resp, response_time],
151
+ )
152
 
153
  # Launch the interface
154
+ feedback_interface.launch()