Spaces:
Runtime error
Runtime error
more sampling options
Browse files
app.py
CHANGED
|
@@ -19,20 +19,101 @@ def output_generate(image):
|
|
| 19 |
generated = model.generate(im, seq_len=20)
|
| 20 |
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
paths = sorted(pathlib.Path("images").glob("*.jpg"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
iface.launch()
|
|
|
|
| 19 |
generated = model.generate(im, seq_len=20)
|
| 20 |
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
|
| 21 |
|
| 22 |
+
def inference_caption(image, decoding_method="Beam search", rep_penalty=1.2, top_p=0.5, min_seq_len=5, seq_len=20):
|
| 23 |
+
im = transform(image).unsqueeze(0).to(device)
|
| 24 |
+
generation_type = "beam_search" if decoding_method == "Beam search" else "top_p"
|
| 25 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 26 |
+
generated = model.generate(
|
| 27 |
+
im,
|
| 28 |
+
generation_type=generation_type,
|
| 29 |
+
top_p=top_p,
|
| 30 |
+
min_seq_len=min_seq_len,
|
| 31 |
+
seq_len=seq_len,
|
| 32 |
+
repetition_penalty=rep_penalty
|
| 33 |
+
)
|
| 34 |
+
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
|
| 35 |
|
| 36 |
paths = sorted(pathlib.Path("images").glob("*.jpg"))
|
| 37 |
+
with gr.Blocks(
|
| 38 |
+
css="""
|
| 39 |
+
.message.svelte-w6rprc.svelte-w6rprc.svelte-w6rprc {font-size: 20px; margin-top: 20px}
|
| 40 |
+
#component-21 > div.wrap.svelte-w6rprc {height: 600px;}
|
| 41 |
+
"""
|
| 42 |
+
) as iface:
|
| 43 |
+
state = gr.State([])
|
| 44 |
|
| 45 |
+
# gr.Markdown(title)
|
| 46 |
+
# gr.Markdown(description)
|
| 47 |
+
# gr.Markdown(article)
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column(scale=1):
|
| 51 |
+
image_input = gr.Image(type="pil")
|
| 52 |
+
|
| 53 |
+
# with gr.Row():
|
| 54 |
+
sampling = gr.Radio(
|
| 55 |
+
choices=["Beam search", "Nucleus sampling"],
|
| 56 |
+
value="Beam search",
|
| 57 |
+
label="Text Decoding Method",
|
| 58 |
+
interactive=True,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
rep_penalty = gr.Slider(
|
| 62 |
+
minimum=1.0,
|
| 63 |
+
maximum=5.0,
|
| 64 |
+
value=1.5,
|
| 65 |
+
step=0.5,
|
| 66 |
+
interactive=True,
|
| 67 |
+
label="Repeat Penalty (larger value prevents repetition)",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
top_p = gr.Slider(
|
| 71 |
+
minimum=0.0,
|
| 72 |
+
maximum=1.0,
|
| 73 |
+
value=1.0,
|
| 74 |
+
step=0.1,
|
| 75 |
+
interactive=True,
|
| 76 |
+
label="Top p (used with nucleus sampling)",
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
min_seq_len = gr.Number(
|
| 80 |
+
value=5, label="Minimum Sequence Length", precision=0, interactive=True
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
seq_len = gr.Number(
|
| 84 |
+
value=20, label="Maximum Sequence Length", precision=0, interactive=True
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
with gr.Column(scale=1.8):
|
| 88 |
+
|
| 89 |
+
with gr.Column():
|
| 90 |
+
caption_output = gr.Textbox(lines=1, label="Caption Output")
|
| 91 |
+
caption_button = gr.Button(
|
| 92 |
+
value="Caption it!", interactive=True, variant="primary"
|
| 93 |
+
)
|
| 94 |
+
caption_button.click(
|
| 95 |
+
inference_caption,
|
| 96 |
+
[
|
| 97 |
+
image_input,
|
| 98 |
+
sampling,
|
| 99 |
+
rep_penalty,
|
| 100 |
+
top_p,
|
| 101 |
+
min_seq_len,
|
| 102 |
+
seq_len
|
| 103 |
+
],
|
| 104 |
+
[caption_output],
|
| 105 |
+
)
|
| 106 |
+
# iface = gr.Interface(
|
| 107 |
+
# fn=output_generate,
|
| 108 |
+
# inputs=gr.Image(label="Input image", type="pil"),
|
| 109 |
+
# outputs=gr.Text(label="Caption output"),
|
| 110 |
+
# title="CoCa: Contrastive Captioners",
|
| 111 |
+
# description=(
|
| 112 |
+
# """<br> An open source implementation of <strong>CoCa: Contrastive Captioners are Image-Text Foundation Models</strong> <a href=https://arxiv.org/abs/2205.01917>https://arxiv.org/abs/2205.01917.</a>
|
| 113 |
+
# <br> Built using <a href=https://github.com/mlfoundations/open_clip>open_clip</a> with an effort from <a href=https://laion.ai/>LAION</a>.
|
| 114 |
+
# <br> For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.<a href="https://huggingface.co/spaces/laion/CoCa?duplicate=true"> <img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>"""
|
| 115 |
+
# ),
|
| 116 |
+
# article="""""",
|
| 117 |
+
# examples=[path.as_posix() for path in paths],
|
| 118 |
+
# )
|
| 119 |
iface.launch()
|