Skip to content

Commit 1285c29

Browse files
committed
Support specific tabs when loading Google Sheets (google-gemini#88)
The CSV URL trick seems to support specifying tabs, so this change plumbs the tab reference (`gid`) through.
1 parent 6cb0d53 commit 1285c29

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

google/generativeai/types/model_types.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def encode_tuning_data(
214214

215215
if isinstance(data, str):
216216
# Strings are either URLs or system paths.
217-
if re.match("^\w+://\S+$", data):
217+
if re.match(r"^\w+://\S+$", data):
218218
data = _normalize_url(data)
219219
else:
220220
# Normalize system paths to use pathlib
@@ -247,10 +247,17 @@ def _normalize_url(url: str) -> str:
247247
sheet_base = "https://docs.google.com/spreadsheets"
248248
if url.startswith(sheet_base):
249249
# Normalize google-sheets URLs to download the csv.
250-
match = re.match(f"{sheet_base}/d/[^/]+", url)
251-
if match is None:
250+
id_match = re.match(f"{sheet_base}/d/[^/]+", url)
251+
if id_match is None:
252252
raise ValueError("Incomplete Google Sheets URL: {data}")
253-
url = f"{match.group(0)}/export?format=csv"
253+
254+
if tab_match := re.search(r"gid=(\d+)", url):
255+
tab_param = f"&gid={tab_match.group(1)}"
256+
else:
257+
tab_param = ""
258+
259+
url = f"{id_match.group(0)}/export?format=csv{tab_param}"
260+
254261
return url
255262

256263

tests/test_models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ def test_create_tuned_model_on_tuned_model(self, tuned_source):
448448
"sheet-export-csv",
449449
"https://docs.google.com/spreadsheets/d/1OffcVSqN6X-RYdWLGccDF3KtnKoIpS7O_9cZbicKK4A/export?format=csv",
450450
],
451+
[
452+
"sheet-with-tab",
453+
"https://docs.google.com/spreadsheets/d/118LXTS3RIkS4yAO68c-cMPP4PwLFTxKYj4R43R7dU0E/edit#gid=1526779134",
454+
],
451455
)
452456
def test_create_dataset(self, data, ik="text_input", ok="output"):
453457
ds = model_types.encode_tuning_data(data, input_key=ik, output_key=ok)

0 commit comments

Comments
 (0)