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

Feature/update templates #180

Merged
merged 5 commits into from
Feb 24, 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
2 changes: 1 addition & 1 deletion .github/workflows/test_and_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 22
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_pull_request_branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 22
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dist/
downloads/
eggs/
.eggs/
lib/
./lib/
lib64/
parts/
sdist/
Expand Down Expand Up @@ -61,4 +61,5 @@ demo.db
test.db

# Templates
flama/templates
flama/templates
templates/out
29 changes: 14 additions & 15 deletions examples/add_model_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import flama
from flama import Flama
from flama.models import ModelComponent, ModelResource, ModelResourceType
from flama.models import BaseModelResource, ModelComponent
from flama.models.base import Model
from flama.resources import resource_method

Expand All @@ -19,30 +19,29 @@ def predict(self, x: typing.Any) -> typing.Any:
class MyCustomModelComponent(ModelComponent):
def __init__(self, model_path: str):
self._model_path = model_path
self.model = MyCustomModel(None)
self.model = None

def load_model(self):
with open(self._model_path, "rb") as f:
self.model = MyCustomModel(flama.load(f).model)
def load(self):
load_model = flama.load(self._model_path)
self.model = MyCustomModel(load_model.model, load_model.meta, load_model.artifacts)

def unload_model(self):
self.model = MyCustomModel(None)
def reset(self):
self.model = None

def resolve(self) -> MyCustomModel:
if not self.model.model:
self.load_model()
if not self.model:
self.load()

assert self.model
return self.model


component = MyCustomModelComponent("sklearn_model.flm")


# component = MyCustomModelComponent("pytorch_model.flm")
# component = MyCustomModelComponent("tensorflow_model.flm")


class MyCustomModelResource(ModelResource, metaclass=ModelResourceType):
class MyCustomModelResource(BaseModelResource[MyCustomModelComponent]):
name = "custom_model"
verbose_name = "Lazy-loaded ScikitLearn Model"
component = component
Expand All @@ -61,7 +60,7 @@ def _get_metadata(self):
},
"custom": {
**self.info,
"loaded": self.component.model.model is not None,
"loaded": self.component.model is not None,
"date": datetime.now().date(),
"time": datetime.now().time(),
},
Expand All @@ -76,7 +75,7 @@ def unload(self):
summary:
Unload the model.
"""
self.component.unload_model()
self.component.reset()
return self._get_metadata()

@resource_method("/metadata/", methods=["GET"], name="metadata-method")
Expand All @@ -98,7 +97,7 @@ def metadata(self):
components=[component],
)

app.models.add_model_resource(path="/model", resource=MyCustomModelResource)
app.models.add_model_resource(path="/model", resource=MyCustomModelComponent)

if __name__ == "__main__":
flama.run(flama_app="__main__:app", server_host="0.0.0.0", server_port=8080, server_reload=True)
22 changes: 12 additions & 10 deletions examples/add_model_resource.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import typing
import typing as t
from datetime import datetime

from pydantic import BaseModel, Field
import pydantic

import flama
from flama import Flama
from flama.models import ModelResource, ModelResourceType
from flama import Flama, schemas
from flama.models import ModelResource
from flama.resources import resource_method

app = Flama(
Expand All @@ -16,19 +16,19 @@
)


class X(BaseModel):
input: list[typing.Any] = Field(title="input", description="Model input")
class X(pydantic.BaseModel):
input: list[t.Any] = pydantic.Field(title="input", description="Model input")


class Y(BaseModel):
output: list[typing.Any] = Field(title="output", description="Model output")
class Y(pydantic.BaseModel):
output: list[t.Any] = pydantic.Field(title="output", description="Model output")


app.schema.register_schema("X", X)
app.schema.register_schema("Y", Y)


class MySKModel(ModelResource, metaclass=ModelResourceType):
class MySKModel(ModelResource):
# special names:
name = "sk_model"
verbose_name = "My ScikitLearn Model"
Expand All @@ -41,7 +41,9 @@ class MySKModel(ModelResource, metaclass=ModelResourceType):
}

@resource_method("/predict/", methods=["POST"], name="model-predict")
def predict(self, data: X) -> Y:
def predict(
self, data: t.Annotated[schemas.SchemaType, schemas.SchemaMetadata(X)]
) -> t.Annotated[schemas.SchemaType, schemas.SchemaMetadata(Y)]:
"""
tags:
- My ScikitLearn Model
Expand Down
8 changes: 4 additions & 4 deletions examples/add_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

import flama
from flama import Flama, Route
from flama import Flama, routing


class AppStatus:
Expand Down Expand Up @@ -64,9 +64,9 @@ def user(username: str):
version="0.1.0",
description="Machine learning API using Flama 🔥",
routes=[
Route("/", home),
Route("/user/me", user_me),
Route("/user/{username}", user),
routing.Route("/", home),
routing.Route("/user/me", user_me),
routing.Route("/user/{username}", user),
],
events={"startup": [startup], "shutdown": [shutdown]},
)
Expand Down
6 changes: 3 additions & 3 deletions examples/background.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio

import flama
from flama import BackgroundThreadTask, Flama
from flama import Flama, background
from flama.http import JSONResponse

