feat: pictures as a gallery
Browse filesFiles changed (4)
1234568 unmodified lines151617181920{"headers": ["image","latitude","longitude","wounded",8 unmodified lines"physical_changes_body","physical_changes_head","physical_changes_feathers","physical_changes_legs"]}123458 unmodified lines14151617181920{"fields": ["latitude","longitude","wounded",8 unmodified lines"physical_changes_body","physical_changes_head","physical_changes_feathers","physical_changes_legs","image"]}
1234567897 unmodified lines17181920212223242526275 unmodified lines333435363738394041145 unmodified lines1871881891901911928 unmodified lines201202203204205206207208209210211212213214215216217import gradio as grfrom gradio_modal import Modalfrom utils_df import get_headersfrom utils_json import *from utils_df import save_individual_to_df, get_headers, save_and_rest_dffrom maps import get_locationfrom functools import partialfrom dead import show_section_dead7 unmodified linesfrom theme import theme, csswith gr.Blocks(theme=theme, css=css) as demo:with gr.Row():show_modal = gr.Button("Add an Animal", scale=3)submit_button = gr.Button("Submit All Animals", scale=1)df = gr.Dataframe(headers=get_headers(),visible=False)with Modal(visible=False) as modal:# ---------------------------------------------------------# Intro Text5 unmodified lines# ---------------------------------------------------------# Camerawith gr.Row():#with gr.Column(scale=1):camera = gr.Image(elem_id="image")# ---------------------------------------------------------# Location#with gr.Row():145 unmodified linesbutton_df = gr.Button("Submit Animal Report", scale = 3)button_clear = gr.ClearButton(scale = 1,components=[location, identified_location,button_collision_dead, button_deliberate_destruction_dead, button_indirect_destruction_dead, button_natural_cause_dead,dropdown_dead, dropdown_level2_dead, openfield_level2_dead, dropdown_extra_level2_dead,8 unmodified linesbutton_clear.click()button_clear.click(hide_physical,outputs=[checkbox_beak, text_beak, checkbox_body, text_body, checkbox_feathers, text_feathers, checkbox_head, text_head, checkbox_legs, text_legs])button_df.click(save_individual_to_df,inputs=[df],outputs=[df])button_df.click(lambda: Modal(visible=False), None, modal)# ---------------------------------------------------------# Event Functions of the landing page buttonsshow_modal.click(lambda: Modal(visible=True), None, modal)show_modal.click(create_json)submit_button.click(save_and_rest_df, inputs=[df], outputs=[df])123456787 unmodified lines161718192021222324252627282930315 unmodified lines3738394041424344454647145 unmodified lines1931941951961971981998 unmodified lines208209210211212213214215216217218219220221222223224225226227import gradio as grfrom gradio_modal import Modalfrom utils_json import *from utils_df import save_individual_to_galleryfrom maps import get_locationfrom functools import partialfrom dead import show_section_dead7 unmodified linesfrom theme import theme, csswith gr.Blocks(theme=theme, css=css) as demo:create_json_all_individuals()with gr.Row():show_modal = gr.Button("Add an Animal", scale=3)submit_button = gr.Button("Submit All Animals", scale=1)# df = gr.Dataframe(headers=get_headers(),# visible=False)gallery = gr.Gallery(label="Gallery of Records", elem_id="gallery",columns=[1], rows=[1],object_fit="contain", height="auto", interactive=False)with Modal(visible=False) as modal:# ---------------------------------------------------------# Intro Text5 unmodified lines# ---------------------------------------------------------# Camerawith gr.Row():def save_image(camera):add_data_to_individual("image", camera.tolist())camera = gr.Image(elem_id="image")camera.input(save_image, inputs=[camera])# ---------------------------------------------------------# Location#with gr.Row():145 unmodified linesbutton_df = gr.Button("Submit Animal Report", scale = 3)button_clear = gr.ClearButton(scale = 1,components=[camera,location, identified_location,button_collision_dead, button_deliberate_destruction_dead, button_indirect_destruction_dead, button_natural_cause_dead,dropdown_dead, dropdown_level2_dead, openfield_level2_dead, dropdown_extra_level2_dead,8 unmodified linesbutton_clear.click()button_clear.click(hide_physical,outputs=[checkbox_beak, text_beak, checkbox_body, text_body, checkbox_feathers, text_feathers, checkbox_head, text_head, checkbox_legs, text_legs])# button_df.click(save_individual_to_df,# inputs=[df],# outputs=[df])button_df.click(save_individual_to_gallery,inputs=[gallery],outputs=[gallery])button_df.click(lambda: Modal(visible=False), None, modal)# ---------------------------------------------------------# Event Functions of the landing page buttonsshow_modal.click(lambda: Modal(visible=True), None, modal)show_modal.click(create_json_one_individual)#submit_button.click(save_and_rest_df, inputs=[df], outputs=[df])#submit_button.click(save_individual_to_gallery)
123456781 unmodified line1011121314151617181920212223242526272829303132333435363738394041424344import gradio as grimport pandas as pdfrom dotenv import load_dotenvfrom utils_config import load_configfrom utils_json import get_json_one_individual, save_all_animalsimport osload_dotenv()PATH = os.getcwd() + "/"1 unmodified linePATH_CONFIG = PATH + PATH_ASSETS + "config/"def get_headers():headers_config = load_config(PATH_CONFIG + "config_df.json")headers = headers_config["headers"]return headersdef match_data_to_headers(headers, one_individual):new_row = {}for key in headers:if key in one_individual:if type(one_individual[key])==list:new_row[key] = ' , '.join(one_individual[key])else:new_row[key] = one_individual[key]else:new_row[key] = "NA"return list(new_row.values())def save_individual_to_df(df):headers = get_headers()one_individual = get_json_one_individual()new_row = match_data_to_headers(headers, one_individual)new_row_df = pd.DataFrame([new_row], columns=headers)df_new = pd.concat([df, new_row_df], ignore_index=True)df_gr = gr.DataFrame(visible=True,value=df_new,headers=headers)return df_grdef save_and_rest_df(df):save_all_animals(df)df = gr.Dataframe(headers=get_headers(),visible=False)return df1234567891 unmodified line1112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import gradio as grimport pandas as pdimport numpy as npfrom dotenv import load_dotenvfrom utils_config import load_configfrom utils_json import get_json_one_individual, save_to_all_individualsimport osload_dotenv()PATH = os.getcwd() + "/"1 unmodified linePATH_CONFIG = PATH + PATH_ASSETS + "config/"def get_headers():headers_config = load_config(PATH_CONFIG + "config_headers.json")headers = headers_config["headers"]return headersdef get_fields():fields_config = load_config(PATH_CONFIG + "config_fields.json")fields = fields_config["fields"]return fieldsdef match_data_to_fields(fields, one_individual):new_row = {}for key in fields:if key in one_individual:if key=="image":new_row[key] = one_individual[key]elif type(one_individual[key])==list:new_row[key] = ' , '.join(one_individual[key])else:new_row[key] = one_individual[key]else:new_row[key] = "NA"return new_rowdef process_animals(all_animals):processed_animals = []print(len(all_animals))for _, animal in all_animals.items():image = np.array(animal["image"])caption = []for key, val in animal.items():if key!="image":if key=="latitude":caption.extend([" | Latitude: " + str(animal["latitude"])])elif key=="longitude":caption.extend([" | Longitude: " + str(animal["longitude"])])elif key=="wounded" and val=="True":caption.extend([" | Wounded: " + animal["wounded"]])elif key=="dead" and val=="True":caption.extend([" | Dead: " + animal["dead"]])elif key=="circumstance":caption.extend([" | Circumstances: " ,animal["circumstance"],animal["circumstance_dropdown_level1"],animal["circumstance_dropdown_level2"],animal["circumstance_openfield_level2"],animal["circumstance_dropdown_extra_level2"]])elif key=="behavior":caption.extend([" | Behavior: ", animal[key]])elif "physical_changes" in key:if not(" | Physical Changes: " in caption) :caption.extend([" | Physical Changes: ","Beak: " + animal["physical_changes_beak"],"Body: " + animal["physical_changes_body"],"Head: " + animal["physical_changes_head"],"Feathers: " + animal["physical_changes_feathers"],"Legs: " + animal["physical_changes_legs"]])caption_str = " ".join(caption)animal = (image, caption_str)processed_animals.append(animal)return processed_animalsdef set_gallery_size(len_animals):if len_animals < 10:num_cols=5num_rows=2else:num_cols = len_animals/2num_rows = len_animals/(num_cols)return num_cols, num_rowsdef save_individual_to_gallery(gallery):one_individual = get_json_one_individual()fields = get_fields()one_individual_matched = match_data_to_fields(fields, one_individual)all_animals = save_to_all_individuals(one_individual_matched)num_cols, num_rows = set_gallery_size(len(all_animals))processed_animals = process_animals(all_animals)gallery = gr.Gallery(label="Gallery of Records", elem_id="gallery",columns=[num_cols], rows=[num_rows],value=processed_animals,object_fit="contain", height="auto", interactive=False)return gallery# def save_individual_to_df(df):# fields = get_fields()# one_individual = get_json_one_individual()# headers = get_headers()# new_row = match_data_to_fields(fields, one_individual)# new_row = format_row(new_row, headers)# new_row_df = pd.DataFrame([new_row], columns=headers)# df_new = pd.concat([df, new_row_df], ignore_index=True)# df_gr = gr.DataFrame(visible=True,# value=df_new,# headers=headers)# return df_gr# def save_and_rest_df(df):# save_all_animals(df)# df = gr.Dataframe(fields=get_fields(),# visible=False)# return df# def format_row(new_row, headers):# formatted_row = {}# #formatted_row["image"] = new_row["image"]# for header in headers:# if header=="location":# formatted_row[header] = " | ".join(["Latitude: " + str(new_row["latitude"]),# "Longitude: " + str(new_row["longitude"])])# elif header=="state":# formatted_row[header] = " | ".join(["Wounded: " + new_row["wounded"],# "Dead: " + new_row["dead"]])# elif header=="circumstance":# formatted_row[header] = " | ".join([new_row["circumstance"],# new_row["circumstance_dropdown_level1"],# new_row["circumstance_dropdown_level2"],# new_row["circumstance_openfield_level2"],# new_row["circumstance_dropdown_extra_level2"]])# elif header=="behavior":# formatted_row[header] = new_row[header]# elif header=="physical_changes":# formatted_row[header] = " | ".join([new_row["physical_changes_beak"],# new_row["physical_changes_body"],# new_row["physical_changes_head"],# new_row["physical_changes_feathers"],# new_row["physical_changes_legs"]])# return list(formatted_row.values())
1234567891011121314151617181920212223242526import jsonimport gradio as grdef create_json(one_individual={}):# Serializing jsonone_individual = json.dumps(one_individual)with open("data/one_individual.json", "w") as outfile:outfile.write(one_individual)def add_data_to_individual(key, value):with open("data/one_individual.json", 'r') as openfile:one_individual = json.load(openfile)one_individual[key] = valuecreate_json(one_individual)def get_json_one_individual():with open("data/one_individual.json", 'r') as openfile:one_individual = json.load(openfile)return one_individualdef save_all_animals(df):all_animals = df.to_json(orient="records")with open("data/all_animals.json", "w") as outfile:outfile.write(all_animals)123456789101112131415161718192021222324252627282930313233343536373839404142import jsonimport gradio as grdef create_json_one_individual(one_individual={}):# Serializing jsonone_individual = json.dumps(one_individual)with open("data/one_individual.json", "w") as outfile:outfile.write(one_individual)def create_json_all_individuals(all_individuals={}):all_individuals = json.dumps(all_individuals)with open("data/all_individuals.json", "w") as outfile:outfile.write(all_individuals)def add_data_to_individual(key, value):with open("data/one_individual.json", 'r') as openfile:one_individual = json.load(openfile)one_individual[key] = valuecreate_json_one_individual(one_individual)def get_json_one_individual():with open("data/one_individual.json", 'r') as openfile:one_individual = json.load(openfile)return one_individualdef get_json_all_individuals():with open("data/all_individuals.json", "r") as openfile:all_individuals = json.load(openfile)return all_individualsdef save_to_all_individuals(one_individual):all_individuals = get_json_all_individuals()all_individuals[str(len(all_individuals))] = one_individualall_individuals_for_json = json.dumps(all_individuals)with open("data/all_individuals.json", "w") as outfile:outfile.write(all_individuals_for_json)return all_individuals# def save_all_individuals(df):# all_individuals = df.to_json(orient="records")# with open("data/all_individuals.json", "w") as outfile:# outfile.write(all_individuals)
app/assets/config/config_df.json → app/assets/config/config_fields.jsonRenamed+3-3
app/main_multianimal.py+22-12
app/utils_df.py+120-21
app/utils_json.py+22-6