File size: 5,793 Bytes
194688b 0cce186 194688b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
from fastapi import FastAPI
from pydantic import BaseModel, Field
from typing import Literal
import json
import numpy as np
import onnxruntime as ort
from typing_extensions import Annotated
import gradio as gr
from cryptography.fernet import Fernet
import os
# Model load
key = os.getenv("ONNX_KEY")
cipher = Fernet(key)
VERSION = "0.0.1"
TITLE = f"DVPI beregnings API (version {VERSION})"
DESCRIPTION = "Beregn Dansk Vandløbs Plante Indeks (DVPI) fra dækningsgrad af plantearter. Beregningen er baseret på en model som efterligner DVPI beregningsmetoden og er dermed ikke eksakt, usikkerheden er i gennemsnit **±0.05 EQR-enheder**."
URL = "https://kennethtm-dvpi.hf.space"
# Load ONNX model and species mappings
with open("model.bin", "rb") as f:
encrypted = f.read()
decrypted = cipher.decrypt(encrypted)
ort_session = ort.InferenceSession(decrypted)
with open("spec2idx.json", "r") as f:
spec2idx = json.load(f)
# Define types
valid_species = tuple(spec2idx.keys())
class SpeciesCover(BaseModel):
species: dict[Literal[valid_species], Annotated[float, Field(ge=0, le=100)]]
model_config = {
"json_schema_extra": {
"examples": [{
"species": {
"Potamogeton alpinus": 25.0,
"Berula erecta": 15.5,
"Calamagrostis canescens": 10.0
}
}]
}
}
class EQRResult(BaseModel):
EQR: float # Round to 2 decimals
DVPI: int
version: str = VERSION
# Create FastAPI app
app = FastAPI(title=TITLE,
description=DESCRIPTION)
def eqr_to_dvpi(eqr: float) -> int:
if eqr < 0.20:
return 1
elif eqr < 0.35:
return 2
elif eqr < 0.50:
return 3
elif eqr < 0.70:
return 4
else:
return 5
# FastAPI routes
@app.post("/dvpi")
def predict(cover_data: SpeciesCover) -> EQRResult:
"""Predict EQR and DVPI from species cover data"""
# Initialize input vector with zeros
input_vector = np.zeros((1, len(spec2idx)))
print(cover_data.species)
# Fill values from input
for species, cover in cover_data.species.items():
idx = spec2idx[species]
input_vector[0, idx] = cover
# Get prediction
input_name = ort_session.get_inputs()[0].name
ort_inputs = {input_name: input_vector.astype(np.float32)}
ort_output = ort_session.run(None, ort_inputs)
eqr = float(ort_output[0][0])
dvpi = eqr_to_dvpi(eqr)
return EQRResult(EQR=round(eqr, 2), DVPI=dvpi)
@app.get("/arter")
def list_species() -> dict:
"""Return list of valid species names"""
return {"species": list(spec2idx.keys())}
# Gradio app
def add_entry(species, cover, current_dict) -> tuple[SpeciesCover, str]:
current_dict[species] = cover
return current_dict, current_dict
def gradio_predict(cover_data: dict):
if len(cover_data) == 0:
return {}
data = SpeciesCover(species=cover_data)
result = predict(data)
return result.model_dump()
with gr.Blocks() as io:
gr.Markdown(f"# {TITLE}")
gr.Markdown(DESCRIPTION)
with gr.Tab(label = "Beregner"):
gr.Markdown("Beregning er baseret på samfund af plantearter og deres dækningsgrad. Dækningsgraden angives i procent som summen af scoren for dækningsgraden (1-5) divideret med det samlede antal undersøgte kvadrater gange 5, og til sidste konverteret til procent. Eksempel: Potamogeton alpinus findes 3 felter med scorerne 2, 3 og 5 ud af 50 undersøgte kvadrater. Dækningsgraden for Potamogeton alpinus er derfor (2+3+5)/(50*5)*100 = 4%.")
current_dict = gr.State({})
with gr.Row():
species_input = gr.Dropdown(choices=valid_species, label="Vælg art")
cover_input = gr.Number(label="Dækningsgrad (%)", minimum=0, maximum=100)
with gr.Row():
add_btn = gr.Button("Tilføj")
reset_btn = gr.Button("Nulstil")
list_display = gr.JSON(label="Artsliste")
calc_btn = gr.Button("Beregn")
results = gr.JSON(label="Resultater")
def reset_dict():
return {}, {}, {}
add_btn.click(
add_entry,
inputs=[species_input, cover_input, current_dict],
outputs=[current_dict, list_display]
)
reset_btn.click(
reset_dict,
inputs=[],
outputs=[current_dict, list_display, results]
)
calc_btn.click(
gradio_predict,
inputs=[current_dict],
outputs=results
)
gr.Markdown("App og model af Kenneth Thorø Martinsen.")
with gr.Tab(label="Dokumentation"):
# Add markdown description with code to call the api in python
gr.Markdown("## Eksempel på brug af API")
gr.Markdown(f"API dokumentation kan findes på [{URL}/docs]({URL}/docs)")
gr.Markdown("### Python")
gr.Code(f"""
import requests
import json
data = {{
"species": {{
"Potamogeton alpinus": 25.0,
"Berula erecta": 15.5,
"Calamagrostis canescens": 10.0
}}
}}
response = requests.post("{URL}/dvpi", json=data)
print(response.json())
""")
gr.Markdown("### R")
gr.Code(f"""
library(httr)
library(jsonlite)
data <- list(species = list(
"Potamogeton alpinus" = 25.0,
"Berula erecta" = 15.5,
"Calamagrostis canescens" = 10.0
))
response <- POST("{URL}/dvpi",
body = toJSON(data, auto_unbox = TRUE),
content_type("application/json"))
print(fromJSON(rawToChar(response$content)))
""")
# Mount Gradio app
app = gr.mount_gradio_app(app, io, path="/")
|