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

misc: add fal api #399

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion projects/fal/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ dependencies = [
"pyjwt[crypto]>=2.8.0,<3",
"uvicorn>=0.29.0,<1",
"cookiecutter",
"tomli"
"tomli",
"cli-nested-json",
]

[project.optional-dependencies]
Expand Down
71 changes: 71 additions & 0 deletions projects/fal/src/fal/cli/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import re

import rich

import fal.apps

# = or := only
KV_SPLIT_RE = re.compile(r"(=|:=)")


def _api(args):
"""Handle the api command execution."""
import cli_nested_json
from rich.console import Group
from rich.live import Live
from rich.panel import Panel
from rich.text import Text

params = [KV_SPLIT_RE.split(param) for param in args.params]
params = cli_nested_json.interpret_nested_json(
[(key, value) for key, _, value in params]
)

handle = fal.apps.submit(args.model_id, params) # type: ignore
logs = [] # type: ignore

with Live(auto_refresh=False) as live:
for event in handle.iter_events(logs=True):
if isinstance(event, fal.apps.Queued):
status = Text(f"⏳ Queued (position: {event.position})", style="yellow")
elif isinstance(event, fal.apps.InProgress):
status = Text("🔄 In Progress", style="blue")
if event.logs:
logs.extend(log.get("message", str(log)) for log in event.logs)
logs = logs[-10:] # Keep only last 10 logs
else:
status = Text("✅ Done", style="green")

status_panel = Panel(status, title="Status")
logs_panel = Panel("\n".join(logs), title="Logs")

live.update(Group(status_panel, logs_panel))
live.refresh()

# Show final result
result = handle.get()
live.update(rich.pretty.Pretty(result))


def add_parser(main_subparsers, parents):
"""Add the api command to the main parser."""
api_help = "Call a fal API endpoint directly"
parser = main_subparsers.add_parser(
"api",
description=api_help,
help=api_help,
parents=parents,
)

parser.add_argument(
"model_id",
help="Name of the Model ID to call",
)

parser.add_argument(
"params",
nargs="*",
help="Key-value pairs (e.g. key=value or nested[a][b]=value)",
)

parser.set_defaults(func=_api)
4 changes: 2 additions & 2 deletions projects/fal/src/fal/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from fal.console import console
from fal.console.icons import CROSS_ICON

from . import apps, auth, create, deploy, doctor, keys, run, runners, secrets
from . import api, apps, auth, create, deploy, doctor, keys, run, runners, secrets
from .debug import debugtools, get_debug_parser
from .parser import FalParser, FalParserExit

Expand All @@ -31,7 +31,7 @@ def _get_main_parser() -> argparse.ArgumentParser:
required=True,
)

for cmd in [auth, apps, deploy, run, keys, secrets, doctor, create, runners]:
for cmd in [auth, apps, deploy, run, keys, secrets, doctor, create, runners, api]:
cmd.add_parser(subparsers, parents)

return parser
Expand Down
Loading