|
|
|
|
|
import gspread |
|
|
from oauth2client.service_account import ServiceAccountCredentials |
|
|
import pandas as pd |
|
|
import os |
|
|
import json |
|
|
|
|
|
def get_gspread_client(): |
|
|
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"] |
|
|
|
|
|
google_creds_json = os.environ.get("GOOGLE_CREDENTIALS_JSON") |
|
|
|
|
|
if google_creds_json: |
|
|
|
|
|
creds_dict = json.loads(google_creds_json) |
|
|
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope) |
|
|
else: |
|
|
|
|
|
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope) |
|
|
|
|
|
client = gspread.authorize(creds) |
|
|
return client |
|
|
|
|
|
def upload_to_google_sheets(session_data): |
|
|
client = get_gspread_client() |
|
|
sheet = client.open("Bipolar Chatbot Logs").sheet1 |
|
|
|
|
|
|
|
|
df = pd.DataFrame(session_data) |
|
|
|
|
|
|
|
|
try: |
|
|
existing_headers = sheet.row_values(1) |
|
|
except gspread.exceptions.APIError: |
|
|
existing_headers = [] |
|
|
|
|
|
|
|
|
columns = ['query', 'response', 'sources', 'feedback', 'source_feedback'] |
|
|
df = df[columns] |
|
|
|
|
|
|
|
|
if not existing_headers: |
|
|
sheet.append_row(columns) |
|
|
|
|
|
|
|
|
for row in df.itertuples(index=False, name=None): |
|
|
sheet.append_row(list(row)) |
|
|
|