Adonai Vera commited on
Commit
bab0655
·
1 Parent(s): e2608da

Add user feedbacks and improve message

Browse files
Files changed (1) hide show
  1. app.py +57 -19
app.py CHANGED
@@ -1,10 +1,20 @@
1
- from turtle import title
2
  import gradio as gr
3
  from transformers import pipeline
4
  from PIL import Image
 
 
 
 
 
5
 
6
  # Initialize the pipeline with your model
7
  pipe = pipeline("image-classification", model="SubterraAI/ofwat_defects_classification")
 
 
 
 
 
 
8
 
9
  defect_full_names_list = [
10
  "Crack Longitudinal",
@@ -136,6 +146,32 @@ defect_dict = {
136
  "CU": "Camera Underwater"
137
  }
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  def replace_label_with_full_name(res, defect_dict_key_code):
140
  new_res = {}
141
  for dic in res:
@@ -163,22 +199,24 @@ def classify_image(image):
163
  # Extract labels and scores
164
  return replace_label_with_full_name(res, defect_dict)
165
 
 
166
  # Create the Gradio interface
167
- iface = gr.Interface(
168
- classify_image,
169
- "image",
170
- "label",
171
- examples=[
172
- ["examples/PP.jpg"],
173
- ["examples/CS.jpg"],
174
- ["examples/GI.jpg"],
175
- ["examples/RC.jpg"]
176
- ],
177
- description="Upload an image to classify its material.",
178
- title="Defects Classification with AI by Subterra",
179
- allow_flagging="manual",
180
- flagging_options=defect_full_names_list
181
- )
182
-
183
- # Launch the interface
184
- iface.launch()
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
4
+ import os
5
+ from huggingface_hub import HfApi, upload_file
6
+ import io
7
+ import numpy as np
8
+ import uuid
9
 
10
  # Initialize the pipeline with your model
11
  pipe = pipeline("image-classification", model="SubterraAI/ofwat_defects_classification")
12
+ HF_TOKEN = os.getenv('HF_TOKEN')
13
+ DATASET_NAME = "SubterraAI/ofwat_defects_loop"
14
+ hf_api = HfApi()
15
+
16
+ # Directory where the flagged images will be saved
17
+ flagged_data_dir = "./flagged_data"
18
 
19
  defect_full_names_list = [
20
  "Crack Longitudinal",
 
146
  "CU": "Camera Underwater"
147
  }
148
 
149
+ def simple_flag(image, label):
150
+ # Convert the input image to PIL format and save to a BytesIO object
151
+ pil_image = Image.fromarray(image.astype(np.uint8))
152
+ img_byte_arr = io.BytesIO()
153
+ pil_image.save(img_byte_arr, format='PNG')
154
+
155
+ # Generate a unique ID for the image
156
+ unique_id = str(uuid.uuid4())
157
+ img_filename = f"{unique_id}.png"
158
+
159
+ # Save the image to a BytesIO object
160
+ image_bytes = img_byte_arr.getvalue()
161
+
162
+ # Upload the image to the correct label directory in the Hugging Face dataset
163
+ label_dir = f"{label}/{img_filename}"
164
+ upload_file(
165
+ path_or_fileobj=io.BytesIO(image_bytes),
166
+ path_in_repo=label_dir,
167
+ repo_id=DATASET_NAME,
168
+ repo_type="dataset",
169
+ token=HF_TOKEN,
170
+ commit_message=f"Add image with label {label}"
171
+ )
172
+
173
+ return "Thank you for your contribution to the open-source world! Your feedback helps us all move towards a clearer future"
174
+
175
  def replace_label_with_full_name(res, defect_dict_key_code):
176
  new_res = {}
177
  for dic in res:
 
199
  # Extract labels and scores
200
  return replace_label_with_full_name(res, defect_dict)
201
 
202
+
203
  # Create the Gradio interface
204
+ with gr.Blocks() as demo:
205
+ gr.Markdown("# Defects Classification with AI by Subterra")
206
+ 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.")
207
+
208
+ with gr.Row():
209
+ with gr.Column():
210
+ img_input = gr.Image()
211
+ submit_button = gr.Button("Classify")
212
+ examples = gr.Examples(["examples/CS.jpg", "examples/GI.jpg", "examples/PP.jpg", "examples/RC.jpg"], label = "Explore Examples", inputs=img_input)
213
+ with gr.Column():
214
+ output_label = gr.Label()
215
+ flagging_options = gr.Radio(defect_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!")
216
+ flag_button = gr.Button("Flag")
217
+ 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)
218
+
219
+ submit_button.click(classify_image, inputs=img_input, outputs=output_label)
220
+ flag_button.click(simple_flag, inputs=[img_input, flagging_options], outputs=flag_status)
221
+
222
+ demo.launch()