KennethTM commited on
Commit
194688b
·
verified ·
1 Parent(s): f2fc5f6

Create app.py

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