Spaces:
Runtime error
Runtime error
File size: 1,709 Bytes
a20b345 |
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 |
import gradio as gr
from transformers import pipeline
from PIL import Image
import os
# Initialize the pipeline with your model
pipe = pipeline("image-classification", model="SubterraAI/ofwat_cleaner_classification")
HF_TOKEN = os.environ.get('HF_TOKEN')
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, dataset_name="ofwat_cleaner_loop", private=True, separate_dirs=True)
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}
def flag_feedback(image, option, flag_status):
# Perform flagging action here using hf_writer
hf_writer.flag((image, option))
# Update the flag status to indicate feedback has been submitted
flag_status.update("Feedback submitted. Thank you!")
return flag_status
# Create a state variable for the flag status
flag_status = gr.State("")
# Create the Gradio interface
iface = gr.Interface(
classify_image,
inputs=[gr.Image(), gr.Radio(["obstruction", "no_obstruction"])],
outputs=[gr.Label(), gr.Textbox(label="Flag Status", value=flag_status)],
examples=[
["examples/CS.jpg"],
["examples/GI.jpg"],
["examples/PP.jpg"]
],
description="Upload an image to view a classification demonstration...",
title="Sewer Obstruction Classification with AI by Subterra",
allow_flagging="manual",
flagging_options=["obstruction", "no_obstruction"],
flagging_callback=lambda image, option: flag_feedback(image, option, flag_status)
)
# Launch the interface
iface.launch() |