File size: 8,913 Bytes
a725af0 18350b3 a725af0 88563b5 869adb6 88563b5 869adb6 995b526 a725af0 f1a6252 355f278 f1a6252 a63231d 21dae66 a63231d 21dae66 a63231d 3ae828c baec762 f1a6252 3ae828c 21dae66 ce5e1a3 21dae66 ce5e1a3 21dae66 ce5e1a3 f1a6252 21dae66 a63231d f1a6252 a725af0 355f278 ce5e1a3 18350b3 f1a6252 18350b3 a725af0 21dae66 18350b3 869adb6 18350b3 3ae828c f1a6252 869adb6 f1a6252 ce5e1a3 f1a6252 18350b3 f1a6252 ce5e1a3 f1a6252 18350b3 a725af0 f1a6252 ce5e1a3 f1a6252 18350b3 f1a6252 21dae66 f1a6252 18350b3 f1a6252 21dae66 f1a6252 a63231d f1a6252 a63231d 995b526 f1a6252 18350b3 f1a6252 18350b3 88563b5 f1a6252 995b526 f1a6252 995b526 f1a6252 18350b3 995b526 f1a6252 a725af0 995b526 a725af0 f1a6252 869adb6 f1a6252 869adb6 18350b3 995b526 f1a6252 995b526 f1a6252 869adb6 f1a6252 869adb6 18350b3 f1a6252 3ae828c 995b526 f1a6252 a725af0 18350b3 |
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
import uuid
from pydantic import ValidationError
import gradio as gr
from circumstances.class_circumstance import Circumstances
from behavior.class_behavior import Behaviors
from behavior.class_behavior_simple import BehaviorsSimple
from physical.class_physical import PhysicalAnomalies
from physical.class_physical_simple import PhysicalAnomaliesSimple
from follow_up.class_follow_up import FollowUpEvents
from classes import Report, Wounded, Dead, ImageBase64
from validation_submission.processing import (
process_circumstance,
process_behaviors,
process_physical,
process_followup,
)
from validation_submission.utils_individual import generate_random_md5
from validation_submission.resets import reset_error_box
from dotenv import load_dotenv
import os
load_dotenv()
PATH = os.getcwd() + "/"
PATH_ASSETS = os.getenv("PATH_ASSETS")
PATH_ICONS = PATH + PATH_ASSETS + "icons/"
def get_fields(data_dict, keyword):
extract = {}
for key, val in data_dict.items():
if keyword in key:
extract[key] = val
return extract
def field_checker(data):
img = ImageBase64.to_base64(data["image"]) if "image" in data.keys() else None
geolocalisation = (
data["geolocalisation"] if "geolocalisation" in data.keys() else None
)
specie = data["specie"] if "specie" in data.keys() else "NA"
number = data["number"] if "specie" in data.keys() else 1
comments = data["comments"] if "specie" in data.keys() else "NA"
return img, geolocalisation, specie, number, comments
def validate_individual(data, error_icon, error_box, mode: str):
error_icon, error_box = reset_error_box(error_icon, error_box)
# data = get_json_one_individual() # TODO: This should change
data["identifier"] = str(uuid.uuid4())
data["image_md5"] = generate_random_md5()
img, geolocalisation, specie, number, comments = field_checker(data)
error_behavior = None
error_circumstance = None
error_followup = None
error_physical = None
error_individual = None
if "wounded_state" not in data or "dead_state" not in data:
data["wounded_state"] = "No"
data["dead_state"] = "No"
if (data["wounded_state"] == "Yes") or (data["dead_state"] == "Yes"):
data_wounded_dead = data
circumstance, error_circumstance = validate_circumstance(data_wounded_dead)
physical, error_physical = validate_physical(data_wounded_dead, mode)
followup, error_followup = validate_follow_up(data_wounded_dead)
if data["wounded_state"] == "Yes":
behavior, error_behavior = validate_behavior(data_wounded_dead, mode)
try:
individual = Report(
identifier=data["identifier"],
image=img,
image_md5=data["image_md5"],
geolocalisation=geolocalisation,
specie=specie,
number=number,
comments=comments,
wounded_state=data["wounded_state"],
wounded=Wounded(
circumstances=circumstance,
behaviors=behavior,
physical_anomalies=physical,
follow_up_events=followup,
),
dead_state=data["dead_state"],
)
except ValidationError as e:
error_individual = e
elif data["dead_state"] == "Yes":
try:
individual = Report(
identifier=data["identifier"],
image=img,
image_md5=data["image_md5"],
geolocalisation=geolocalisation,
specie=specie,
number=number,
comments=comments,
wounded_state=data["wounded_state"],
dead_state=data["dead_state"],
dead=Dead(
circumstances=circumstance,
physical_anomalies=physical,
follow_up_events=followup,
),
)
except ValidationError as e:
error_individual = e
else:
try:
individual = Report(
identifier=data["identifier"],
image=img,
image_md5=data["image_md5"],
geolocalisation=geolocalisation,
specie=specie,
number=number,
comments=comments,
wounded_state=data["wounded_state"],
dead_state=data["dead_state"],
)
except ValidationError as e:
error_individual = e
if (
error_behavior
or error_circumstance
or error_followup
or error_physical
or error_individual
):
error_icon = gr.Image(
PATH_ICONS + "supprimer.png",
height=80,
width=80,
interactive=False,
show_fullscreen_button=False,
show_share_button=False,
show_download_button=False,
show_label=False,
visible=True,
)
error_box = show_error(
error_box,
error_behavior,
error_circumstance,
error_followup,
error_physical,
error_individual,
)
individual = None
else:
error_icon = gr.Image(
PATH_ICONS + "correct.png",
height=80,
width=80,
interactive=False,
show_fullscreen_button=False,
show_share_button=False,
show_download_button=False,
show_label=False,
visible=True,
)
error_box = gr.Text(
label="ALL VALID.",
value="Record Registered. Remember to press the CLEAR button before submitting a new record.",
visible=True,
elem_id="valid",
)
return individual, error_icon, error_box
def show_error(
error_box,
error_behavior,
error_circumstance,
error_followup,
error_physical,
error_individual,
):
error_text = ""
if error_circumstance:
error_text += f"Error in circumstance: {error_circumstance}\n"
if error_behavior:
error_text += f"Error in behavior: {error_behavior}\n"
if error_physical:
error_text += f"Error in physical: {error_physical}\n"
if error_followup:
error_text += f"Error in follow-up: {error_followup}\n"
if error_individual:
error_text += f"Error in individual: {error_individual}\n"
error_text += "PLEASE CORRECT THESE ERRORS BEFORE SUBMITTING."
error_box = gr.Text(
label="ERROR DETECTED !", value=error_text, visible=True, elem_id="error"
)
return error_box
#### VALIDATION FUNCTIONS
def validate_circumstance(data):
circumstance_raw = get_fields(data, "circumstance")
circumstance_formatted = process_circumstance(circumstance_raw)
try:
Circumstances.model_validate(circumstance_formatted)
circumstances = Circumstances(**circumstance_formatted)
error = None
except ValidationError as e:
error = e
circumstances = None
return circumstances, error
def validate_behavior(data, mode):
behaviors_raw = get_fields(data, "behaviors")
behaviors_formatted = process_behaviors(behaviors_raw)
try:
if mode == "simple":
BehaviorsSimple.model_validate(behaviors_formatted)
behavior = BehaviorsSimple(**behaviors_formatted)
elif mode == "advanced":
Behaviors.model_validate(behaviors_formatted)
behavior = Behaviors(**behaviors_formatted)
error = None
except ValidationError as e:
behavior = None
error = e
return behavior, error
def validate_physical(data, mode):
physical_raw = get_fields(data, "physical")
physical_formatted = process_physical(physical_raw)
try:
if mode == "simple":
PhysicalAnomaliesSimple.model_validate(physical_formatted)
physical = PhysicalAnomaliesSimple(**physical_formatted)
elif mode == "advanced":
PhysicalAnomalies.model_validate(physical_formatted)
physical = PhysicalAnomalies(**physical_formatted)
error = None
except ValidationError as e:
physical = None
error = e
return physical, error
def validate_follow_up(data):
followup_raw = get_fields(data, "followup")
followup_formatted = process_followup(followup_raw)
try:
FollowUpEvents.model_validate(followup_formatted)
followup = FollowUpEvents(**followup_formatted)
error = None
except ValidationError as e:
followup = None
return followup, error
|