initial commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
.venv/
|
||||
.pytest_cache/
|
||||
.coverage
|
||||
*.egg-info/
|
||||
.env
|
||||
uv.lock
|
||||
*.db
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
# Build context is the ISSSTELEON repo root (see root docker-compose.yml)
|
||||
# so this Dockerfile can COPY its sibling Core/, Persistance/ and models/.
|
||||
FROM python:3.12-slim
|
||||
|
||||
RUN pip install --no-cache-dir uv
|
||||
|
||||
WORKDIR /app
|
||||
COPY Core/ Core/
|
||||
COPY Persistance/ Persistance/
|
||||
COPY models/ models/
|
||||
COPY WebApi/ WebApi/
|
||||
|
||||
RUN cd Persistance && uv sync --no-dev
|
||||
RUN cd WebApi && uv sync --no-dev
|
||||
|
||||
ENV CACU_DATABASE_URL=sqlite:////data/cacu.db
|
||||
ENV CAMA_DATABASE_URL=sqlite:////data/cama.db
|
||||
ENV TEA_DATABASE_URL=sqlite:////data/tea.db
|
||||
|
||||
RUN chmod +x /app/WebApi/docker-entrypoint.sh
|
||||
|
||||
EXPOSE 5000
|
||||
ENTRYPOINT ["/app/WebApi/docker-entrypoint.sh"]
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
mkdir -p /data
|
||||
|
||||
cd /app/Persistance
|
||||
uv run alembic -c persistance/cacu/alembic.ini upgrade head
|
||||
uv run alembic -c persistance/cama/alembic.ini upgrade head
|
||||
uv run alembic -c persistance/tea/alembic.ini upgrade head
|
||||
|
||||
cd /app/WebApi
|
||||
exec uv run python main.py
|
||||
@@ -0,0 +1,6 @@
|
||||
from webapi import create_app
|
||||
|
||||
app = create_app()
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000)
|
||||
@@ -0,0 +1,26 @@
|
||||
[project]
|
||||
name = "webapi"
|
||||
version = "0.1.0"
|
||||
description = "Flask REST API fronting Persistance (reads) and models (train/predict) for CaCu, CaMa and TEA."
|
||||
requires-python = ">=3.11"
|
||||
dependencies = [
|
||||
"flask>=3.0",
|
||||
"persistance",
|
||||
"models",
|
||||
]
|
||||
|
||||
[tool.uv.sources]
|
||||
persistance = { path = "../Persistance", editable = true }
|
||||
models = { path = "../models", editable = true }
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
packages = ["webapi"]
|
||||
|
||||
[dependency-groups]
|
||||
dev = [
|
||||
"pytest>=8.0.0",
|
||||
]
|
||||
@@ -0,0 +1,54 @@
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client():
|
||||
db_path = tempfile.NamedTemporaryFile(suffix=".db", delete=False).name
|
||||
os.environ["CACU_DATABASE_URL"] = f"sqlite:///{db_path}"
|
||||
|
||||
from core.cacu.base import CaCuModel
|
||||
from core.cacu.models import Paciente
|
||||
from persistance.db import get_cacu_engine
|
||||
|
||||
engine = get_cacu_engine()
|
||||
CaCuModel.metadata.create_all(engine)
|
||||
|
||||
from sqlmodel import Session
|
||||
|
||||
with Session(engine) as session:
|
||||
session.add(Paciente(pac_id_hospital="H-1", pac_sexo="Femenino"))
|
||||
session.commit()
|
||||
|
||||
from webapi import create_app
|
||||
|
||||
app = create_app()
|
||||
app.config.update(TESTING=True)
|
||||
with app.test_client() as client:
|
||||
yield client
|
||||
|
||||
os.unlink(db_path)
|
||||
|
||||
|
||||
def test_health(client):
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
assert response.get_json() == {"status": "ok"}
|
||||
|
||||
|
||||
def test_get_paciente(client):
|
||||
response = client.get("/cacu/pacientes/1")
|
||||
assert response.status_code == 200
|
||||
assert response.get_json()["pac_id_hospital"] == "H-1"
|
||||
|
||||
|
||||
def test_get_paciente_not_found(client):
|
||||
response = client.get("/cacu/pacientes/999")
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_predict_not_implemented(client):
|
||||
response = client.post("/cacu/predict")
|
||||
assert response.status_code == 501
|
||||
@@ -0,0 +1,19 @@
|
||||
from flask import Flask
|
||||
|
||||
from webapi.blueprints.cacu import cacu_bp
|
||||
from webapi.blueprints.cama import cama_bp
|
||||
from webapi.blueprints.tea import tea_bp
|
||||
|
||||
|
||||
def create_app() -> Flask:
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
app.register_blueprint(cacu_bp, url_prefix="/cacu")
|
||||
app.register_blueprint(cama_bp, url_prefix="/cama")
|
||||
app.register_blueprint(tea_bp, url_prefix="/tea")
|
||||
|
||||
return app
|
||||
@@ -0,0 +1,25 @@
|
||||
from flask import Blueprint, jsonify
|
||||
|
||||
from models.cacu import CaCuModel
|
||||
from persistance.cacu import paciente_repository
|
||||
|
||||
cacu_bp = Blueprint("cacu", __name__)
|
||||
|
||||
|
||||
@cacu_bp.get("/pacientes/<int:pac_id>")
|
||||
def get_paciente(pac_id: int):
|
||||
"""Example read route — proves WebApi resolves Persistance's repository API."""
|
||||
paciente = paciente_repository.get(pac_id)
|
||||
if paciente is None:
|
||||
return jsonify({"error": "not found"}), 404
|
||||
return jsonify(paciente.model_dump())
|
||||
|
||||
|
||||
@cacu_bp.post("/predict")
|
||||
def predict():
|
||||
"""Example predict route — proves WebApi resolves the models package."""
|
||||
model = CaCuModel()
|
||||
try:
|
||||
return jsonify({"prediction": model.predict(data=None)})
|
||||
except NotImplementedError as exc:
|
||||
return jsonify({"error": str(exc)}), 501
|
||||
@@ -0,0 +1,23 @@
|
||||
from flask import Blueprint, jsonify
|
||||
|
||||
from models.cama import CaMaModel
|
||||
from persistance.cama import paciente_repository
|
||||
|
||||
cama_bp = Blueprint("cama", __name__)
|
||||
|
||||
|
||||
@cama_bp.get("/pacientes/<int:pac_id>")
|
||||
def get_paciente(pac_id: int):
|
||||
paciente = paciente_repository.get(pac_id)
|
||||
if paciente is None:
|
||||
return jsonify({"error": "not found"}), 404
|
||||
return jsonify(paciente.model_dump())
|
||||
|
||||
|
||||
@cama_bp.post("/predict")
|
||||
def predict():
|
||||
model = CaMaModel()
|
||||
try:
|
||||
return jsonify({"prediction": model.predict(data=None)})
|
||||
except NotImplementedError as exc:
|
||||
return jsonify({"error": str(exc)}), 501
|
||||
@@ -0,0 +1,23 @@
|
||||
from flask import Blueprint, jsonify
|
||||
|
||||
from models.tea import TeaModel
|
||||
from persistance.tea import pacientes_repository
|
||||
|
||||
tea_bp = Blueprint("tea", __name__)
|
||||
|
||||
|
||||
@tea_bp.get("/pacientes/<string:pac_id>")
|
||||
def get_paciente(pac_id: str):
|
||||
paciente = pacientes_repository.get(pac_id)
|
||||
if paciente is None:
|
||||
return jsonify({"error": "not found"}), 404
|
||||
return jsonify(paciente.model_dump())
|
||||
|
||||
|
||||
@tea_bp.post("/predict")
|
||||
def predict():
|
||||
model = TeaModel()
|
||||
try:
|
||||
return jsonify({"prediction": model.predict(data=None)})
|
||||
except NotImplementedError as exc:
|
||||
return jsonify({"error": str(exc)}), 501
|
||||
Reference in New Issue
Block a user