-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added web server for deployment (#27)
* refactor: Updated package import hierarchy * chore: Removed unused dependencies * feat: Added dummy cache mechanism * feat: Added extra information logging * feat: Added FastAPI web server for deployment * chore: Added docker orchestration * feat: Added date as a route argument * refactor: Reversed cache to prevent system running OOM * refactor: Silenced urllib warnings * chore: Added Heroku setup file * chore: Build debug * chore: Updated heroku setup file * chore: Added apt prebuild * chore: Build debug * chore: Fixed procfile * chore: Added back requirements * refactor: Reflected package import fix * refactor: Removed unnecessary dependencies * chore: Added workflow to check web server sanity * style: Fixed lint * refactor: Removed unused import * chore: Fixed CI config * chore: Fixed workflows * feat: Added possibility to load env variables from .env * docs: Updated README
- Loading branch information
Showing
21 changed files
with
234 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: web-server | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
docker-ready: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Build & run docker | ||
env: | ||
CDS_UID: ${{ secrets.CDS_UID }} | ||
CDS_API_KEY: ${{ secrets.CDS_API_KEY }} | ||
run: PORT=8003 docker-compose up -d --build | ||
- name: Ping app inside the container | ||
run: sleep 5 && nc -vz localhost 8003 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
libspatialindex-dev | ||
python3-rtree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
FROM python:3.8.1 | ||
|
||
# set work directory | ||
WORKDIR /usr/src/app | ||
|
||
# set environment variables | ||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
|
||
# copy app requirements | ||
COPY ./requirements.txt requirements.txt | ||
COPY ./requirements-app.txt /usr/src/app/requirements-app.txt | ||
COPY ./setup.py setup.py | ||
COPY ./README.md README.md | ||
COPY ./pyro_risks pyro_risks | ||
|
||
# install dependencies | ||
RUN apt-get update && \ | ||
apt-get install --no-install-recommends -y libspatialindex-dev python3-rtree && \ | ||
pip install --upgrade pip setuptools wheel && \ | ||
pip install -e . && \ | ||
pip install -r /usr/src/app/requirements-app.txt && \ | ||
mkdir /usr/src/app/app && \ | ||
rm -rf /root/.cache/pip && \ | ||
rm -rf /var/lib/apt/lists/* | ||
|
||
# copy project | ||
COPY app/ /usr/src/app/app/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: pip install -e . && pip install -r requirements-app.txt && uvicorn --reload --workers 1 --host 0.0.0.0 --port=${PORT:-5000} app.main:app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pyro_risks.models.predict import PyroRisk | ||
|
||
|
||
__all__ = ['predictor'] | ||
|
||
|
||
predictor = PyroRisk(which='RF') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import List | ||
from fastapi import APIRouter | ||
from app.api.inference import predictor | ||
from app.api.schemas import RegionRisk | ||
|
||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/{country}/{date}", response_model=List[RegionRisk], summary="Computes the wildfire risk") | ||
async def get_pyrorisk(country: str, date: str): | ||
"""Using the country identifier, this will compute the wildfire risk for all known subregions""" | ||
preds = predictor.predict(date) | ||
return [RegionRisk(geocode=k, score=v['score'], explainability=v['explainability']) for k, v in preds.items()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from typing import Optional | ||
from pydantic import BaseModel, Field | ||
|
||
|
||
class RegionRisk(BaseModel): | ||
geocode: str = Field(..., example="01") | ||
score: float = Field(..., gt=0, lt=1, example=0.5) | ||
explainability: Optional[str] = Field(None, example="weather") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import os | ||
import secrets | ||
|
||
|
||
PROJECT_NAME: str = 'PyroRisk' | ||
PROJECT_DESCRIPTION: str = 'Wildfire risk estimation' | ||
VERSION: str = "0.1.0a0" | ||
DEBUG: bool = os.environ.get('DEBUG', '') != 'False' | ||
LOGO_URL: str = "https://pyronear.org/img/logo_letters.png" | ||
|
||
|
||
SECRET_KEY: str = secrets.token_urlsafe(32) | ||
if DEBUG: | ||
# To keep the same Auth at every app loading in debug mode and not having to redo the auth. | ||
debug_secret_key = "000000000000000000000000000000000000" | ||
SECRET_KEY = debug_secret_key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import time | ||
from fastapi import FastAPI, Request | ||
from fastapi.openapi.utils import get_openapi | ||
|
||
from app import config as cfg | ||
from app.api.routes import risk | ||
|
||
|
||
app = FastAPI(title=cfg.PROJECT_NAME, description=cfg.PROJECT_DESCRIPTION, debug=cfg.DEBUG, version=cfg.VERSION) | ||
|
||
# Routing | ||
app.include_router(risk.router, prefix="/risk", tags=["risk"]) | ||
|
||
|
||
# Middleware | ||
@app.middleware("http") | ||
async def add_process_time_header(request: Request, call_next): | ||
start_time = time.time() | ||
response = await call_next(request) | ||
process_time = time.time() - start_time | ||
response.headers["X-Process-Time"] = str(process_time) | ||
return response | ||
|
||
|
||
# Docs | ||
def custom_openapi(): | ||
if app.openapi_schema: | ||
return app.openapi_schema | ||
openapi_schema = get_openapi( | ||
title=cfg.PROJECT_NAME, | ||
version=cfg.VERSION, | ||
description=cfg.PROJECT_DESCRIPTION, | ||
routes=app.routes, | ||
) | ||
openapi_schema["info"]["x-logo"] = {"url": cfg.LOGO_URL} | ||
app.openapi_schema = openapi_schema | ||
return app.openapi_schema | ||
|
||
|
||
app.openapi = custom_openapi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: '3.7' | ||
|
||
services: | ||
web: | ||
build: . | ||
command: uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000 | ||
volumes: | ||
- ./app/:/usr/src/app/app/ | ||
ports: | ||
- ${PORT}:8000 | ||
environment: | ||
- CDS_UID=${CDS_UID} | ||
- CDS_API_KEY=${CDS_API_KEY} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .version import __version__ | ||
from pyro_risks import datasets | ||
from pyro_risks import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .predict import * | ||
from .score_v0 import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fastapi==0.61.1 | ||
uvicorn>=0.11.1 | ||
pyro_risks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters