CoolFace
Apppublic

ConceptaMAGIC/operations-CLIP-Score

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py35 linesDownload Raw Back to root
1import gradio as gr2from transformers import CLIPProcessor, CLIPModel3 4model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")5processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")6 7 8def calculate_score(image, text):9    labels = text.split(";")10    labels = [l.strip() for l in labels]11    labels = list(filter(None, labels))12    if len(labels) == 0:13        return dict()14    inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)15    outputs = model(**inputs)16    logits_per_image = outputs.logits_per_image.detach().numpy()17 18    results_dict = {19        label: score / 100.0 for label, score in zip(labels, logits_per_image[0])20    }21    return results_dict22 23 24if __name__ == "__main__":25 26    demo = gr.Interface(27        fn=calculate_score,28        inputs=["image", "text"],29        outputs="label",30        allow_flagging="never",31        description="# CLIP Score",32    )33 34    demo.launch()35