Spaces:
Runtime error
Runtime error
Langdon
commited on
Commit
β’
9f60a77
1
Parent(s):
35d6db8
update
Browse files- .devcontainer/devcontainer.json +26 -0
- 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=
|
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[
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Function to collect feedback and update the spreadsheet
|
43 |
-
def feedback_response(
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
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 |
-
|
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 |
-
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
|
100 |
-
3. After looking at the results,
|
101 |
-
4.
|
102 |
-
|
103 |
-
|
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()
|