Spaces:
Sleeping
Sleeping
File size: 8,313 Bytes
dea4744 7a12ec7 b19d492 dea4744 e9e80b2 dea4744 7a12ec7 dea4744 12eb8ec dea4744 1ab8bf1 dea4744 297b140 7731867 dea4744 42397fb 12eb8ec dea4744 |
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 244 245 246 247 248 249 250 251 |
"""PaliGemma demo gradio app."""
import datetime
import functools
import glob
import json
import logging
import os
import time
import gradio as gr
import PIL.Image
import gradio_helpers
import models
import paligemma_parse
INTRO_TEXT = """🤲 PaliGemma GGUF demo\n\n
| [Paper](https://arxiv.org/abs/2407.07726)
| [GitHub](https://github.com/google-research/big_vision/blob/main/big_vision/configs/proj/paligemma/README.md)
| [HF blog post](https://huggingface.co/blog/paligemma)
| [Google blog post](https://developers.googleblog.com/en/gemma-family-and-toolkit-expansion-io-2024)
| [Vertex AI Model Garden](https://console.cloud.google.com/vertex-ai/publishers/google/model-garden/363)
| [Demo](https://huggingface.co/spaces/google/paligemma)
|\n\n
[PaliGemma](https://ai.google.dev/gemma/docs/paligemma) is an open vision-language model by Google,
inspired by [PaLI-3](https://arxiv.org/abs/2310.09199) and
built with open components such as the [SigLIP](https://arxiv.org/abs/2303.15343)
vision model and the [Gemma](https://arxiv.org/abs/2403.08295) language model. PaliGemma is designed as a versatile
model for transfer to a wide range of vision-language tasks such as image and short video caption, visual question
answering, text reading, object detection and object segmentation.
\n\n
This space includes models fine-tuned on a mix of downstream tasks.
See the [blog post](https://huggingface.co/blog/paligemma) and
[README](https://github.com/google-research/big_vision/blob/main/big_vision/configs/proj/paligemma/README.md)
for detailed information how to use and fine-tune PaliGemma models.
\n\n
**This is an experimental research model.** Make sure to add appropriate guardrails when using the model for applications.
"""
make_image = lambda value, visible: gr.Image(
value, label='Image', type='filepath', visible=visible)
make_annotated_image = functools.partial(gr.AnnotatedImage, label='Image')
make_highlighted_text = functools.partial(gr.HighlightedText, label='Output')
# https://coolors.co/4285f4-db4437-f4b400-0f9d58-e48ef1
COLORS = ['#4285f4', '#db4437', '#f4b400', '#0f9d58', '#e48ef1']
@gradio_helpers.synced
def compute(image, prompt, model_name, sampler):
"""Runs model inference."""
if image is None:
raise gr.Error('Image required')
logging.info('prompt="%s"', prompt)
if isinstance(image, str):
image = PIL.Image.open(image)
if gradio_helpers.should_mock():
logging.warning('Mocking response')
time.sleep(2.)
output = paligemma_parse.EXAMPLE_STRING
else:
if not model_name:
raise gr.Error('Models not loaded yet')
output = models.generate(model_name, sampler, image, prompt)
# output = 'output'
logging.info('output="%s"', output)
width, height = image.size
objs = paligemma_parse.extract_objs(output, width, height, unique_labels=True)
labels = set(obj.get('name') for obj in objs if obj.get('name'))
color_map = {l: COLORS[i % len(COLORS)] for i, l in enumerate(labels)}
highlighted_text = [(obj['content'], obj.get('name')) for obj in objs]
annotated_image = (
image,
[
(
obj['mask'] if obj.get('mask') is not None else obj['xyxy'],
obj['name'] or '',
)
for obj in objs
if 'mask' in obj or 'xyxy' in obj
],
)
has_annotations = bool(annotated_image[1])
return (
make_highlighted_text(
highlighted_text, visible=True, color_map=color_map),
make_image(image, visible=not has_annotations),
make_annotated_image(
annotated_image, visible=has_annotations, width=width, height=height,
color_map=color_map),
)
def warmup(model_name):
image = PIL.Image.new('RGB', [1, 1])
_ = compute(image, '', model_name, 'greedy')
def reset():
return (
'', make_highlighted_text('', visible=False),
make_image(None, visible=True), make_annotated_image(None, visible=False),
)
def create_app():
"""Creates demo UI."""
make_model = lambda choices: gr.Dropdown(
value=(choices + [''])[0],
choices=choices,
label='Model',
visible=bool(choices),
)
make_prompt = lambda value, visible=True: gr.Textbox(
value, label='Prompt', visible=visible)
with gr.Blocks() as demo:
##### Main UI structure.
gr.Markdown(INTRO_TEXT)
with gr.Row():
image = make_image(None, visible=True) # input
annotated_image = make_annotated_image(None, visible=False) # output
with gr.Column():
with gr.Row():
prompt = make_prompt('', visible=True)
model_info = gr.Markdown(label='Model Info')
with gr.Row():
model = make_model([])
samplers = [
'greedy', 'nucleus(0.1)', 'nucleus(0.3)', 'temperature(0.5)']
sampler = gr.Dropdown(
value=samplers[0], choices=samplers, label='Decoding'
)
with gr.Row():
run = gr.Button('Run', variant='primary')
clear = gr.Button('Clear')
highlighted_text = make_highlighted_text('', visible=False)
##### UI logic.
def update_ui(model, prompt):
prompt = make_prompt(prompt, visible=True)
model_info = f'Model `{model}` – {models.MODELS_INFO.get(model, "No info.")}'
return [prompt, model_info]
gr.on(
[model.change],
update_ui,
[model, prompt],
[prompt, model_info],
)
gr.on(
[run.click, prompt.submit],
compute,
[image, prompt, model, sampler],
[highlighted_text, image, annotated_image],
)
clear.click(
reset, None, [prompt, highlighted_text, image, annotated_image]
)
##### Examples.
gr.set_static_paths(['examples/'])
all_examples = [json.load(open(p)) for p in glob.glob('examples/*.json')]
logging.info('loaded %d examples', len(all_examples))
example_image = gr.Image(
label='Image', visible=False) # proxy, never visible
example_model = gr.Text(
label='Model', visible=False) # proxy, never visible
example_prompt = gr.Text(
label='Prompt', visible=False) # proxy, never visible
example_license = gr.Markdown(
label='Image License', visible=False) # placeholder, never visible
gr.Examples(
examples=[
[
f'examples/{ex["name"]}.jpg',
ex['prompt'],
ex['model'],
ex['license'],
]
for ex in all_examples
if ex['model'] in models.MODELS
],
inputs=[example_image, example_prompt, example_model, example_license],
)
##### Examples UI logic.
example_image.change(
lambda image_path: (
make_image(image_path, visible=True),
make_annotated_image(None, visible=False),
make_highlighted_text('', visible=False),
),
example_image,
[image, annotated_image, highlighted_text],
)
def example_model_changed(model):
if model not in gradio_helpers.get_paths():
raise gr.Error(f'Model "{model}" not loaded!')
return model
example_model.change(example_model_changed, example_model, model)
example_prompt.change(make_prompt, example_prompt, prompt)
##### Status.
status = gr.Markdown(f'Startup: {datetime.datetime.now()}')
# gpu_kind = gr.Markdown(f'GPU=?')
# demo.load(
# lambda: [
# gradio_helpers.get_status(),
# make_model(list(gradio_helpers.get_paths())),
# ],
# None,
# [status, model],
# )
# def get_gpu_kind():
# device = jax.devices()[0]
# if not gradio_helpers.should_mock() and device.platform != 'gpu':
# raise gr.Error('GPU not visible to JAX!')
# return f'GPU={device.device_kind}'
# demo.load(get_gpu_kind, None, gpu_kind)
return demo
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
for k, v in os.environ.items():
logging.info('environ["%s"] = %r', k, v)
gradio_helpers.set_warmup_function(warmup)
for name, (repo, filenames) in models.MODELS.items():
gradio_helpers.register_download(name, repo, filenames)
create_app().queue().launch()
|