ryaalbr commited on
Commit
0d38b2c
1 Parent(s): c297c95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -28
app.py CHANGED
@@ -28,35 +28,53 @@ def rand_image():
28
  def set_labels(text):
29
  return text.split(",")
30
 
 
 
 
 
31
  with gr.Blocks() as demo:
32
- labels = gr.State([]) # creates hidden component that can store a value and can be used as input/output; here, initial value is an empty list
33
- instructions = """## Instructions:
34
- 1. Enter list of labels separated by commas (or select one of the examples below)
35
- 2. Click **Get Random Image** to grab a random image from dataset and analyze it against the labels
36
- 3. Click **Re-Classify Image** to re-run classification on current image after changing labels"""
37
- gr.Markdown(instructions)
38
- with gr.Row(variant="compact"):
39
- label_text = gr.Textbox(show_label=False, placeholder="Enter classification labels").style(container=False)
40
- #submit_btn = gr.Button("Submit").style(full_width=False)
41
- gr.Examples(["spring, summer, fall, winter",
42
- "mountain, city, beach, ocean, desert, forest, valley",
43
- "red, blue, green, white, black, purple, brown",
44
- "person, animal, landscape, something else",
45
- "day, night, dawn, dusk"], inputs=label_text)
46
- with gr.Row():
47
- with gr.Column(variant="panel"):
48
- im = gr.Image(interactive=False).style(height=height)
49
- with gr.Row():
50
- get_btn = gr.Button("Get Random Image").style(full_width=False)
51
- reclass_btn = gr.Button("Re-Classify Image").style(full_width=False)
52
- cf = gr.Label()
53
- #submit_btn.click(fn=set_labels, inputs=label_text)
54
- label_text.change(fn=set_labels, inputs=label_text, outputs=labels) # parse list if changed
55
- label_text.blur(fn=set_labels, inputs=label_text, outputs=labels) # parse list if focus is moved elsewhere; ensures that list is fully parsed before classification
56
- label_text.submit(fn=set_labels, inputs=label_text, outputs=labels) # parse list if user hits enter; ensures that list is fully parsed before classification
57
- get_btn.click(fn=rand_image, outputs=im)
58
- im.change(predict, inputs=[im, labels], outputs=cf)
59
- reclass_btn.click(predict, inputs=[im, labels], outputs=cf)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  demo.queue()
62
  demo.launch()
 
28
  def set_labels(text):
29
  return text.split(",")
30
 
31
+ get_caption = gr.load("ryaalbr/caption", src="spaces")
32
+ def generate_text(image):
33
+ return get_caption(image)
34
+
35
  with gr.Blocks() as demo:
36
+
37
+ with gr.Tab("Zero-Shot Classification"):
38
+ labels = gr.State([]) # creates hidden component that can store a value and can be used as input/output; here, initial value is an empty list
39
+ instructions = """## Instructions:
40
+ 1. Enter list of labels separated by commas (or select one of the examples below)
41
+ 2. Click **Get Random Image** to grab a random image from dataset and analyze it against the labels
42
+ 3. Click **Re-Classify Image** to re-run classification on current image after changing labels"""
43
+ gr.Markdown(instructions)
44
+ with gr.Row(variant="compact"):
45
+ label_text = gr.Textbox(show_label=False, placeholder="Enter classification labels").style(container=False)
46
+ #submit_btn = gr.Button("Submit").style(full_width=False)
47
+ gr.Examples(["spring, summer, fall, winter",
48
+ "mountain, city, beach, ocean, desert, forest, valley",
49
+ "red, blue, green, white, black, purple, brown",
50
+ "person, animal, landscape, something else",
51
+ "day, night, dawn, dusk"], inputs=label_text)
52
+ with gr.Row():
53
+ with gr.Column(variant="panel"):
54
+ im = gr.Image(interactive=False).style(height=height)
55
+ with gr.Row():
56
+ get_btn = gr.Button("Get Random Image").style(full_width=False)
57
+ reclass_btn = gr.Button("Re-Classify Image").style(full_width=False)
58
+ cf = gr.Label()
59
+ #submit_btn.click(fn=set_labels, inputs=label_text)
60
+ label_text.change(fn=set_labels, inputs=label_text, outputs=labels) # parse list if changed
61
+ label_text.blur(fn=set_labels, inputs=label_text, outputs=labels) # parse list if focus is moved elsewhere; ensures that list is fully parsed before classification
62
+ label_text.submit(fn=set_labels, inputs=label_text, outputs=labels) # parse list if user hits enter; ensures that list is fully parsed before classification
63
+ get_btn.click(fn=rand_image, outputs=im)
64
+ im.change(predict, inputs=[im, labels], outputs=cf)
65
+ reclass_btn.click(predict, inputs=[im, labels], outputs=cf)
66
+
67
+ with gr.Tab("Image Captioning"):
68
+ with gr.Row():
69
+ with gr.Column(variant="panel"):
70
+ im_cap = gr.Image(interactive=False).style(height=height)
71
+ with gr.Row():
72
+ get_btn_cap = gr.Button("Get Random Image").style(full_width=False)
73
+ caption_btn = gr.Button("Create Caption").style(full_width=False)
74
+ caption = gr.Text()
75
+ get_btn_cap.click(fn=rand_image, outputs=im_cap)
76
+ #im_cap.change(generate_text, inputs=im_cap, outputs=caption)
77
+ caption_btn.click(generate_text, inputs=im_cap, outputs=caption)
78
 
79
  demo.queue()
80
  demo.launch()