Spaces:
Runtime error
Runtime error
File size: 3,520 Bytes
46535f9 e4993da 46535f9 3e2fb01 46535f9 16e9c84 |
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 |
import os
from PIL import Image
import gradio as gr
from glob import glob
import torch
from transformers import AutoModel, AutoProcessor
DEFAULT_EXAMPLE_PATH = f'examples/example_0'
device = "cuda" if torch.cuda.is_available() else "cpu"
weight_dtype = torch.bfloat16 if device == "cuda" else torch.float32
print(f"Using device: {device} ({weight_dtype})")
print("Loading model...")
model_pretrained_name_or_path = "yuvalkirstain/PickScore_v1"
processor = AutoProcessor.from_pretrained(model_pretrained_name_or_path)
model = AutoModel.from_pretrained(model_pretrained_name_or_path, torch_dtype=weight_dtype).eval().to(device)
print("Model loaded.")
def calc_probs(prompt, images):
print("Processing inputs...")
image_inputs = processor(
images=images,
padding=True,
truncation=True,
max_length=77,
return_tensors="pt",
).to(device)
image_inputs = {k: v.to(weight_dtype) for k, v in image_inputs.items()}
text_inputs = processor(
text=prompt,
padding=True,
truncation=True,
max_length=77,
return_tensors="pt",
).to(device)
with torch.no_grad():
print("Embedding images and text...")
image_embs = model.get_image_features(**image_inputs)
image_embs = image_embs / torch.norm(image_embs, dim=-1, keepdim=True)
text_embs = model.get_text_features(**text_inputs)
text_embs = text_embs / torch.norm(text_embs, dim=-1, keepdim=True)
print("Calculating scores...")
scores = model.logit_scale.exp() * (text_embs.float() @ image_embs.float().T)[0]
print("Calculating probabilities...")
probs = torch.softmax(scores, dim=-1)
return probs.cpu().tolist()
def predict(prompt, image_1, image_2):
print(f"Starting prediction for prompt: {prompt}")
probs = calc_probs(prompt, [image_1, image_2])
print(f"Prediction: {probs}")
return str(round(probs[0], 3)), str(round(probs[1], 3))
with gr.Blocks(title="PickScore v1") as demo:
gr.Markdown("# PickScore v1")
gr.Markdown(
"This is a demo for the PickScore model - see [paper](https://arxiv.org/abs/2305.01569), [code](https://github.com/yuvalkirstain/PickScore), [dataset](https://huggingface.co/datasets/pickapic-anonymous/pickapic_v1), and [model](https://huggingface.co/yuvalkirstain/PickScore_v1).")
gr.Markdown("## Instructions")
gr.Markdown("Write a prompt, place two images, and press run to get their PickScore!")
with gr.Row():
prompt = gr.inputs.Textbox(lines=1, label="Prompt",
default=open(f'{DEFAULT_EXAMPLE_PATH}/prompt.txt').readline())
with gr.Row():
image_1 = gr.components.Image(type="pil", label="image 1",
value=Image.open(f'{DEFAULT_EXAMPLE_PATH}/image_1.png'))
image_2 = gr.components.Image(type="pil", label="image 2",
value=Image.open(f'{DEFAULT_EXAMPLE_PATH}/image_2.png'))
with gr.Row():
pred_1 = gr.outputs.Textbox(label="Probability 1")
pred_2 = gr.outputs.Textbox(label="Probability 2")
btn = gr.Button("Run")
btn.click(fn=predict, inputs=[prompt, image_1, image_2], outputs=[pred_1, pred_2])
gr.Examples(
[[open(f'{path}/prompt.txt').readline(), f'{path}/image_1.png', f'{path}/image_2.png'] for path in
glob(f'examples/*')],
[prompt, image_1, image_2],
[pred_1, pred_2],
predict
)
demo.launch()
|