Commit
·
0974218
1
Parent(s):
c85c7f3
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import open_clip
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
model, _, transform = open_clip.create_model_and_transform(
|
| 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.convert("RGB")
|
| 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 |
+
inp = gr.Image(label="Image to Caption", type="pil")
|
| 26 |
+
out = gr.Textbox(label="Caption")
|
| 27 |
+
btn = gr.Button("Generate caption")
|
| 28 |
+
btn.click(fn=generate_caption,inputs=inp, outputs=out)
|
| 29 |
+
|
| 30 |
+
demo.launch()
|