Skip to content

Commit

Permalink
WIP: app: add GET /api/model
Browse files Browse the repository at this point in the history
This will be used by the UI to get the available wakenet models, in
preparation of dynamically updating the supported wake words on Willow.

For development I'm testing with a local JSON file and manually
generated tarballs. JSON content:

[
  {
    "model_friendly_name": "Alexa",
    "model_name": "wn9_alexa",
    "url": "http://was.dev.willow/wn9_alexa.tar.gz"
  },
  {
    "model_friendly_name": "Computer",
    "model_name": "wn9_computer_tts",
    "url": "http://was.dev.willow/wn9_computer_tts.tar.gz"
  }
]

Example content of a tarball:

drwxr-xr-x stijn/users       0 2023-05-10 05:06 wn9_alexa/
-rw-r--r-- stijn/users     168 2023-05-10 05:06 wn9_alexa/_MODEL_INFO_
-rw-r--r-- stijn/users  289638 2023-05-10 05:06 wn9_alexa/wn9_data
-rw-r--r-- stijn/users    1200 2023-05-10 05:06 wn9_alexa/wn9_index
-rw-r--r-- stijn/users       6 2023-12-15 23:23 wn9_alexa.txt

The txt file contains the friendly form of the wake word, for the
example above that would be "Alexa". We need this for the Willow help
text: "Say 'Alexa' to start!". We currently hardcode this full text in
the Willow C code, but only for 3 wake words. This will break once we
flash a wake word that is not included in the model partition during
build/flash.

We should also keep this friendly name in the JSON so that the UI can
just read that property to fill the dropdown.

TODO:
* implement getting JSON and tarball in CF worker
* add version in JSON - we can parse this from _MODEL_INFO_ but can we
  get the version of the models using an ESP-SR API?
  • Loading branch information
stintel committed Dec 20, 2023
1 parent 8848752 commit 73e76f8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
DB_URL = 'sqlite:////app/storage/was.db'
DIR_ASSET = '/app/storage/asset'
DIR_OTA = '/app/storage/ota'
URL_WILLOW_MODELS = 'http://was.dev.willow/willow-srmodels.json'
URL_WILLOW_RELEASES = 'https://worker.heywillow.io/api/release?format=was'
URL_WILLOW_CONFIG = 'https://worker.heywillow.io/api/config'
URL_WILLOW_TZ = 'https://worker.heywillow.io/api/asset?type=tz'
Expand Down
2 changes: 2 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from .routers import asset
from .routers import client
from .routers import config
from .routers import model
from .routers import ota
from .routers import release
from .routers import status
Expand Down Expand Up @@ -171,6 +172,7 @@ def api_redirect_admin():
app.include_router(asset.router)
app.include_router(client.router)
app.include_router(config.router)
app.include_router(model.router)
app.include_router(ota.router)
app.include_router(release.router)
app.include_router(status.router)
Expand Down
42 changes: 42 additions & 0 deletions app/routers/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from logging import getLogger
from typing import Literal

from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from requests import get

from ..const import URL_WILLOW_MODELS


log = getLogger("WAS")
router = APIRouter(
prefix="/api",
)


def get_models():
models = []

try:
models = get(URL_WILLOW_MODELS)
models = models.json()
except Exception:
raise HTTPException(status_code=500, detail="failed to get models")

return models


class GetModel(BaseModel):
type: Literal["wakenet"] = Field(
Query(..., description="Model type")
)


@router.get("/model")
async def api_get_model(asset: GetModel = Depends()):
log.debug("API GET MODEL: Request")

models = get_models()

return JSONResponse(content=models)

0 comments on commit 73e76f8

Please sign in to comment.