Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import clip
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
model, preprocess = clip.load("ViT-B/32", device=device)
|
| 8 |
+
|
| 9 |
+
def allure(image, gender):
|
| 10 |
+
image = Image.fromarray(image.astype("uint8"), "RGB")
|
| 11 |
+
|
| 12 |
+
image = preprocess(image).unsqueeze(0).to(device)
|
| 13 |
+
positive_terms = [f'a hot {gender}', f'a beautiful {gender}', f'an alluring {gender}']
|
| 14 |
+
negative_terms = [f'a gross {gender}', f'an ugly {gender}', f'a hideous {gender}']
|
| 15 |
+
|
| 16 |
+
pairs = list(zip(positive_terms, negative_terms))
|
| 17 |
+
|
| 18 |
+
def evaluate(terms):
|
| 19 |
+
text = clip.tokenize(terms).to(device)
|
| 20 |
+
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
logits_per_image, logits_per_text = model(image, text)
|
| 23 |
+
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
|
| 24 |
+
return probs[0]
|
| 25 |
+
|
| 26 |
+
probs = [evaluate(pair) for pair in pairs]
|
| 27 |
+
|
| 28 |
+
positive_probs = [prob[0] for prob in probs]
|
| 29 |
+
negative_probs = [prob[1] for prob in probs]
|
| 30 |
+
|
| 31 |
+
hotness_score = round((probs[0][0] - probs[0][1] + 1) * 50, 2)
|
| 32 |
+
beauty_score = round((probs[1][0] - probs[1][1] + 1) * 50, 2)
|
| 33 |
+
attractiveness_score = round((probs[2][0] - probs[2][1] + 1) * 50, 2)
|
| 34 |
+
|
| 35 |
+
hot_score = sum(positive_probs)/len(positive_probs)
|
| 36 |
+
ugly_score = sum(negative_probs)/len(negative_probs)
|
| 37 |
+
composite = ((hot_score - ugly_score)+1) * 50
|
| 38 |
+
composite = round(composite, 2)
|
| 39 |
+
return composite, hotness_score, beauty_score, attractiveness_score
|
| 40 |
+
|
| 41 |
+
iface = gr.Interface(
|
| 42 |
+
fn=allure,
|
| 43 |
+
inputs=[
|
| 44 |
+
gr.inputs.Image(label="Image"),
|
| 45 |
+
gr.inputs.Dropdown(
|
| 46 |
+
[
|
| 47 |
+
'person', 'man', 'woman'
|
| 48 |
+
],
|
| 49 |
+
default='person',
|
| 50 |
+
)
|
| 51 |
+
],
|
| 52 |
+
outputs=[
|
| 53 |
+
gr.Textbox(label="Summary"),
|
| 54 |
+
gr.Textbox(label="Hotness"),
|
| 55 |
+
gr.Textbox(label="Beauty"),
|
| 56 |
+
gr.Textbox(label="Allure"),
|
| 57 |
+
],
|
| 58 |
+
title="Allure Evaluator",
|
| 59 |
+
description="A simple attractiveness evaluation app using OpenAI's CLIP model. How it works: the input image is passed to OpenAI's CLIP image captioning model and evaluated for how much it conforms to the model's idea of hotness, beauty, and attractiveness. These values are then combined to produce a composite score on a scale of 0 to 100.",
|
| 60 |
+
)
|
| 61 |
+
iface.launch()
|