Adonai Vera
Improve message
0904938
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_cleaner_classification")
HF_TOKEN = os.getenv('HF_TOKEN')
DATASET_NAME = "SubterraAI/ofwat_cleaner_loop"
hf_api = HfApi()
# Directory where the flagged images will be saved
flagged_data_dir = "./flagged_data"
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 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 {dic["label"]: dic["score"] for dic in res}
with gr.Blocks() as demo:
gr.Markdown("# Sewer Obstruction 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"], label = "Explore Examples", inputs=img_input)
with gr.Column():
output_label = gr.Label()
flagging_options = gr.Radio(["obstruction", "no_obstruction"], 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()