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 have 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 input_message = gr.Textbox(label="Input Message") feedback_choice = gr.Radio( choices=[ "Informative", "Inaccurate", "Nonsense" ], label="Feedback") additional_comments = gr.Textbox(label="Additional Comments") output_response = gr.Textbox(label="Output", type="text") output_elapsed_time = gr.Textbox(label="Elapsed Time (s)", type="text") # Create separate buttons for response and feedback submission response_button = gr.Button(value="Submit Response") feedback_button = gr.Button(value="Submit Feedback") # Create functions for response and feedback submission def submit_response(): input_text = input_message.value response, elapsed_time = feedback_response(input_text, "", "") output_response.value = response output_elapsed_time.value = str(elapsed_time) def submit_feedback(): input_text = input_message.value feedback = feedback_choice.value comments = additional_comments.value response, elapsed_time = feedback_response(input_text, feedback, comments) output_response.value = response output_elapsed_time.value = str(elapsed_time) # Set the functions for button clicks response_button.click(submit_response) feedback_button.click(submit_feedback) # Create the Gradio interface with buttons gr.Interface( fn=None, inputs=[input_message, feedback_choice, additional_comments, response_button, feedback_button], outputs=[output_response, output_elapsed_time], 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, select the feedback option 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 have been recorded!" message is shown, you are done ** DISCLAIMER: You have to type an input message, select a feedback option, and type in additional comments for your responses to be recorded ** For further questions contact LEAR Labs! """ ).launch()