-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheets.py
More file actions
62 lines (52 loc) · 1.97 KB
/
sheets.py
File metadata and controls
62 lines (52 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import gspread
import tiktoken
import logging
import os
from omegaconf import DictConfig
from typing import List, Dict
class GSpreadClient:
def __init__(self):
pass
def Authenticate(self, config_path: str):
'''
Method to authenticate with Google Sheets
Returns:
The authenticated Google Sheets client
'''
self.validate_credentials(config_path)
try:
gc = gspread.service_account(filename=config_path)
return gc
except Exception as e:
raise RuntimeError(f"Failed to authenticate with Google Sheets: {e}")
def validate_credentials(self, config_path: str):
'''
Method to validate the Google Sheets credentials
Args:
config_path: The path to the credentials file
'''
if not os.path.exists(config_path):
raise FileNotFoundError(f"The credentials file was not found at {config_path}")
class DataHandler:
def __init__(self, cfg: DictConfig, gspread_client: gspread.client, google_sheet_url: str):
self.openai_model = cfg.openAI.model
self.token_limit = cfg.openAI.token_limit
self.fine_tuning_cost_per_1000_tokens = cfg.openAI.fine_tuning_cost_per_1000_tokens
self.gspread_client = gspread_client
self.google_sheet_url = google_sheet_url
def extract_data(self, sheet_index: int) -> List[Dict[str, str]]:
'''
Method to extract data from Google Sheets
Args:
sheet_index: The index of the sheet to extract data from
Returns:
The extracted data
'''
logger = logging.getLogger("DataHandler")
try:
sh = self.gspread_client.open_by_url(self.google_sheet_url)
worksheet = sh.get_worksheet(sheet_index)
rows = worksheet.get_all_values()
return rows
except Exception as e:
raise RuntimeError(f"Failed to extract data from Google Sheets: {e}")