Spaces:
Sleeping
Sleeping
import gradio as gr | |
import numpy as np | |
from huggingface_hub import from_pretrained_fastai | |
learn_imp = from_pretrained_fastai('MikeTrizna/Prunus_lineage_classifier') | |
def predict(image_np, description=None): | |
classes = ['Neotropical racemose', 'Paleotropical racemose', | |
'Solitary/Corymbose', 'Temperate racemose'] | |
pred,pred_idx,probs = learn_imp.predict(image_np) | |
confidences = {classes[idx]: f'{prob:.04f}' for idx, prob in enumerate(probs)} | |
return confidences | |
example_metadata = [{'idigbio_specimen': '36b28730-fd06-44b4-86a4-77d3be052ef1', | |
'idigbio_media': '1c086296-6d1f-4218-a18a-ca2f86c295d0', | |
'gbif_occurrence': '2425450644', | |
'gbif_media': '60d5b76a58cf28eff526d5c1d7906321', | |
'organism': 'Prunus myrtifolia', | |
'intrageneric_group': 'Neotropical racemose'}, | |
{'idigbio_specimen': '0252042a-e14f-4437-99f8-f9c690f1d2a8', | |
'idigbio_media': '104f44c9-63f1-4579-93c2-54c6ddeddeda', | |
'gbif_occurrence': '1056985949', | |
'gbif_media': '65d035ce17427cba5a698850e903cbc8', | |
'organism': 'Prunus oleifolia', | |
'intrageneric_group': 'Neotropical racemose'}, | |
{'idigbio_specimen': 'e025ff74-333b-461f-a86e-63d8f4a6bd90', | |
'idigbio_media': 'e3276fdd-ce58-40dc-bf20-8742f7634428', | |
'gbif_occurrence': '2515155769', | |
'gbif_media': 'd952c78c4b50825dc267d3ab915ea71e', | |
'organism': 'Prunus oocarpa', | |
'intrageneric_group': 'Paleotropical racemose'}, | |
{'idigbio_specimen': '4a991d4d-d6a5-4a4a-9c99-9af948f9e0e0', | |
'idigbio_media': 'e5ac59df-5b18-4c8d-a19b-02131d358855', | |
'gbif_occurrence': '2515160308', | |
'gbif_media': 'fc94119768b9ea1c453cb0ab9887b54a', | |
'organism': 'Prunus pullei', | |
'intrageneric_group': 'Paleotropical racemose'}, | |
{'idigbio_specimen': 'e32b4d05-5389-4466-83cb-25e319c2fa9f', | |
'idigbio_media': '0060a57d-e779-4984-913c-95b576daf0d3', | |
'gbif_occurrence': '3865356188', | |
'gbif_media': 'c40a3ebe7ea67f67d33b7405134f3133', | |
'organism': 'Prunus serotina', | |
'intrageneric_group': 'Temperate racemose'}, | |
{'idigbio_specimen': '31697ac1-223d-477b-bc8d-0d10ced90ad0', | |
'idigbio_media': 'b9fa9ff9-2e8b-42f5-8728-5b0b5be98490', | |
'gbif_occurrence': '2515504033', | |
'gbif_media': 'be7e108416c0a052311f879642997dee', | |
'organism': 'Prunus grayana', | |
'intrageneric_group': 'Temperate racemose'}, | |
{'idigbio_specimen': '80a8afd7-ca01-4df1-a566-305912e25166', | |
'idigbio_media': '52e06de9-c035-4afc-9fac-c8bee7628d38', | |
'organism': 'Prunus glandulosa', | |
'intrageneric_group': 'Solitary/Corymbose'}, | |
{'idigbio_specimen': '7835f8bd-d78c-4884-a669-853634296371', | |
'idigbio_media': '0b13e5eb-73da-4e0e-9b1a-794b737f7716', | |
'gbif_occurrence': '2265382670', | |
'gbif_media': '1373276d8fdd232579e7d04c4e64edb1', | |
'organism': 'Prunus pensylvanica', | |
'intrageneric_group': 'Solitary/Corymbose'}] | |
table_header = """**Example Guide** | |
| Image | Species | Link to iDigBio Specimen Record | Intrageneric group | | |
| --- | --- | --- | --- | | |
""" | |
table_body = '' | |
gbif_scale = 150 | |
for example in example_metadata: | |
if 'gbif_media' in example: | |
image_src = f"<image src = 'https://api.gbif.org/v1/image/cache/fit-in/{gbif_scale}x/occurrence/{example['gbif_occurrence']}/media/{example['gbif_media']}'></image>" | |
elif 'idigbio_media' in example: | |
image_src = f"<image src = 'https://api.idigbio.org/v2/media/{example['idigbio_media']}?size=thumbnail'></image>" | |
else: | |
image_src = '[No image source]' | |
species = f"*{example['organism']}*" | |
idigbio_link = f"https://www.idigbio.org/portal/records/{example['idigbio_specimen']}" | |
intrageneric_group = example['intrageneric_group'] | |
table_row = ' | '.join([image_src, species, | |
idigbio_link, intrageneric_group]) | |
table_body += table_row + '\n' | |
example_table = table_header + table_body | |
example_list = ["examples/myrtifolia_1c086296-6d1f-4218-a18a-ca2f86c295d0.jpg", | |
"examples/oleifolia_104f44c9-63f1-4579-93c2-54c6ddeddeda.jpg", | |
"examples/oocarpa_e3276fdd-ce58-40dc-bf20-8742f7634428.jpg", | |
"examples/pullei_e5ac59df-5b18-4c8d-a19b-02131d358855.jpg", | |
"examples/serotina_0060a57d-e779-4984-913c-95b576daf0d3.jpg", | |
"examples/grayana_b9fa9ff9-2e8b-42f5-8728-5b0b5be98490.jpg", | |
"examples/glandulosa_52e06de9-c035-4afc-9fac-c8bee7628d38.jpg", | |
"examples/pensylvanica_0b13e5eb-73da-4e0e-9b1a-794b737f7716.jpg"] | |
description = """This model can place herbarium sheet images from the genus Prunus into one of four | |
categories that represent key clades within the genus: Solitary/Corymbose, Temperate Racemose, Neotropical | |
Racemose, Paleotropical Racemose.""" | |
demo = gr.Interface(predict, | |
gr.Image(type="numpy"), | |
outputs="label", | |
examples=example_list, | |
description=description, | |
title="Image classification of four major lineages in the plum genus", | |
article=example_table) | |
demo.launch() |