app = Flama()
Expand All @@ -13,9 +13,9 @@ async def sleep_task(value: int):

@app.route("/")
async def test():
task = BackgroundThreadTask(sleep_task, 10)
task = background.BackgroundThreadTask(sleep_task, 10)
return JSONResponse("hello", background=task)


if __name__ == "__main__":
flama.run(app, host="0.0.0.0", port=8000)
flama.run(flama_app=app, server_host="0.0.0.0", server_port=8080)
2 changes: 1 addition & 1 deletion examples/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def person(person: Person):


if __name__ == "__main__":
flama.run(app, host="0.0.0.0", port=8000)
flama.run(flama_app=app, server_host="0.0.0.0", server_port=8080)
18 changes: 11 additions & 7 deletions examples/data_schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pydantic import BaseModel, validator
import typing as t

import pydantic

import flama
from flama import Flama
from flama import Flama, schemas

app = Flama(
title="Puppy Register", # API title
Expand All @@ -12,12 +14,12 @@
)


class Puppy(BaseModel):
class Puppy(pydantic.BaseModel):
id: int
name: str
age: int

@validator("age")
@pydantic.field_validator("age")
def minimum_age_validation(cls, v):
if v < 0:
raise ValueError("Age must be positive")
Expand All @@ -32,7 +34,7 @@ def home():
return {"hello": "world"}


def list_puppies(name: str = None) -> list[Puppy]:
def list_puppies(name: t.Optional[str] = None) -> t.Annotated[list[schemas.SchemaType], schemas.SchemaMetadata(Puppy)]:
"""
tags:
- puppy
Expand All @@ -48,7 +50,9 @@ def list_puppies(name: str = None) -> list[Puppy]:
...


def create_puppy(puppy: Puppy) -> Puppy:
def create_puppy(
puppy: t.Annotated[schemas.SchemaType, schemas.SchemaMetadata(Puppy)],
) -> t.Annotated[schemas.SchemaType, schemas.SchemaMetadata(Puppy)]:
"""
tags:
- puppy
Expand All @@ -68,4 +72,4 @@ def create_puppy(puppy: Puppy) -> Puppy:
app.add_route("/puppy/", create_puppy, methods=["POST"])

if __name__ == "__main__":
flama.run(app, host="0.0.0.0", port=8000)
flama.run(flama_app=app, server_host="0.0.0.0", server_port=8080)
13 changes: 7 additions & 6 deletions examples/error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import flama
from flama import Flama, routing

app = flama.Flama(
app = Flama(
title="Hello-🔥",
version="1.0",
description="My first API",
Expand All @@ -26,13 +27,13 @@ def home():
return {"message": "Hello 🔥"}


error_app = flama.Router()
error_app = routing.Router()


class FooException(Exception): ...


@error_app.route("/500")
@error_app.route("/500/")
def error_500():
"""
tags:
Expand All @@ -51,7 +52,7 @@ def error_500():
app.mount("/error", app=error_app)

# Mount a complex urls tree to illustrate in 404 error page
bar_app = flama.Router()
bar_app = routing.Router()


@bar_app.route("/foobar/")
Expand All @@ -62,10 +63,10 @@ def foobar(): ...
def barfoo(): ...


foo_app = flama.Router()
foo_app = routing.Router()
foo_app.mount("/bar/", bar_app)

app.mount("/foo/", foo_app)

if __name__ == "__main__":
flama.run(app, host="0.0.0.0", port=8000)
flama.run(flama_app=app, server_host="0.0.0.0", server_port=8080)
4 changes: 4 additions & 0 deletions examples/ml_responsive_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import torch

import flama
from flama import Flama
from flama.models import ModelComponent, ModelResource, ModelResourceType
from flama.models.base import Model
Expand Down Expand Up @@ -63,3 +64,6 @@ class CustomModelResource(ModelResource, metaclass=ModelResourceType):

app.add_component(component)
app.models.add_model_resource("/", CustomModelResource)

if __name__ == "__main__":
flama.run(flama_app=app, server_host="0.0.0.0", server_port=8080)
4 changes: 4 additions & 0 deletions examples/ml_responsive_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from sklearn.linear_model import LogisticRegression

import flama
from flama import Flama
from flama.models import ModelComponent, ModelResource, ModelResourceType
from flama.models.base import Model
Expand Down Expand Up @@ -58,3 +59,6 @@ class CustomModelResource(ModelResource, metaclass=ModelResourceType):

app.add_component(component)
app.models.add_model_resource("/", CustomModelResource)

if __name__ == "__main__":
flama.run(flama_app=app, server_host="0.0.0.0", server_port=8080)
4 changes: 4 additions & 0 deletions examples/ml_responsive_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import tensorflow as tf

import flama
from flama import Flama
from flama.models import ModelComponent, ModelResource, ModelResourceType
from flama.models.base import Model
Expand Down Expand Up @@ -64,3 +65,6 @@ class CustomModelResource(ModelResource, metaclass=ModelResourceType):

app.add_component(component)
app.models.add_model_resource("/", CustomModelResource)

if __name__ == "__main__":
flama.run(flama_app=app, server_host="0.0.0.0", server_port=8080)
Loading
Loading