Jason Adrian commited on
Commit
4a3b432
·
1 Parent(s): 3e4de6f

Adding metadata + new class features

Browse files
Files changed (1) hide show
  1. app.py +26 -8
app.py CHANGED
@@ -1,13 +1,14 @@
1
  import gradio as gr
2
  import random
3
  import csv
 
4
 
5
  class_names = ['cat', 'dog']
6
 
7
  def update_dropdown(className):
8
  class_names.append(className)
9
  updated_choices = gr.Dropdown(choices=class_names)
10
- return updated_choices
11
 
12
  def show_picked_class(className):
13
  return className
@@ -27,7 +28,7 @@ def image_classifier(inp):
27
 
28
  labeled_result = {name:score for name, score in zip(class_names, normalized_percentages)}
29
 
30
- return labeled_result, gr.Dropdown(choices=class_names)
31
 
32
  demo = gr.Blocks()
33
 
@@ -50,10 +51,9 @@ with demo as app:
50
  b2 = gr.Button("Show me the picked class")
51
  picked_class = gr.Textbox()
52
 
53
- b1.click(update_dropdown, inputs=text_input, outputs=text_options)
54
  b2.click(show_picked_class, inputs=text_options, outputs=picked_class)
55
 
56
- process_btn.click(image_classifier, inputs=inp_img, outputs=[out_txt, text_options])
57
  clear_btn.click(lambda:(
58
  gr.update(value=None),
59
  gr.update(value=None)
@@ -83,6 +83,8 @@ with demo as app:
83
  images_label = gr.Dropdown(class_names, label="Class Label", multiselect=False)
84
  b3 = gr.Button("Save and change the label using dropdown")
85
 
 
 
86
  multiple_inputs.upload(show_to_gallery, inputs=multiple_inputs, outputs=[gallery, imgs])
87
 
88
  gallery.select(get_select_index, None, selected)
@@ -95,23 +97,39 @@ with demo as app:
95
 
96
  b3.click(change_labels, [imgs, selected, images_label], [imgs, gallery])
97
 
 
98
  b4 = gr.Button("Upload to metadata")
99
 
100
  def upload_metadata(imgs):
101
- with open('metadata.csv', mode='w', newline='') as csv_file:
 
 
 
102
  # Create a CSV writer
103
  csv_writer = csv.writer(csv_file)
104
 
105
  # Write the header row
106
- csv_writer.writerow(['File_Path', 'Label'])
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  # Write the data rows
109
  csv_writer.writerows(imgs)
110
 
111
  print(f"Metadata CSV file has been created.")
112
- return imgs
113
 
114
- b4.click(upload_metadata, imgs, imgs)
115
 
116
 
117
  demo.launch(debug=True)
 
1
  import gradio as gr
2
  import random
3
  import csv
4
+ import datetime
5
 
6
  class_names = ['cat', 'dog']
7
 
8
  def update_dropdown(className):
9
  class_names.append(className)
10
  updated_choices = gr.Dropdown(choices=class_names)
11
+ return updated_choices, updated_choices
12
 
13
  def show_picked_class(className):
14
  return className
 
28
 
29
  labeled_result = {name:score for name, score in zip(class_names, normalized_percentages)}
30
 
31
+ return labeled_result
32
 
33
  demo = gr.Blocks()
34
 
 
51
  b2 = gr.Button("Show me the picked class")
52
  picked_class = gr.Textbox()
53
 
 
54
  b2.click(show_picked_class, inputs=text_options, outputs=picked_class)
55
 
56
+ process_btn.click(image_classifier, inputs=inp_img, outputs=out_txt)
57
  clear_btn.click(lambda:(
58
  gr.update(value=None),
59
  gr.update(value=None)
 
83
  images_label = gr.Dropdown(class_names, label="Class Label", multiselect=False)
84
  b3 = gr.Button("Save and change the label using dropdown")
85
 
86
+ b1.click(update_dropdown, inputs=text_input, outputs=[text_options, images_label])
87
+
88
  multiple_inputs.upload(show_to_gallery, inputs=multiple_inputs, outputs=[gallery, imgs])
89
 
90
  gallery.select(get_select_index, None, selected)
 
97
 
98
  b3.click(change_labels, [imgs, selected, images_label], [imgs, gallery])
99
 
100
+ gr.Markdown('### Save Metadata Into .csv')
101
  b4 = gr.Button("Upload to metadata")
102
 
103
  def upload_metadata(imgs):
104
+ time_uploaded = datetime.datetime.now()
105
+ time_str = time_uploaded.strftime("%m-%d-%Y_%H-%M-%S")
106
+
107
+ with open(f'{time_str}.csv', mode='w', newline='') as csv_file:
108
  # Create a CSV writer
109
  csv_writer = csv.writer(csv_file)
110
 
111
  # Write the header row
112
+ csv_writer.writerow(['image_path', 'ground_truth', 'time_uploaded', 'prediction_label', 'prediction_conf'])
113
+
114
+ for image in imgs:
115
+ image.append(time_str)
116
+ model_output = image_classifier(image)
117
+ # Sort the label and confidence output in descending order
118
+ sorted_output = dict(sorted(model_output.items(), key=lambda item: item[1], reverse=True))
119
+
120
+ # Extract the label with the highest value
121
+ label_prediction = next(iter(sorted_output))
122
+ image.append(label_prediction)
123
+
124
+ label_confidence = model_output[label_prediction]
125
+ image.append(label_confidence)
126
 
127
  # Write the data rows
128
  csv_writer.writerows(imgs)
129
 
130
  print(f"Metadata CSV file has been created.")
 
131
 
132
+ b4.click(upload_metadata, inputs=imgs)
133
 
134
 
135
  demo.launch(debug=True)