Spaces:
Runtime error
Runtime error
Update from brunch
Browse files- Dockerfile +3 -0
- app/utils/gmail_utils.py +11 -3
Dockerfile
CHANGED
@@ -34,6 +34,9 @@ RUN mkdir -p /workspace/hub/.cache && \
|
|
34 |
mkdir -p /app/.cache && \
|
35 |
chmod -R 777 /app/.cache
|
36 |
|
|
|
|
|
|
|
37 |
# Create a non-root user
|
38 |
RUN useradd -m -u 1000 user && \
|
39 |
chown -R user /workspace
|
|
|
34 |
mkdir -p /app/.cache && \
|
35 |
chmod -R 777 /app/.cache
|
36 |
|
37 |
+
# Copy token.json to /workspace
|
38 |
+
COPY token.json /workspace/
|
39 |
+
|
40 |
# Create a non-root user
|
41 |
RUN useradd -m -u 1000 user && \
|
42 |
chown -R user /workspace
|
app/utils/gmail_utils.py
CHANGED
@@ -12,10 +12,18 @@ SCOPES = [
|
|
12 |
]
|
13 |
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def authenticate_gmail(gmail_creds_path):
|
16 |
creds = None
|
17 |
-
|
18 |
-
|
|
|
19 |
if not creds or not creds.valid:
|
20 |
if creds and creds.expired and creds.refresh_token:
|
21 |
creds.refresh(Request())
|
@@ -27,7 +35,7 @@ def authenticate_gmail(gmail_creds_path):
|
|
27 |
)
|
28 |
creds = flow.run_local_server(port=8796, access_type='offline', prompt='consent')
|
29 |
# Save the credentials for the next run
|
30 |
-
with open(
|
31 |
token.write(creds.to_json())
|
32 |
return creds
|
33 |
|
|
|
12 |
]
|
13 |
|
14 |
|
15 |
+
def get_token_path():
|
16 |
+
# Use /workspace/token.json if running in Docker/HF Space, else local
|
17 |
+
if os.environ.get("IS_HF_SPACE") == "1" or os.path.exists("/workspace"):
|
18 |
+
return "/workspace/token.json"
|
19 |
+
return "token.json"
|
20 |
+
|
21 |
+
|
22 |
def authenticate_gmail(gmail_creds_path):
|
23 |
creds = None
|
24 |
+
token_path = get_token_path()
|
25 |
+
if os.path.exists(token_path):
|
26 |
+
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
|
27 |
if not creds or not creds.valid:
|
28 |
if creds and creds.expired and creds.refresh_token:
|
29 |
creds.refresh(Request())
|
|
|
35 |
)
|
36 |
creds = flow.run_local_server(port=8796, access_type='offline', prompt='consent')
|
37 |
# Save the credentials for the next run
|
38 |
+
with open(token_path, 'w') as token:
|
39 |
token.write(creds.to_json())
|
40 |
return creds
|
41 |
|