import gradio as gr import pandas as pd import gspread from google.auth import default import requests import time import json import re # 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"] message = message.encode("utf-8").decode("unicode_escape") message = re.sub(r"^[\s'\"]*(bot|cite sources)?[\s'\"]*", "", message) message = message.strip(" ':\"") # Calculate the elapsed time elapsed_time = time.time() - start_time elapsed_time = round(elapsed_time, 2) return { bot_resp: message, response_time: elapsed_time, } # Function to collect feedback and update the spreadsheet def feedback_response( input_message, bot_message, response_time, feedback_primary, feedback_secondary, comments, ): feedback = [] feedback.append(feedback_primary) feedback.extend(feedback_secondary) # Update Google Sheets sh = gc.open_by_key(sheet_id) worksheet = sh.worksheet(worksheet_name) # Create a DataFrame from the response and additional comments df = pd.DataFrame( { "Input Message": [input_message], "Output": [bot_message], "Time took in Seconds": [response_time], "Feedback": [str(feedback)], "Additional Comments": [comments], } ) # Append the data to the worksheet worksheet.append_rows(df.values.tolist()) gr.Info("Feedback Submitted") return { primary_feedback: None, secondary_feedback: None, additional_comments: None, } with gr.Blocks(title="iTELL Chat Feedback") as feedback_interface: title = "Itell Guide Response Bot" gr.components.Markdown( f"