Daniton commited on
Commit
786144e
·
1 Parent(s): ca6d8b4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pathlib
2
+
3
+ import gradio as gr
4
+ import open_clip
5
+ import torch
6
+
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+ model, _, transform = open_clip.create_model_and_transforms(
10
+ "coca_ViT-L-14",
11
+ pretrained="mscoco_finetuned_laion2B-s13B-b90k"
12
+ )
13
+ model.to(device)
14
+
15
+ title="""<h1 align="center">CoCa: Contrastive Captioners</h1>"""
16
+ description=(
17
+ """<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>
18
+ <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>.
19
+ <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>"""
20
+ )
21
+
22
+ def output_generate(image):
23
+ im = transform(image).unsqueeze(0).to(device)
24
+ with torch.no_grad(), torch.cuda.amp.autocast():
25
+ generated = model.generate(im, seq_len=20)
26
+ return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
27
+
28
+ def inference_caption(image, decoding_method="Beam search", rep_penalty=1.2, top_p=0.5, min_seq_len=5, seq_len=20):
29
+ im = transform(image).unsqueeze(0).to(device)
30
+ generation_type = "beam_search" if decoding_method == "Beam search" else "top_p"
31
+ with torch.no_grad(), torch.cuda.amp.autocast():
32
+ generated = model.generate(
33
+ im,
34
+ generation_type=generation_type,
35
+ top_p=float(top_p),
36
+ min_seq_len=min_seq_len,
37
+ seq_len=seq_len,
38
+ repetition_penalty=float(rep_penalty)
39
+ )
40
+ return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
41
+
42
+ paths = sorted(pathlib.Path("images").glob("*.jpg"))
43
+ with gr.Blocks() as iface:
44
+ state = gr.State([])
45
+
46
+ gr.Markdown(title)
47
+ gr.Markdown(description)
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.0,
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=0.5,
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 (has to higher than Minimum)", precision=0, interactive=True
85
+ )
86
+
87
+ with gr.Column(scale=1):
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
+
107
+ examples = gr.Examples(
108
+ examples=[path.as_posix() for path in paths],
109
+ inputs=[image_input],
110
+ )
111
+ iface.launch()