Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,18 +3,83 @@ import pandas as pd
|
|
3 |
import gspread
|
4 |
from google.auth import default
|
5 |
import requests
|
6 |
-
import time
|
7 |
import json
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Define custom CSS styles for the buttons
|
12 |
custom_css = """
|
13 |
<style>
|
14 |
-
.button-container {
|
15 |
-
display: flex;
|
16 |
-
justify-content: space-between;
|
17 |
-
}
|
18 |
.feedback-button {
|
19 |
background-color: #008CBA;
|
20 |
color: white;
|
@@ -34,28 +99,33 @@ custom_css = """
|
|
34 |
</style>
|
35 |
"""
|
36 |
|
37 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
feedback_interface = gr.Interface(
|
39 |
fn=feedback_response,
|
40 |
inputs=[
|
41 |
gr.Textbox(label="Input Message"),
|
|
|
42 |
gr.Textbox(label="Additional Comments")
|
43 |
],
|
44 |
outputs=[
|
45 |
-
gr.
|
46 |
-
|
47 |
-
<button class="feedback-button" id="good-feedback" onclick="setFeedback('Good')">Good</button>
|
48 |
-
<button class="feedback-button" id="bad-feedback" onclick="setFeedback('Bad')">Bad</button>
|
49 |
-
<button class="feedback-button" id="inappropriate-feedback" onclick="setFeedback('Inappropriate')">Inappropriate</button>
|
50 |
-
</div>
|
51 |
-
"""),
|
52 |
-
gr.HTML(label="Feedback (Bottom Row)", html=custom_css + """
|
53 |
-
<div class="button-container">
|
54 |
-
<button class="feedback-button" id="informative-feedback" onclick="setFeedback('Informative')">Informative</button>
|
55 |
-
<button class="feedback-button" id="inaccurate-feedback" onclick="setFeedback('Inaccurate')">Inaccurate</button>
|
56 |
-
<button class="feedback-button" id="nonsense-feedback" onclick="setFeedback('Nonsense')">Nonsense</button>
|
57 |
-
</div>
|
58 |
-
""")
|
59 |
],
|
60 |
title="Beta: Itell Guide Response Bot",
|
61 |
description="""
|
|
|
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 |
+
gc = gspread.service_account(filename='botresponse-6f1a8c749aa0.json')
|
11 |
+
|
12 |
+
# Specify your Google Sheets credentials, sheet_id, and worksheet_name
|
13 |
+
sheet_id = "18hnoTsEaGMWMael42MXubb-FzAe5jJB5RpaSolIXyb0"
|
14 |
+
worksheet_name = "Sheet1"
|
15 |
+
|
16 |
+
# Function to get the initial response
|
17 |
+
def get_initial_response(input_message):
|
18 |
+
url = "https://itell-api.learlab.vanderbilt.edu/chat"
|
19 |
+
|
20 |
+
payload = {
|
21 |
+
"textbook_name": "think-python-2e",
|
22 |
+
"message": input_message
|
23 |
+
}
|
24 |
+
headers = {"Content-Type": "application/json"}
|
25 |
+
|
26 |
+
# Measure the start time
|
27 |
+
start_time = time.time()
|
28 |
+
|
29 |
+
response = requests.post(url, json=payload, headers=headers)
|
30 |
+
data = json.loads(response.text)
|
31 |
+
message = data['message']
|
32 |
+
|
33 |
+
# Calculate the elapsed time
|
34 |
+
elapsed_time = time.time() - start_time
|
35 |
+
elapsed_time = round(elapsed_time, 2)
|
36 |
+
response_time_message = f"Response time: {elapsed_time} seconds"
|
37 |
+
|
38 |
+
return message, response_time_message # Return the initial_response and elapsed_time
|
39 |
+
|
40 |
+
# Function to collect feedback and update the spreadsheet
|
41 |
+
def feedback_response(input_message, feedback, comments):
|
42 |
+
# Get the initial response and elapsed_time
|
43 |
+
initial_response, elapsed_time = get_initial_response(input_message)
|
44 |
+
|
45 |
+
# Collect feedback
|
46 |
+
response = ''
|
47 |
+
if feedback == 'Informative':
|
48 |
+
response = 'Informative'
|
49 |
+
elif feedback == 'Inaccurate':
|
50 |
+
response = 'Inaccurate'
|
51 |
+
elif feedback == 'Nonsense':
|
52 |
+
response = 'Nonsense'
|
53 |
+
elif feedback == 'Good':
|
54 |
+
response = 'Good'
|
55 |
+
elif feedback == 'Bad':
|
56 |
+
response = 'Bad'
|
57 |
+
elif feedback == 'Inappropriate':
|
58 |
+
response = 'Inappropriate'
|
59 |
+
else:
|
60 |
+
response = 'NA'
|
61 |
+
|
62 |
+
# Update Google Sheets
|
63 |
+
sh = gc.open_by_key(sheet_id)
|
64 |
+
worksheet = sh.worksheet(worksheet_name)
|
65 |
+
|
66 |
+
thankyou = "Thank you for your feedback. Your response and feedback have been recorded!"
|
67 |
+
# Append the data to the worksheet only if comments have a value
|
68 |
+
if comments:
|
69 |
+
# Create a DataFrame from the response and additional comments
|
70 |
+
df = pd.DataFrame({'Input Message': [input_message], 'Output': [initial_response],
|
71 |
+
'Time took in Seconds': [elapsed_time], 'Feedback': [response],
|
72 |
+
'Additional Comments': [comments]})
|
73 |
+
|
74 |
+
# Append the data to the worksheet
|
75 |
+
worksheet.append_rows(df.values.tolist())
|
76 |
+
initial_response = thankyou
|
77 |
+
|
78 |
+
return initial_response, elapsed_time # Return the initial_response and elapsed_time
|
79 |
|
80 |
# Define custom CSS styles for the buttons
|
81 |
custom_css = """
|
82 |
<style>
|
|
|
|
|
|
|
|
|
83 |
.feedback-button {
|
84 |
background-color: #008CBA;
|
85 |
color: white;
|
|
|
99 |
</style>
|
100 |
"""
|
101 |
|
102 |
+
# Define the custom HTML for the buttons
|
103 |
+
custom_html = custom_css + """
|
104 |
+
<div>
|
105 |
+
<label>Feedback (Top Row):</label>
|
106 |
+
<button class="feedback-button" onclick="setFeedback('Good')">Good</button>
|
107 |
+
<button class="feedback-button" onclick="setFeedback('Bad')">Bad</button>
|
108 |
+
<button class="feedback-button" onclick="setFeedback('Inappropriate')">Inappropriate</button>
|
109 |
+
</div>
|
110 |
+
<div>
|
111 |
+
<label>Feedback (Bottom Row):</label>
|
112 |
+
<button class="feedback-button" onclick="setFeedback('Informative')">Informative</button>
|
113 |
+
<button class="feedback-button" onclick="setFeedback('Inaccurate')">Inaccurate</button>
|
114 |
+
<button class="feedback-button" onclick="setFeedback('Nonsense')">Nonsense</button>
|
115 |
+
</div>
|
116 |
+
"""
|
117 |
+
|
118 |
+
# Set up the Gradio Interface with HTML components for custom buttons
|
119 |
feedback_interface = gr.Interface(
|
120 |
fn=feedback_response,
|
121 |
inputs=[
|
122 |
gr.Textbox(label="Input Message"),
|
123 |
+
gr.HTML(label="Feedback Buttons", html=custom_html),
|
124 |
gr.Textbox(label="Additional Comments")
|
125 |
],
|
126 |
outputs=[
|
127 |
+
gr.Textbox(label="Output", type="text"),
|
128 |
+
gr.Textbox(label="Elapsed Time (s)", type="text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
],
|
130 |
title="Beta: Itell Guide Response Bot",
|
131 |
description="""
|