Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
with gr.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|