Spaces:
Runtime error
Runtime error
File size: 5,435 Bytes
a5df04e 6c880c2 a5df04e a9f27ef 6c880c2 a5df04e 1671151 6c880c2 1671151 a5df04e 1671151 a5df04e 6c880c2 |
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 |
import gradio as gr
from transformers import pipeline
from PIL import Image
import os
from huggingface_hub import HfApi, upload_file
import io
import numpy as np
import uuid
# Initialize the pipeline with your model
pipe = pipeline("image-classification", model="SubterraAI/ofwat_material_classification")
HF_TOKEN = os.getenv('HF_TOKEN')
DATASET_NAME = "SubterraAI/ofwat_material_loop"
hf_api = HfApi()
# Directory where the flagged images will be saved
flagged_data_dir = "./flagged_data"
material_codes = {
"AC": "Asphalt Concrete",
"BL": "Block",
"BR": "Brick",
"CI": "Cast Iron",
"CO": "Concrete",
"CS": "Corrugated Steel",
"DI": "Ductile Iron",
"EP": "Epoxy",
"GI": "Galvanized Iron",
"MAR": "Masonry",
"N": "Not specified, possibly a custom abbreviation",
"OTH": "Other",
"PE": "Polyethylene",
"PF": "Plywood-Faced",
"PP": "Polypropylene",
"PVC": "Polyvinyl Chloride",
"RC": "Reinforced Concrete",
"ST": "Steel",
"U": "Unspecified, possibly a custom abbreviation",
"UPVC": "Unplasticized Polyvinyl Chloride",
"VC": "Vinyl Coated",
"XI": "Extra Impact",
"XP": "Extruded Polystyrene",
"Z": "Not specified, possibly a custom abbreviation"
}
material_full_names_list = [
'Asphalt Concrete',
'Block',
'Brick',
'Cast Iron',
'Concrete',
'Corrugated Steel',
'Ductile Iron',
'Epoxy',
'Galvanized Iron',
'Masonry',
'Not specified, possibly a custom abbreviation',
'Other',
'Polyethylene',
'Plywood-Faced',
'Polypropylene',
'Polyvinyl Chloride',
'Reinforced Concrete',
'Steel',
'Unspecified, possibly a custom abbreviation',
'Unplasticized Polyvinyl Chloride',
'Vinyl Coated',
'Extra Impact',
'Extruded Polystyrene',
'Not specified, possibly a custom abbreviation',
'Vitrified Clay Lined'
]
def simple_flag(image, label):
# Convert the input image to PIL format and save to a BytesIO object
pil_image = Image.fromarray(image.astype(np.uint8))
img_byte_arr = io.BytesIO()
pil_image.save(img_byte_arr, format='PNG')
# Generate a unique ID for the image
unique_id = str(uuid.uuid4())
img_filename = f"{unique_id}.png"
# Save the image to a BytesIO object
image_bytes = img_byte_arr.getvalue()
# Upload the image to the correct label directory in the Hugging Face dataset
label_dir = f"{label}/{img_filename}"
upload_file(
path_or_fileobj=io.BytesIO(image_bytes),
path_in_repo=label_dir,
repo_id=DATASET_NAME,
repo_type="dataset",
token=HF_TOKEN,
commit_message=f"Add image with label {label}"
)
return "Thank you for your contribution to the open-source world! Your feedback helps us all move towards a clearer future"
def replace_label_with_full_name(res, defect_dict_key_code):
new_res = {}
for dic in res:
# Splitting the label to handle possible suffix
parts = dic["label"].split('_', 1)
code = parts[0]
suffix = '_' + parts[1] if len(parts) > 1 else ''
# Replacing the code with its full name, if it exists in the dictionary
full_name = defect_dict_key_code.get(code, code)
# Constructing the new label with the suffix if it exists
new_label = full_name + suffix
new_res[new_label] = dic["score"]
return new_res
def classify_image(image):
# Convert the input image to PIL format
PIL_image = Image.fromarray(image).convert('RGB')
# Classify the image using the pipeline
res = pipe(PIL_image)
# Extract labels and scores
return replace_label_with_full_name(res, material_codes)
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Material Classification with AI by Subterra")
gr.Markdown("Upload an image to view a classification demonstration leveraging the dataset/library of images collected by WRc & United Utilities during The Water Services Regulation Authority (OFWAT) Innovation Challenge – Artificial Intelligence and Sewers. Not only can you see the initial classification, but you as the user can also inform us if the classification is correct. Your response will be used to retrain this model. The team at Subterra would like to thank all of those involved in collecting this dataset as we hope that other groups will use it to further advance technology solutions for the water industry.")
with gr.Row():
with gr.Column():
img_input = gr.Image()
submit_button = gr.Button("Classify")
examples = gr.Examples(["examples/CS.jpg", "examples/GI.jpg", "examples/PP.jpg", "examples/RC.jpg"], label = "Explore Examples", inputs=img_input)
with gr.Column():
output_label = gr.Label()
flagging_options = gr.Radio(material_full_names_list, label="Does this classification look off to you? Your sharp eyes can help correct it. Flag any inaccuracies and suggest the right label!")
flag_button = gr.Button("Flag")
flag_status = gr.Textbox(label = "Every flag you submit polishes our dataset. Thanks for being an active participant in our open-source journey.",visible=True)
submit_button.click(classify_image, inputs=img_input, outputs=output_label)
flag_button.click(simple_flag, inputs=[img_input, flagging_options], outputs=flag_status)
demo.launch() |