File size: 7,235 Bytes
8f68280 120a3c2 8f68280 120a3c2 8f68280 120a3c2 81cf2fa f7f5be8 8f68280 120a3c2 8f68280 30474d6 120a3c2 30474d6 120a3c2 81cf2fa 8f68280 120a3c2 8f68280 120a3c2 81cf2fa 8f68280 120a3c2 8f68280 f7f5be8 120a3c2 81cf2fa f7f5be8 8f68280 120a3c2 8f68280 120a3c2 f7f5be8 120a3c2 81cf2fa 8f68280 d44bf9b 120a3c2 f7f5be8 8f68280 f7f5be8 d44bf9b f7f5be8 8f68280 81cf2fa 8f68280 d44bf9b 8f68280 d44bf9b 8f68280 d44bf9b 8f68280 d44bf9b 8f68280 d44bf9b 8f68280 21e0a1c 8f68280 21e0a1c 81cf2fa d44bf9b 81cf2fa 8f68280 81cf2fa f7f5be8 d44bf9b 81cf2fa 8f68280 f7f5be8 8f68280 f7f5be8 81cf2fa 8f68280 f7f5be8 8f68280 81cf2fa 8f68280 81cf2fa 8f68280 f7f5be8 8f68280 92d528b 8f68280 21e0a1c f7f5be8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
from io import BytesIO
import string
import gradio as gr
import requests
from utils import Endpoint
def encode_image(image):
buffered = BytesIO()
image.save(buffered, format="JPEG")
buffered.seek(0)
return buffered
def query_chat_api(
image, prompt, decoding_method, temperature, len_penalty, repetition_penalty
):
url = endpoint.url
headers = {"User-Agent": "BLIP-2 HuggingFace Space"}
data = {
"prompt": prompt,
"use_nucleus_sampling": decoding_method == "Nucleus sampling",
"temperature": temperature,
"length_penalty": len_penalty,
"repetition_penalty": repetition_penalty,
}
image = encode_image(image)
files = {"image": image}
response = requests.post(url, data=data, files=files, headers=headers)
if response.status_code == 200:
return response.json()
else:
return "Error: " + response.text
def query_caption_api(
image, decoding_method, temperature, len_penalty, repetition_penalty
):
url = endpoint.url
# replace /generate with /caption
url = url.replace("/generate", "/caption")
headers = {"User-Agent": "BLIP-2 HuggingFace Space"}
data = {
"use_nucleus_sampling": decoding_method == "Nucleus sampling",
"temperature": temperature,
"length_penalty": len_penalty,
"repetition_penalty": repetition_penalty,
}
image = encode_image(image)
files = {"image": image}
response = requests.post(url, data=data, files=files, headers=headers)
if response.status_code == 200:
return response.json()
else:
return "Error: " + response.text
def postprocess_output(output):
# if last character is not a punctuation, add a full stop
if not output[0][-1] in string.punctuation:
output[0] += "."
return output
def inference_chat(
image,
text_input,
decoding_method,
temperature,
length_penalty,
repetition_penalty,
history=[],
):
text_input = text_input
history.append(text_input)
prompt = " ".join(history)
print(prompt)
output = query_chat_api(
image, prompt, decoding_method, temperature, length_penalty, repetition_penalty
)
output = postprocess_output(output)
history += output
chat = [
(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)
] # convert to tuples of list
return {chatbot: chat, state: history}
def inference_caption(
image,
decoding_method,
temperature,
length_penalty,
repetition_penalty,
):
output = query_caption_api(
image, decoding_method, temperature, length_penalty, repetition_penalty
)
return output[0]
title = """<h1 align="center">BLIP-2</h1>"""
description = """Gradio demo for BLIP-2, image-to-text generation from Salesforce Research. To use it, simply upload your image, or click one of the examples to load them. Please visit our <a href='https://github.com/salesforce/LAVIS/tree/main/projects/blip2' target='_blank'>project webpage</a>.</p>
<p> <strong>Disclaimer</strong>: This is a research prototype and is not intended for production use. No data including but not restricted to text and images is collected. </p>"""
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2201.12086' target='_blank'>BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models</a>"
endpoint = Endpoint()
examples = [
["house.png", "How could someone get out of the house?"],
["forbidden_city.webp", "In what dynasties was this place build?"],
# [
# "sunset.png",
# "Write a romantic message that goes along this photo.",
# ],
]
with gr.Blocks() as iface:
state = gr.State([])
gr.Markdown(title)
gr.Markdown(description)
gr.Markdown(article)
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil")
with gr.Row():
sampling = gr.Radio(
choices=["Beam search", "Nucleus sampling"],
value="Beam search",
label="Text Decoding Method",
interactive=True,
)
temperature = gr.Slider(
minimum=0.5,
maximum=1.0,
value=0.8,
step=0.1,
interactive=True,
label="Temperature (used with nucleus sampling)",
)
len_penalty = gr.Slider(
minimum=-1.0,
maximum=2.0,
value=1.0,
step=0.2,
interactive=True,
label="Length Penalty (set to larger for longer sequence, used with beam search)",
)
rep_penalty = gr.Slider(
minimum=1.0,
maximum=5.0,
value=1.5,
step=0.5,
interactive=True,
label="Repeat Penalty (larger value prevents repetition)",
)
with gr.Row():
caption_output = gr.Textbox(lines=2, label="Caption Output (from OPT)")
caption_button = gr.Button(
value="Caption it!", interactive=True, variant="primary"
)
caption_button.click(
inference_caption,
[
image_input,
sampling,
temperature,
len_penalty,
rep_penalty,
],
[caption_output],
)
with gr.Column():
chat_input = gr.Textbox(lines=2, label="Chat Input")
with gr.Row():
chatbot = gr.Chatbot(label="Chat Output (from FlanT5)")
image_input.change(lambda: (None, "", "", []), [], [chatbot, chat_input, caption_output, state])
with gr.Row():
clear_button = gr.Button(value="Clear", interactive=True)
clear_button.click(
lambda: ("", None, [], []),
[],
[chat_input, image_input, chatbot, state],
)
submit_button = gr.Button(
value="Submit", interactive=True, variant="primary"
)
submit_button.click(
inference_chat,
[
image_input,
chat_input,
sampling,
temperature,
len_penalty,
rep_penalty,
state,
],
[chatbot, state],
)
examples = gr.Examples(
examples=examples,
inputs=[image_input, chat_input],
# outputs=[chatbot, state],
# run_on_click=True,
# fn = inference_chat,
)
iface.queue(concurrency_count=1, api_open=False, max_size=10)
iface.launch(enable_queue=True)
|