File tree 3 files changed +38
-14
lines changed
3 files changed +38
-14
lines changed Original file line number Diff line number Diff line change 1
1
import multiprocessing
2
2
import os .path
3
3
import tempfile
4
+ import threading
4
5
import typing
5
6
from enum import Enum
6
7
@@ -932,9 +933,6 @@ def _translate_text(
932
933
return result .strip ()
933
934
934
935
935
- _session = None
936
-
937
-
938
936
def _MinT_translate_one_text (
939
937
text : str , source_language : str , target_language : str
940
938
) -> str :
@@ -953,18 +951,27 @@ def _MinT_translate_one_text(
953
951
return tanslation .get ("translation" , text )
954
952
955
953
956
- def get_google_auth_session ():
954
+ _session = None
955
+ _session_lock = threading .Lock ()
956
+
957
+
958
+ def get_google_auth_session (scopes : typing .Optional [list [str ]] = None ):
957
959
global _session
958
960
959
961
if _session is None :
960
- import google .auth
961
- from google .auth .transport .requests import AuthorizedSession
962
+ with _session_lock :
963
+ if _session is None :
964
+ import google .auth
965
+ from google .auth .transport .requests import AuthorizedSession
962
966
963
- creds , project = google .auth .default (
964
- scopes = ["https://www.googleapis.com/auth/cloud-platform" ]
965
- )
966
- # takes care of refreshing the token and adding it to request headers
967
- _session = AuthorizedSession (credentials = creds ), project
967
+ if not scopes :
968
+ scopes = ["https://www.googleapis.com/auth/cloud-platform" ]
969
+
970
+ creds , project = google .auth .default (
971
+ scopes = scopes ,
972
+ )
973
+ # takes care of refreshing the token and adding it to request headers
974
+ _session = AuthorizedSession (credentials = creds ), project
968
975
969
976
return _session
970
977
Original file line number Diff line number Diff line change @@ -74,6 +74,7 @@ def gdrive_download(
74
74
) -> tuple [bytes , str ]:
75
75
from googleapiclient import discovery
76
76
from googleapiclient .http import MediaIoBaseDownload
77
+ from daras_ai_v2 .asr import get_google_auth_session
77
78
78
79
if export_links is None :
79
80
export_links = {}
@@ -87,7 +88,9 @@ def gdrive_download(
87
88
# export google docs to appropriate type
88
89
export_mime_type = DOCS_EXPORT_MIMETYPES .get (mime_type , mime_type )
89
90
if f_url_export := export_links .get (export_mime_type , None ):
90
- r = requests .get (f_url_export )
91
+ drive_scopes = ["https://www.googleapis.com/auth/drive.readonly" ]
92
+ session , _ = get_google_auth_session (drive_scopes )
93
+ r = session .get (f_url_export )
91
94
file_bytes = r .content
92
95
raise_for_status (r , is_user_url = True )
93
96
return file_bytes , export_mime_type
Original file line number Diff line number Diff line change @@ -377,9 +377,23 @@ def doc_url_to_file_metadata(f_url: str) -> FileMetadata:
377
377
meta = gdrive_metadata (url_to_gdrive_file_id (f ))
378
378
except HttpError as e :
379
379
if e .status_code == 404 :
380
+ from google .oauth2 .service_account import Credentials
381
+
382
+ service_account_client_email = Credentials .from_service_account_file (
383
+ settings .service_account_key_path
384
+ ).service_account_email
385
+
380
386
raise UserError (
381
- f"Could not download the google doc at { f_url } "
382
- f"Please make sure to make the document public for viewing."
387
+ # language=HTML
388
+ f"""<p>This knowledge base Google Doc is not accessible: <a href="{ f_url } " target="_blank">{ f_url } </a></p>
389
+ <p>To address this:</p>
390
+ <ul>
391
+ <li>Please make the Google Doc publicly viewable, or</li>
392
+ <li>Share the Doc or its parent folder with <br>
393
+ <a href="mailto:{ service_account_client_email } ">{ service_account_client_email } </a>
394
+ as an authorized viewer.
395
+ </li>
396
+ </ul>"""
383
397
) from e
384
398
else :
385
399
raise
You can’t perform that action at this time.
0 commit comments