Spaces:
Running
Running
Update app.py
Browse files6 unmodified lines7891011121314151617181920212223242526272829308 unmodified lines39404142434445464748495051525354555657585960616263646566676869707172731 unmodified line757677787980814 unmodified lines8687888990919293949596971 unmodified line991001011021031041052 unmodified lines1081091101111121131144 unmodified lines1191201211221231241251261271281291301311321331341351361371381391401411421431446 unmodified linesfrom PIL import Imagefrom huggingface_hub import hf_hub_download# ── Model — exactly as in the Pony V7 Captioner notebook ───────────────────────class AestheticScorer(nn.Module):def __init__(self, input_size: int = 768):super().__init__()self.model = nn.Sequential(nn.Linear(input_size, 1024),nn.ReLU(),nn.Dropout(0.5),nn.Linear(1024, 512),nn.ReLU(),nn.Dropout(0.3),nn.Linear(512, 1),)def forward(self, x: torch.Tensor) -> torch.Tensor:return self.model(x)DEVICE = "cuda" if torch.cuda.is_available() else "cpu"print(f"[info] device: {DEVICE}")8 unmodified lines)checkpoint_data = torch.load(ckpt_path, map_location=DEVICE)state_dict = checkpoint_data["state_dict"]# Strip the "model." prefix from keys (same as notebook)state_dict = {k.replace("model.", ""): v for k, v in state_dict.items()}aesthetic_model = AestheticScorer(input_size=768).to(DEVICE)aesthetic_model.load_state_dict(state_dict)aesthetic_model.eval()print("[info] Model ready.")# ── Scoring — identical to notebook ────────────────────────────────────────────@torch.no_grad()def get_score(image: Image.Image) -> float:"""Returns raw float score (typically 0-1 range)."""image_tensor = preprocess(image.convert("RGB")).unsqueeze(0).to(DEVICE)features = clip_model.encode_image(image_tensor).cpu().numpy()norm = np.linalg.norm(features, axis=1, keepdims=True)norm[norm == 0] = 1features = features / normfeatures_t = torch.tensor(features, dtype=torch.float32, device=DEVICE)raw = aesthetic_model(features_t).item()return rawdef raw_to_pony(raw: float) -> int:"""Convert raw score to pony score_0...score_9 (same formula as notebook)."""return int(max(0.0, min(0.99, raw)) * 10)# ── Colour palette ─────────────────────────────────────────────────────────────SCORE_COLOURS = ["#c0392b", "#e74c3c", "#e67e22", "#f39c12", "#d4ac0d","#27ae60", "#1e8449", "#148f77", "#0e6655", "#0a4f42",1 unmodified linedef build_html(raw: float) -> str:pony = raw_to_pony(raw)colour = SCORE_COLOURS[pony]tiles_html = ""4 unmodified linesweight = "700" if active else "400"scale = "scale(1.12)" if active else "scale(1)"opac = "1" if active else "0.45"tiles_html += f"""<div style="background:{bg};border:{border};border-radius:8px;padding:10px 0;text-align:center;font-size:.82rem;font-weight:{weight};color:#fff;transform:{scale};opacity:{opac};transition:all .2s;user-select:none;">score_{i}</div>"""bar_w = min(raw, 1.0) * 100return f"""<div style="font-family:'Inter',sans-serif;padding:8px 0;"><div style="text-align:center;margin-bottom:20px;">1 unmodified linepadding:14px 36px;font-size:2rem;font-weight:800;letter-spacing:.04em;box-shadow:0 4px 20px {colour}66;">score_{pony}</div><div style="color:#aaa;font-size:.85rem;margin-top:8px;">raw score: <code style="color:#ddd">{raw:.4f}</code></div></div><div style="display:grid;grid-template-columns:repeat(10,1fr);gap:6px;margin-bottom:16px;">2 unmodified lines<div style="background:rgba(255,255,255,.1);border-radius:6px;height:8px;overflow:hidden;"><div style="width:{bar_w:.1f}%;height:100%;background:linear-gradient(90deg,#c0392b,#f39c12,#27ae60);border-radius:6px;transition:width .4s;"></div></div><div style="display:flex;justify-content:space-between;font-size:.72rem;color:#777;margin-top:4px;"><span>score_0</span><span>score_9</span>4 unmodified linesdef classify(image):if image is None:return "<p style='color:#888;text-align:center'>Upload an image to score it.</p>"raw = get_score(image)return build_html(raw)# ── Gradio UI ───────────────────────────────────────────────────────────────────with gr.Blocks(title="Aesthetic Classifier - PurpleSmartAI",theme=gr.themes.Soft(primary_hue="purple"),css=".gradio-container{max-width:860px!important;margin:auto} #title{text-align:center} #sub{text-align:center;color:#888;font-size:.9rem;margin-bottom:1.5rem}",) as demo:gr.Markdown("# Aesthetic Classifier", elem_id="title")gr.Markdown("CLIP ViT-L/14 regression model by **PurpleSmartAI** for Pony V7 captioning. ""Outputs a **score_0...score_9** tag used directly in training captions.",elem_id="sub",)with gr.Row():with gr.Column(scale=1):img_input = gr.Image(type="pil", label="Input Image", height=340)run_btn = gr.Button("Score image", variant="primary", size="lg")with gr.Column(scale=1):out_html = gr.HTML(value="<p style='color:#888;text-align:center;padding:40px 0'>Upload an image to see its score.</p>",6 unmodified lines7891011128 unmodified lines2122232425262728293031323334353637383940414243444546474849505152535455565758591 unmodified line616263646566674 unmodified lines727374757677787980818283841 unmodified line868788899091922 unmodified lines95969798991001014 unmodified lines1061071081091101111121131141151161171181191201211221231241251261271281296 unmodified linesfrom PIL import Imagefrom huggingface_hub import hf_hub_downloadDEVICE = "cuda" if torch.cuda.is_available() else "cpu"print(f"[info] device: {DEVICE}")8 unmodified lines)checkpoint_data = torch.load(ckpt_path, map_location=DEVICE)state_dict = checkpoint_data["state_dict"]# Strip "model." prefix — keys become "0.weight", "3.weight", "6.weight"state_dict = {k.replace("model.", ""): v for k, v in state_dict.items()}# Build Sequential directly so keys match ("0.weight", "3.weight", "6.weight")aesthetic_model = nn.Sequential(nn.Linear(768, 1024),nn.ReLU(),nn.Dropout(0.5),nn.Linear(1024, 512),nn.ReLU(),nn.Dropout(0.3),nn.Linear(512, 1),).to(DEVICE)aesthetic_model.load_state_dict(state_dict)aesthetic_model.eval()print("[info] Model ready.")@torch.no_grad()def get_score(image: Image.Image) -> float:image_tensor = preprocess(image.convert("RGB")).unsqueeze(0).to(DEVICE)features = clip_model.encode_image(image_tensor).cpu().numpy()norm = np.linalg.norm(features, axis=1, keepdims=True)norm[norm == 0] = 1features = features / normfeatures_t = torch.tensor(features, dtype=torch.float32, device=DEVICE)return aesthetic_model(features_t).item()def raw_to_pony(raw: float) -> int:return int(max(0.0, min(0.99, raw)) * 10)SCORE_COLOURS = ["#c0392b", "#e74c3c", "#e67e22", "#f39c12", "#d4ac0d","#27ae60", "#1e8449", "#148f77", "#0e6655", "#0a4f42",1 unmodified linedef build_html(raw: float) -> str:pony = raw_to_pony(raw)colour = SCORE_COLOURS[pony]tiles_html = ""4 unmodified linesweight = "700" if active else "400"scale = "scale(1.12)" if active else "scale(1)"opac = "1" if active else "0.45"tiles_html += (f'<div style="background:{bg};border:{border};border-radius:8px;'f'padding:10px 0;text-align:center;font-size:.82rem;font-weight:{weight};color:#fff;'f'transform:{scale};opacity:{opac};transition:all .2s;user-select:none;">score_{i}</div>')bar_w = min(raw, 1.0) * 100return f"""<div style="font-family:'Inter',sans-serif;padding:8px 0;"><div style="text-align:center;margin-bottom:20px;">1 unmodified linepadding:14px 36px;font-size:2rem;font-weight:800;letter-spacing:.04em;box-shadow:0 4px 20px {colour}66;">score_{pony}</div><div style="color:#aaa;font-size:.85rem;margin-top:8px;">raw: <code style="color:#ddd">{raw:.4f}</code></div></div><div style="display:grid;grid-template-columns:repeat(10,1fr);gap:6px;margin-bottom:16px;">2 unmodified lines<div style="background:rgba(255,255,255,.1);border-radius:6px;height:8px;overflow:hidden;"><div style="width:{bar_w:.1f}%;height:100%;background:linear-gradient(90deg,#c0392b,#f39c12,#27ae60);border-radius:6px;"></div></div><div style="display:flex;justify-content:space-between;font-size:.72rem;color:#777;margin-top:4px;"><span>score_0</span><span>score_9</span>4 unmodified linesdef classify(image):if image is None:return "<p style='color:#888;text-align:center'>Upload an image to score it.</p>"return build_html(get_score(image))with gr.Blocks(title="Aesthetic Classifier - PurpleSmartAI",theme=gr.themes.Soft(primary_hue="purple"),css=".gradio-container{max-width:860px!important;margin:auto} #title{text-align:center} #sub{text-align:center;color:#888;font-size:.9rem;margin-bottom:1.5rem}",) as demo:gr.Markdown("# 🎨 Aesthetic Classifier", elem_id="title")gr.Markdown("CLIP ViT-L/14 regression model by **PurpleSmartAI** for Pony V7 captioning. ""Outputs a **score_0…score_9** tag used directly in training captions.",elem_id="sub",)with gr.Row():with gr.Column(scale=1):img_input = gr.Image(type="pil", label="Input Image", height=340)run_btn = gr.Button("✨ Score image", variant="primary", size="lg")with gr.Column(scale=1):out_html = gr.HTML(value="<p style='color:#888;text-align:center;padding:40px 0'>Upload an image to see its score.</p>",
app.py+24-39