Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,104 +6,25 @@ import requests
|
|
6 |
import time
|
7 |
import json
|
8 |
|
9 |
-
#
|
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
|
29 |
-
start_time = time.time()
|
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, add_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 == 'Good':
|
50 |
-
response = 'Good'
|
51 |
-
elif feedback == 'Bad':
|
52 |
-
response = 'Bad'
|
53 |
-
elif feedback == 'Inappropriate':
|
54 |
-
response = 'Inappropriate'
|
55 |
-
else:
|
56 |
-
response = 'NA'
|
57 |
-
|
58 |
-
# More feedback
|
59 |
-
# Collect feedback
|
60 |
-
add_response = ''
|
61 |
-
if add_feedback == 'Informative':
|
62 |
-
add_response = 'Informative'
|
63 |
-
elif add_feedback == 'Inaccurate':
|
64 |
-
add_response = 'Inaccurate'
|
65 |
-
elif add_feedback == 'Nonsense':
|
66 |
-
add_response = 'Nonsense'
|
67 |
-
else:
|
68 |
-
add_response = 'NA'
|
69 |
-
|
70 |
-
# Update Google Sheets
|
71 |
-
sh = gc.open_by_key(sheet_id)
|
72 |
-
worksheet = sh.worksheet(worksheet_name)
|
73 |
-
|
74 |
-
thankyou = "Thank you for your feedback. Your response and feedback has been recorded!"
|
75 |
-
# Append the data to the worksheet only if comments have a value
|
76 |
-
if comments:
|
77 |
-
# Create a DataFrame from the response and additional comments
|
78 |
-
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]})
|
79 |
-
|
80 |
-
# Append the data to the worksheet
|
81 |
-
worksheet.append_rows(df.values.tolist())
|
82 |
-
initial_response = thankyou
|
83 |
-
|
84 |
-
|
85 |
-
return initial_response, elapsed_time # Return the initial_response and elapsed_time
|
86 |
|
87 |
# Set up the Gradio Interface
|
88 |
feedback_interface = gr.Interface(
|
89 |
fn=feedback_response,
|
90 |
inputs=[
|
91 |
gr.Textbox(label="Input Message"),
|
92 |
-
gr.
|
|
|
93 |
choices=[
|
94 |
-
"Good",
|
95 |
-
"
|
96 |
-
|
97 |
-
|
98 |
-
"Inaccurate",
|
99 |
-
"Nonsense"
|
100 |
-
],
|
101 |
-
label="Feedback"),
|
102 |
gr.Textbox(label="Additional Comments")
|
103 |
],
|
104 |
outputs=[
|
105 |
gr.Textbox(label="Output", type="text"),
|
106 |
-
gr.Textbox(label="Elapsed Time (s)", type="text")
|
107 |
],
|
108 |
title="Beta: Itell Guide Response Bot",
|
109 |
description="""
|
@@ -125,4 +46,4 @@ feedback_interface = gr.Interface(
|
|
125 |
)
|
126 |
|
127 |
# Launch the interface
|
128 |
-
feedback_interface.launch()
|
|
|
6 |
import time
|
7 |
import json
|
8 |
|
9 |
+
# ... (rest of your code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Set up the Gradio Interface
|
12 |
feedback_interface = gr.Interface(
|
13 |
fn=feedback_response,
|
14 |
inputs=[
|
15 |
gr.Textbox(label="Input Message"),
|
16 |
+
gr.RadioGroup(
|
17 |
+
label="Feedback",
|
18 |
choices=[
|
19 |
+
["Good", "Bad", "Inappropriate"],
|
20 |
+
["Informative", "Inaccurate", "Nonsense"]
|
21 |
+
]
|
22 |
+
),
|
|
|
|
|
|
|
|
|
23 |
gr.Textbox(label="Additional Comments")
|
24 |
],
|
25 |
outputs=[
|
26 |
gr.Textbox(label="Output", type="text"),
|
27 |
+
gr.Textbox(label="Elapsed Time (s)", type="text")
|
28 |
],
|
29 |
title="Beta: Itell Guide Response Bot",
|
30 |
description="""
|
|
|
46 |
)
|
47 |
|
48 |
# Launch the interface
|
49 |
+
feedback_interface.launch()
|