-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
50 lines (44 loc) · 1.77 KB
/
app.py
File metadata and controls
50 lines (44 loc) · 1.77 KB
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
# app.py (root)
import os, sys
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
import gradio as gr
from typing import Optional
from PIL import Image
from src.evaluate import (
embed_image, embed_text, alignment_score,
load_reference_embeddings, mean_reference_aesthetic_score
)
HQ_REFS = [
"data/example_images/photo-1661852818096-e74ff4f806bb.jpeg",
"data/example_images/photo-1605749439419-80c81f67eefc.jpeg",
"data/example_images/Mountain-Sunset.jpg"
]
HQ_EMBS = load_reference_embeddings(HQ_REFS) if HQ_REFS else []
DEFAULT_PROMPT = "A high-quality, realistic photograph of the described subject"
def evaluate(img: Image.Image, prompt: Optional[str]):
if img is None:
return "No image", "N/A"
img = img.convert("RGB")
prompt = (prompt or DEFAULT_PROMPT).strip()
ie = embed_image(img)
te = embed_text([prompt])
align = float(alignment_score(ie, te))
aest_out = "N/A"
if HQ_EMBS:
aest = mean_reference_aesthetic_score(ie, HQ_EMBS)
if aest is not None:
aest_out = round(float(aest), 4)
return round(align, 4), aest_out
def build_app():
with gr.Blocks(title="Generative Image Evaluator (CLIP)") as demo:
gr.Markdown("# Generative Image Evaluator (CLIP)")
gr.Markdown("Upload an image and a prompt. Outputs CLIP alignment and an optional aesthetic proxy.")
with gr.Row():
img_in = gr.Image(type="pil", label="Upload image", height=350)
prompt_in = gr.Textbox(label="Prompt", value=DEFAULT_PROMPT)
btn = gr.Button("Evaluate")
out_align = gr.Label(label="Alignment score")
out_aesth = gr.Label(label="Aesthetic score (proxy)")
btn.click(evaluate, [img_in, prompt_in], [out_align, out_aesth])
return demo
demo = build_app()