Skip to content

Commit

Permalink
feat(fal): support basic fal profiles (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop authored Jan 21, 2025
1 parent 72a7376 commit b7ec5a4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions projects/fal/src/fal/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"uvicorn",
"starlette_exporter",
"structlog",
"tomli",
]


Expand Down
5 changes: 4 additions & 1 deletion projects/fal/src/fal/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import click

from fal.auth import auth0, local
from fal.config import Config
from fal.console import console
from fal.console.icons import CHECK_ICON
from fal.exceptions.auth import UnauthenticatedException
Expand Down Expand Up @@ -61,7 +62,9 @@ def key_credentials() -> tuple[str, str] | None:
if os.environ.get("FAL_FORCE_AUTH_BY_USER") == "1":
return None

key = os.environ.get("FAL_KEY") or get_colab_token()
config = Config()

key = os.environ.get("FAL_KEY") or config.get("key") or get_colab_token()
if key:
key_id, key_secret = key.split(":", 1)
return (key_id, key_secret)
Expand Down
23 changes: 23 additions & 0 deletions projects/fal/src/fal/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

import tomli


class Config:
DEFAULT_CONFIG_PATH = "~/.fal/config.toml"
DEFAULT_PROFILE = "default"

def __init__(self):
self.config_path = os.path.expanduser(
os.getenv("FAL_CONFIG_PATH", self.DEFAULT_CONFIG_PATH)
)
self.profile = os.getenv("FAL_PROFILE", self.DEFAULT_PROFILE)

try:
with open(self.config_path, "rb") as file:
self.config = tomli.load(file)
except FileNotFoundError:
self.config = {}

def get(self, key):
return self.config.get(self.profile, {}).get(key)
12 changes: 6 additions & 6 deletions projects/fal/tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def fal_file_content_matches(file: File, content: str):


def test_fal_file_from_path(isolated_client):
@isolated_client(requirements=[f"pydantic=={pydantic_version}"])
@isolated_client(requirements=[f"pydantic=={pydantic_version}", "tomli"])
def fal_file_from_temp(content: str):
with tempfile.NamedTemporaryFile() as temp_file:
file_path = temp_file.name
Expand All @@ -475,7 +475,7 @@ def fal_file_from_temp(content: str):


def test_fal_file_from_bytes(isolated_client):
@isolated_client(requirements=[f"pydantic=={pydantic_version}"])
@isolated_client(requirements=[f"pydantic=={pydantic_version}", "tomli"])
def fal_file_from_bytes(content: str):
return File.from_bytes(content.encode(), repository="in_memory")

Expand All @@ -486,7 +486,7 @@ def fal_file_from_bytes(content: str):


def test_fal_file_save(isolated_client):
@isolated_client(requirements=[f"pydantic=={pydantic_version}"])
@isolated_client(requirements=[f"pydantic=={pydantic_version}", "tomli"])
def fal_file_to_local_file(content: str):
file = File.from_bytes(content.encode(), repository="in_memory")

Expand Down Expand Up @@ -521,7 +521,7 @@ def test_fal_file_input(isolated_client, file_url: str, expected_content: str):
class TestInput(BaseModel):
file: File = Field()

@isolated_client(requirements=[f"pydantic=={pydantic_version}"])
@isolated_client(requirements=[f"pydantic=={pydantic_version}", "tomli"])
def init_file_on_fal(input: TestInput) -> File:
return input.file

Expand All @@ -542,7 +542,7 @@ def test_fal_compressed_file(isolated_client):
class TestInput(BaseModel):
files: CompressedFile

@isolated_client(requirements=[f"pydantic=={pydantic_version}"])
@isolated_client(requirements=[f"pydantic=={pydantic_version}", "tomli"])
def init_compressed_file_on_fal(input: TestInput) -> int:
extracted_file_paths = [file for file in input.files]
return extracted_file_paths
Expand All @@ -557,7 +557,7 @@ def init_compressed_file_on_fal(input: TestInput) -> int:


def test_fal_cdn(isolated_client):
@isolated_client(requirements=[f"pydantic=={pydantic_version}"])
@isolated_client(requirements=[f"pydantic=={pydantic_version}", "tomli"])
def upload_to_fal_cdn() -> FalImage:
return FalImage.from_bytes(b"0", "jpeg", repository="cdn")

Expand Down
8 changes: 4 additions & 4 deletions projects/fal/tests/toolkit/image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def assert_fal_images_equals(fal_image_1: Image, fal_image_2: Image):


def test_fal_image_from_pil(isolated_client):
@isolated_client(requirements=["pillow", f"pydantic=={pydantic_version}"])
@isolated_client(requirements=["pillow", f"pydantic=={pydantic_version}", "tomli"])
def fal_image_from_bytes_remote():
pil_image = get_image()
return Image.from_pil(pil_image, repository="in_memory")
Expand All @@ -87,7 +87,7 @@ def fal_image_from_bytes_remote():


def test_fal_image_from_bytes(isolated_client):
@isolated_client(requirements=["pillow", f"pydantic=={pydantic_version}"])
@isolated_client(requirements=["pillow", f"pydantic=={pydantic_version}", "tomli"])
def fal_image_from_bytes_remote():
image_bytes = get_image(as_bytes=True)
return Image.from_bytes(image_bytes, repository="in_memory", format="png")
Expand All @@ -107,7 +107,7 @@ def test_fal_image_input(isolated_client, image_url):
class TestInput(BaseModel):
image: Image = Field()

@isolated_client(requirements=["pillow", f"pydantic=={pydantic_version}"])
@isolated_client(requirements=["pillow", f"pydantic=={pydantic_version}", "tomli"])
def init_image_on_fal(input: TestInput) -> Image:
return TestInput(image=input.image).image

Expand All @@ -127,7 +127,7 @@ def test_fal_image_input_to_pil(isolated_client):
class TestInput(BaseModel):
image: Image = Field()

@isolated_client(requirements=["pillow", f"pydantic=={pydantic_version}"])
@isolated_client(requirements=["pillow", f"pydantic=={pydantic_version}", "tomli"])
def init_image_on_fal(input: TestInput) -> bytes:
input_image = TestInput(image=input.image).image
pil_image = input_image.to_pil()
Expand Down

0 comments on commit b7ec5a4

Please sign in to comment.