Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import gspread
|
| 4 |
from google.auth import default
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Authenticate and authorize with Google Sheets
|
| 7 |
#creds, _ = default()
|
|
@@ -12,31 +14,65 @@ gc = gspread.service_account(filename='botresponse-6f1a8c749aa0.json')
|
|
| 12 |
sheet_id = "18hnoTsEaGMWMael42MXubb-FzAe5jJB5RpaSolIXyb0"
|
| 13 |
worksheet_name = "Sheet1"
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
response = ''
|
| 17 |
if feedback == 'Informative':
|
| 18 |
response = 'Informative'
|
| 19 |
elif feedback == 'Inaccurate':
|
| 20 |
response = 'Inaccurate'
|
| 21 |
-
|
| 22 |
response = 'Nonsense'
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
sh = gc.open_by_key(sheet_id)
|
| 26 |
worksheet = sh.worksheet(worksheet_name)
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Set up the Gradio Interface
|
| 37 |
feedback_interface = gr.Interface(
|
| 38 |
fn=feedback_response,
|
| 39 |
inputs=[
|
|
|
|
| 40 |
gr.Radio(
|
| 41 |
choices=[
|
| 42 |
"Informative",
|
|
@@ -45,13 +81,14 @@ feedback_interface = gr.Interface(
|
|
| 45 |
],
|
| 46 |
label="Feedback"
|
| 47 |
),
|
| 48 |
-
gr.Textbox(label="New Input Query"),
|
| 49 |
gr.Textbox(label="Additional Comments")
|
| 50 |
-
|
| 51 |
],
|
| 52 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 53 |
title="Beta: Itell Guide Response Bot",
|
| 54 |
-
description
|
| 55 |
)
|
| 56 |
|
| 57 |
# Launch the interface
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import gspread
|
| 4 |
from google.auth import default
|
| 5 |
+
import requests
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
# Authenticate and authorize with Google Sheets
|
| 9 |
#creds, _ = default()
|
|
|
|
| 14 |
sheet_id = "18hnoTsEaGMWMael42MXubb-FzAe5jJB5RpaSolIXyb0"
|
| 15 |
worksheet_name = "Sheet1"
|
| 16 |
|
| 17 |
+
# Function to get the initial response
|
| 18 |
+
def get_initial_response(input_message):
|
| 19 |
+
url = "https://itell-api.learlab.vanderbilt.edu/chat"
|
| 20 |
+
|
| 21 |
+
payload = {
|
| 22 |
+
"textbook_name": "think-python-2e",
|
| 23 |
+
"message": input_message
|
| 24 |
+
}
|
| 25 |
+
headers = {"Content-Type": "application/json"}
|
| 26 |
+
|
| 27 |
+
# Measure the start time
|
| 28 |
+
start_time = time.time()
|
| 29 |
+
|
| 30 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 31 |
+
data = json.loads(response.text)
|
| 32 |
+
message = data['message']
|
| 33 |
+
|
| 34 |
+
# Calculate the elapsed time
|
| 35 |
+
elapsed_time = time.time() - start_time
|
| 36 |
+
elapsed_time = round(elapsed_time, 2)
|
| 37 |
+
response_time_message = f"Response time: {elapsed_time} seconds"
|
| 38 |
+
|
| 39 |
+
return message, response_time_message # Return the initial_response and elapsed_time
|
| 40 |
+
|
| 41 |
+
# Function to collect feedback and update the spreadsheet
|
| 42 |
+
def feedback_response(input_message, feedback, comments):
|
| 43 |
+
# Get the initial response and elapsed_time
|
| 44 |
+
initial_response, elapsed_time = get_initial_response(input_message)
|
| 45 |
+
|
| 46 |
+
# Collect feedback
|
| 47 |
response = ''
|
| 48 |
if feedback == 'Informative':
|
| 49 |
response = 'Informative'
|
| 50 |
elif feedback == 'Inaccurate':
|
| 51 |
response = 'Inaccurate'
|
| 52 |
+
elif feedback == 'Nonsense':
|
| 53 |
response = 'Nonsense'
|
| 54 |
+
else:
|
| 55 |
+
response = 'NA'
|
| 56 |
+
|
| 57 |
+
# Update Google Sheets
|
| 58 |
sh = gc.open_by_key(sheet_id)
|
| 59 |
worksheet = sh.worksheet(worksheet_name)
|
| 60 |
+
|
| 61 |
+
# Append the data to the worksheet only if comments have a value
|
| 62 |
+
if comments:
|
| 63 |
+
# Create a DataFrame from the response and additional comments
|
| 64 |
+
df = pd.DataFrame({'Input Message': [input_message], 'Output': [initial_response], 'Time took in Seconds': [elapsed_time],'Feedback': [response], 'Additional Comments': [comments]})
|
| 65 |
+
|
| 66 |
+
# Append the data to the worksheet
|
| 67 |
+
worksheet.append_rows(df.values.tolist())
|
| 68 |
+
|
| 69 |
+
return initial_response, elapsed_time # Return the initial_response and elapsed_time
|
| 70 |
|
| 71 |
# Set up the Gradio Interface
|
| 72 |
feedback_interface = gr.Interface(
|
| 73 |
fn=feedback_response,
|
| 74 |
inputs=[
|
| 75 |
+
gr.Textbox(label="Input Message"),
|
| 76 |
gr.Radio(
|
| 77 |
choices=[
|
| 78 |
"Informative",
|
|
|
|
| 81 |
],
|
| 82 |
label="Feedback"
|
| 83 |
),
|
|
|
|
| 84 |
gr.Textbox(label="Additional Comments")
|
|
|
|
| 85 |
],
|
| 86 |
+
outputs=[
|
| 87 |
+
gr.Textbox(label="Output", type="text"),
|
| 88 |
+
gr.Textbox(label="Elapsed Time (s)", type="text") # Change the type to "text" for two decimal places
|
| 89 |
+
],
|
| 90 |
title="Beta: Itell Guide Response Bot",
|
| 91 |
+
description="This is an interface to test iTELL's guide on the side. Please be aware that responses can take up to 20 seconds",
|
| 92 |
)
|
| 93 |
|
| 94 |
# Launch the interface
|