Spaces:
Runtime error
Runtime error
Commit
·
688b141
1
Parent(s):
53624ac
Update app.py
Browse files
app.py
CHANGED
@@ -7,24 +7,35 @@ model, _, transform = open_clip.create_model_and_transforms(
|
|
7 |
model_name="coca_ViT-L-14",
|
8 |
pretrained="laion2B-s13B-b90k"
|
9 |
)
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# load an image
|
12 |
def generate_caption(image):
|
13 |
-
im = image
|
14 |
# transform the image and add a batch size dimension
|
15 |
im = transform(im).unsqueeze(0)
|
16 |
|
17 |
generated = model.generate(im)
|
18 |
generated = generated.detach()
|
19 |
|
20 |
-
return(open_clip.decode(generated[0]))
|
21 |
|
22 |
with gr.Blocks() as demo:
|
23 |
gr.Markdown("## Captioning with OpenCLIP CoCa")
|
24 |
with gr.Row():
|
25 |
-
|
|
|
26 |
out = gr.Textbox(label="Caption")
|
27 |
btn = gr.Button("Generate caption")
|
28 |
-
btn.click(fn=generate_caption,inputs=
|
29 |
|
30 |
demo.launch()
|
|
|
7 |
model_name="coca_ViT-L-14",
|
8 |
pretrained="laion2B-s13B-b90k"
|
9 |
)
|
10 |
+
def resize_background(img):
|
11 |
+
width, height = img.size
|
12 |
+
aspect_ratio = width / height
|
13 |
+
|
14 |
+
if aspect_ratio != 1:
|
15 |
+
new_img = Image.new("RGB", (512, 512), color=(255, 255, 255))
|
16 |
+
new_img.paste(img, (int((512 - width) / 2), int((512 - height) / 2)))
|
17 |
+
img = new_img
|
18 |
+
|
19 |
+
img = img.resize((512, 512), Image.LANCZOS)
|
20 |
+
return img
|
21 |
# load an image
|
22 |
def generate_caption(image):
|
23 |
+
im = resize_background(image)
|
24 |
# transform the image and add a batch size dimension
|
25 |
im = transform(im).unsqueeze(0)
|
26 |
|
27 |
generated = model.generate(im)
|
28 |
generated = generated.detach()
|
29 |
|
30 |
+
return(open_clip.decode(generated[0]).split("<start_of_text>")[1].split("<end_of_text>")[0])
|
31 |
|
32 |
with gr.Blocks() as demo:
|
33 |
gr.Markdown("## Captioning with OpenCLIP CoCa")
|
34 |
with gr.Row():
|
35 |
+
with gr.Column():
|
36 |
+
image = gr.Image(label="Image to Caption", type="pil")
|
37 |
out = gr.Textbox(label="Caption")
|
38 |
btn = gr.Button("Generate caption")
|
39 |
+
btn.click(fn=generate_caption,inputs=image, outputs=out)
|
40 |
|
41 |
demo.launch()
|