Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fal/auth): add support for Google Colab token retrieval #389

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions projects/fal/src/fal/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ def _to_fal_app_name(name: str) -> str:


def _print_python_packages() -> None:
from pkg_resources import working_set
from importlib.metadata import distributions

packages = [f"{package.key}=={package.version}" for package in working_set]
packages = [f"{dist.metadata['Name']}=={dist.version}" for dist in distributions()]

print("[debug] Python packages installed:", ", ".join(packages))

Expand Down
49 changes: 47 additions & 2 deletions projects/fal/src/fal/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import os
from dataclasses import dataclass, field
from threading import Lock
from typing import Optional

import click

Expand All @@ -11,13 +13,56 @@
from fal.exceptions.auth import UnauthenticatedException


class GoogleColabState:
def __init__(self):
self.is_checked = False
self.lock = Lock()
self.secret: Optional[str] = None


_colab_state = GoogleColabState()


def is_google_colab() -> bool:
try:
from IPython import get_ipython

return "google.colab" in str(get_ipython())
except ModuleNotFoundError:
return False
except NameError:
return False
Comment on lines +26 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something I did not test yet is if this works with google colab pro as well ? (same with the client)

due to some issues in my country I can't access colab pro

this is how huggingface_hub handles it in case of any issues
https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/utils/_runtime.py#L296

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, interesting. I think I have access to Pro, so I'll try it out in a sec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be working fine in pro.



def get_colab_token() -> Optional[str]:
if not is_google_colab():
return None
with _colab_state.lock:
if _colab_state.is_checked: # request access only once
return _colab_state.secret

try:
from google.colab import userdata # noqa: I001
except ImportError:
return None

try:
token = userdata.get("FAL_KEY")
_colab_state.secret = token.strip()
except Exception:
_colab_state.secret = None

_colab_state.is_checked = True
return _colab_state.secret


def key_credentials() -> tuple[str, str] | None:
# Ignore key credentials when the user forces auth by user.
if os.environ.get("FAL_FORCE_AUTH_BY_USER") == "1":
return None

if "FAL_KEY" in os.environ:
key = os.environ["FAL_KEY"]
key = os.environ.get("FAL_KEY") or get_colab_token()
if key:
key_id, key_secret = key.split(":", 1)
return (key_id, key_secret)
elif "FAL_KEY_ID" in os.environ and "FAL_KEY_SECRET" in os.environ:
Expand Down
Loading