File tree 3 files changed +37
-15
lines changed
3 files changed +37
-15
lines changed Original file line number Diff line number Diff line change 2
2
import multiprocessing
3
3
import os .path
4
4
import tempfile
5
+ import threading
5
6
import typing
6
7
from enum import Enum
7
8
@@ -873,9 +874,6 @@ def _translate_text(
873
874
return result .strip ()
874
875
875
876
876
- _session = None
877
-
878
-
879
877
def _MinT_translate_one_text (
880
878
text : str , source_language : str , target_language : str
881
879
) -> str :
@@ -894,18 +892,25 @@ def _MinT_translate_one_text(
894
892
return tanslation .get ("translation" , text )
895
893
896
894
897
- def get_google_auth_session ():
895
+ _session = None
896
+ _session_lock = threading .Lock ()
897
+
898
+
899
+ def get_google_auth_session (scopes = None ):
898
900
global _session
899
901
900
902
if _session is None :
901
- import google .auth
902
- from google .auth .transport .requests import AuthorizedSession
903
-
904
- creds , project = google .auth .default (
905
- scopes = ["https://www.googleapis.com/auth/cloud-platform" ]
906
- )
907
- # takes care of refreshing the token and adding it to request headers
908
- _session = AuthorizedSession (credentials = creds ), project
903
+ with _session_lock :
904
+ if _session is None :
905
+ import google .auth
906
+ from google .auth .transport .requests import AuthorizedSession
907
+
908
+ scopes = scopes or ["https://www.googleapis.com/auth/cloud-platform" ]
909
+ creds , project = google .auth .default (
910
+ scopes = scopes ,
911
+ )
912
+ # takes care of refreshing the token and adding it to request headers
913
+ _session = AuthorizedSession (credentials = creds ), project
909
914
910
915
return _session
911
916
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 @@ -373,9 +373,23 @@ def doc_url_to_file_metadata(f_url: str) -> FileMetadata:
373
373
meta = gdrive_metadata (url_to_gdrive_file_id (f ))
374
374
except HttpError as e :
375
375
if e .status_code == 404 :
376
+ from google .oauth2 .service_account import Credentials
377
+
378
+ service_account_client_email = Credentials .from_service_account_file (
379
+ settings .service_account_key_path
380
+ ).service_account_email
381
+
376
382
raise UserError (
377
- f"Could not download the google doc at { f_url } "
378
- f"Please make sure to make the document public for viewing."
383
+ # language=HTML
384
+ f"""<p>This knowledge base Google Doc is not accessible: <a href="{ f_url } " target="_blank">{ f_url } </a></p>
385
+ <p>To address this:</p>
386
+ <ul>
387
+ <li>Please make the Google Doc publicly viewable, or</li>
388
+ <li>Share the Doc or its parent folder with <br>
389
+ <a href="mailto:{ service_account_client_email } ">{ service_account_client_email } </a>
390
+ as an authorized viewer.
391
+ </li>
392
+ </ul>"""
379
393
) from e
380
394
else :
381
395
raise
You can’t perform that action at this time.
0 commit comments