Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +82 -0
- idx_to_target.json +1 -0
- model.bin +3 -0
- requirements.txt +2 -0
- species_to_idx.json +0 -0
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import onnxruntime as ort
|
| 4 |
+
import json
|
| 5 |
+
from cryptography.fernet import Fernet
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Model load
|
| 12 |
+
key = os.getenv("ARTER_MODEL_KEY")
|
| 13 |
+
cipher = Fernet(key)
|
| 14 |
+
|
| 15 |
+
with open("model.bin", "rb") as f:
|
| 16 |
+
encrypted = f.read()
|
| 17 |
+
decrypted = cipher.decrypt(encrypted)
|
| 18 |
+
ort_session = ort.InferenceSession(decrypted)
|
| 19 |
+
|
| 20 |
+
# Initialize ONNX session
|
| 21 |
+
input_name = ort_session.get_inputs()[0].name
|
| 22 |
+
|
| 23 |
+
def softmax(x):
|
| 24 |
+
return np.exp(x) / np.sum(np.exp(x), axis=1)
|
| 25 |
+
|
| 26 |
+
# Load mappings
|
| 27 |
+
with open("species_to_idx.json", "r") as f:
|
| 28 |
+
species_to_idx = json.load(f)
|
| 29 |
+
|
| 30 |
+
with open("idx_to_target.json", "r") as f:
|
| 31 |
+
idx_to_target = json.load(f)
|
| 32 |
+
|
| 33 |
+
target_list = list(idx_to_target.keys())
|
| 34 |
+
|
| 35 |
+
def predict(species):
|
| 36 |
+
|
| 37 |
+
if not species:
|
| 38 |
+
return {}, {}, {}
|
| 39 |
+
|
| 40 |
+
input = [species_to_idx[s] for s in species]
|
| 41 |
+
input_np = np.array(input, dtype=np.int64).reshape(1, -1)
|
| 42 |
+
|
| 43 |
+
output = ort_session.run([], {input_name: input_np})
|
| 44 |
+
output = {target: softmax(output[i]) for i, target in enumerate(target_list)}
|
| 45 |
+
|
| 46 |
+
format_output = []
|
| 47 |
+
|
| 48 |
+
for target in target_list:
|
| 49 |
+
probs = output[target][0]
|
| 50 |
+
classes = idx_to_target[target]
|
| 51 |
+
format_output.append({classes[str(i)]: float(p) for i, p in enumerate(probs)})
|
| 52 |
+
|
| 53 |
+
return format_output
|
| 54 |
+
|
| 55 |
+
# Gradio interface
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("## Naturtype og §3 klassifikation baseret på artsfund")
|
| 58 |
+
gr.Markdown("Dette værktøj analyserer artsregistreringer for at bestemme et områdes hovednaturtype, naturtilstand og om området er omfattet af Naturbeskyttelseslovens paragraf 3.")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
choices = sorted(species_to_idx.keys())
|
| 62 |
+
species_dropdown = gr.Dropdown(
|
| 63 |
+
choices=choices,
|
| 64 |
+
multiselect=True,
|
| 65 |
+
label="Vælg arter",
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
naturtype = gr.Label("", label="Hovednaturtype")
|
| 70 |
+
p3 = gr.Label("", label="§3 område")
|
| 71 |
+
tilstand = gr.Label("", label="Naturtilstand")
|
| 72 |
+
|
| 73 |
+
gr.Markdown("App og model af Kenneth Thorø Martinsen (kenneth2810@gmail.com).")
|
| 74 |
+
|
| 75 |
+
species_dropdown.change(
|
| 76 |
+
predict,
|
| 77 |
+
inputs=[species_dropdown],
|
| 78 |
+
outputs=[naturtype, p3, tilstand]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|
idx_to_target.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"hovednaturtype": {"0": "Ferskeng", "1": "Hede", "2": "Ikke en \u00a73 naturtype", "3": "Mose og K\u00e6r", "4": "Overdrev", "5": "Skov", "6": "Strandenge, strandsumpe", "7": "Strandklit", "8": "S\u00f8"}, "arealet_nbl": {"0": "ja", "1": "nej"}, "naturtilstand": {"0": "I", "1": "II", "2": "III", "3": "IV", "4": "V"}}
|
model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2b5d57bf5b46a41a1add8cd98daedf6578ada17d793b5e7c557117c04a9a5a8a
|
| 3 |
+
size 2418872
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cryptography
|
| 2 |
+
onnxruntime
|
species_to_idx.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|