Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,82 @@ import requests
|
|
6 |
import time
|
7 |
import json
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Set up the Gradio Interface
|
12 |
feedback_interface = gr.Interface(
|
|
|
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
|
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 |
+
return initial_response, elapsed_time # Return the initial_response and elapsed_time
|
85 |
|
86 |
# Set up the Gradio Interface
|
87 |
feedback_interface = gr.Interface(
|