Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# fashionclip_app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 6 |
+
|
| 7 |
+
# Lade das Modell und den Prozessor
|
| 8 |
+
model = CLIPModel.from_pretrained("patrickjohncyh/fashion-clip")
|
| 9 |
+
processor = CLIPProcessor.from_pretrained("patrickjohncyh/fashion-clip")
|
| 10 |
+
|
| 11 |
+
# Prompts für jede Merkmalsgruppe
|
| 12 |
+
category_prompts = ["a t-shirt", "a long-sleeved shirt", "a hoodie", "a sweatshirt", "a pullover", "a tank top"]
|
| 13 |
+
color_prompts = ["a red garment", "a blue garment", "a black garment", "a white garment", "a green garment", "a yellow garment", "a gray garment", "a brown garment", "a pink garment", "a purple garment"]
|
| 14 |
+
pattern_prompts = ["a plain shirt", "a striped shirt", "a floral shirt", "a checked shirt", "a dotted shirt", "an abstract patterned shirt"]
|
| 15 |
+
fit_prompts = ["a slim fit shirt", "an oversized top", "a regular fit shirt", "a cropped shirt", "a shirt with a crew neck", "a shirt with a v-neck", "a shirt with a round neckline"]
|
| 16 |
+
|
| 17 |
+
# Hilfsfunktion: finde das passendste Prompt für eine Gruppe
|
| 18 |
+
def predict_best_prompt(image, prompts):
|
| 19 |
+
print(f"[DEBUG] Image type: {type(image)}, Prompt count: {len(prompts)}")
|
| 20 |
+
inputs = processor(text=prompts, images=[image], return_tensors="pt", padding=True)
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
outputs = model(**inputs)
|
| 23 |
+
logits_per_image = outputs.logits_per_image
|
| 24 |
+
probs = logits_per_image.softmax(dim=1).squeeze().tolist()
|
| 25 |
+
best_idx = torch.tensor(probs).argmax().item()
|
| 26 |
+
return prompts[best_idx], probs[best_idx]
|
| 27 |
+
|
| 28 |
+
# Hauptfunktion für die App
|
| 29 |
+
def analyze_image(image):
|
| 30 |
+
if image is None:
|
| 31 |
+
return "⚠️ Please upload or take a picture first."
|
| 32 |
+
|
| 33 |
+
results = {}
|
| 34 |
+
results["Category"], cat_score = predict_best_prompt(image, category_prompts)
|
| 35 |
+
results["Color"], color_score = predict_best_prompt(image, color_prompts)
|
| 36 |
+
results["Pattern"], pattern_score = predict_best_prompt(image, pattern_prompts)
|
| 37 |
+
results["Fit"], fit_score = predict_best_prompt(image, fit_prompts)
|
| 38 |
+
|
| 39 |
+
return f"""
|
| 40 |
+
Category: {results['Category']} ({cat_score:.2f})\n
|
| 41 |
+
Color: {results['Color']} ({color_score:.2f})\n
|
| 42 |
+
Pattern: {results['Pattern']} ({pattern_score:.2f})\n
|
| 43 |
+
Fit: {results['Fit']} ({fit_score:.2f})
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
# Gradio UI erstellen
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=analyze_image,
|
| 49 |
+
inputs=gr.Image(type="pil", label="Upload or take a picture", sources=["upload", "webcam"]),
|
| 50 |
+
outputs="text",
|
| 51 |
+
title="Fashion Attribute Predictor (Prototype 2)",
|
| 52 |
+
description="Upload or capture an image of a t-shirt or pullover. The model predicts category, color, pattern, and fit using FashionCLIP."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# App starten
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
iface.launch()
|