import gradio as gr import pandas as pd import gspread from google.auth import default import requests import time import json # Authenticate and authorize with Google Sheets #creds, _ = default() #gc = gspread.authorize(creds) gc = gspread.service_account(filename='botresponse-6f1a8c749aa0.json') # Specify your Google Sheets credentials, sheet_id, and worksheet_name sheet_id = "18hnoTsEaGMWMael42MXubb-FzAe5jJB5RpaSolIXyb0" worksheet_name = "Sheet1" # Function to get the initial response def get_initial_response(input_message): url = "https://itell-api.learlab.vanderbilt.edu/chat" payload = { "textbook_name": "think-python-2e", "message": input_message } headers = {"Content-Type": "application/json"} # Measure the start time start_time = time.time() response = requests.post(url, json=payload, headers=headers) data = json.loads(response.text) message = data['message'] # Calculate the elapsed time elapsed_time = time.time() - start_time elapsed_time = round(elapsed_time, 2) response_time_message = f"Response time: {elapsed_time} seconds" return message, response_time_message # Return the initial_response and elapsed_time # Function to collect feedback and update the spreadsheet def feedback_response(input_message, feedback, comments): # Get the initial response and elapsed_time initial_response, elapsed_time = get_initial_response(input_message) # Collect feedback response = '' if feedback == 'Informative': response = 'Informative' elif feedback == 'Inaccurate': response = 'Inaccurate' elif feedback == 'Nonsense': response = 'Nonsense' else: response = 'NA' # Update Google Sheets sh = gc.open_by_key(sheet_id) worksheet = sh.worksheet(worksheet_name) thankyou = "Thank you for your feedback. Your response and feedback has been recorded!" # Append the data to the worksheet only if comments have a value if comments: # Create a DataFrame from the response and additional comments df = pd.DataFrame({'Input Message': [input_message], 'Output': [initial_response], 'Time took in Seconds': [elapsed_time],'Feedback': [response], 'Additional Comments': [comments]}) # Append the data to the worksheet worksheet.append_rows(df.values.tolist()) initial_response = thankyou return initial_response, elapsed_time # Return the initial_response and elapsed_time # Set up the Gradio Interface feedback_interface = gr.Interface( fn=feedback_response, inputs=[ gr.Textbox(label="Input Message"), gr.Radio( choices=[ "Informative", "Inaccurate", "Nonsense" ], label="Feedback" ), gr.Textbox(label="Additional Comments") ], outputs=[ gr.Textbox(label="Output", type="text"), gr.Textbox(label="Elapsed Time (s)", type="text") # Change the type to "text" for two decimal places ], title="Beta: Itell Guide Response Bot", description=""" # Introduction This is an interface to test iTELL's guide on the side. Please be aware that responses can take up to 20 seconds # Step by Step Introduction 1. Place a question in the input message textbox 2. Wait roughly 10 ~ 20 seconds for the response and elapsed time to come out on the right 3. After looking at the results, click on the feedback options that best describes the output: Informative, Inaccurate, Nonsense 4. After choosing the best option, write down additional comments for more feedback 5. Press submit again and wait for the output to come out again for your feedback to be submitted 6. Once the "Thank you for your feedback. Your response and feedback has been recorded!" message is shown, you are done ** DISCLAIMER: You have to type a input message, click on the feedback buttons, and type in additional comments for your responses to be recorded ** For further questions contact LEAR Labs! """ ) # Launch the interface feedback_interface.launch()