Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -71,7 +71,6 @@ def health_check():
|
|
| 71 |
async def classify_fashion(image_data: dict):
|
| 72 |
"""
|
| 73 |
Endpoint pour Lovable - accepte une URL d'image
|
| 74 |
-
Format attendu: {"imageUrl": "https://example.com/image.jpg"}
|
| 75 |
"""
|
| 76 |
try:
|
| 77 |
if not model or not processor:
|
|
@@ -82,15 +81,15 @@ async def classify_fashion(image_data: dict):
|
|
| 82 |
if not image_url:
|
| 83 |
raise HTTPException(status_code=400, detail="imageUrl is required")
|
| 84 |
|
| 85 |
-
# Télécharger l'image
|
| 86 |
response = requests.get(image_url, timeout=30)
|
| 87 |
response.raise_for_status()
|
| 88 |
|
| 89 |
# Ouvrir et préparer l'image
|
| 90 |
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 91 |
-
image.
|
| 92 |
|
| 93 |
-
# Préparer
|
| 94 |
all_english_categories = []
|
| 95 |
category_mapping = {}
|
| 96 |
|
|
@@ -99,39 +98,47 @@ async def classify_fashion(image_data: dict):
|
|
| 99 |
for en_cat in en_categories:
|
| 100 |
category_mapping[en_cat] = fr_cat
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
text=all_english_categories,
|
| 105 |
-
images=image,
|
| 106 |
-
return_tensors="pt",
|
| 107 |
-
padding=True, # ← CORRECTION IMPORTANTE
|
| 108 |
-
truncation=True,
|
| 109 |
-
max_length=77,
|
| 110 |
-
return_overflowing_tokens=False
|
| 111 |
-
)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
probs = logits_per_image.softmax(dim=1)
|
| 121 |
-
|
| 122 |
-
results = {cat: prob.item() for cat, prob in zip(all_english_categories, probs[0])}
|
| 123 |
|
| 124 |
-
# Trouver la catégorie avec le meilleur score
|
| 125 |
best_english_category = max(results, key=results.get)
|
| 126 |
confidence = results[best_english_category]
|
| 127 |
|
| 128 |
-
# Convertir en
|
|
|
|
|
|
|
|
|
|
| 129 |
best_french_category = category_mapping.get(best_english_category, "autre")
|
| 130 |
|
| 131 |
return {
|
| 132 |
"success": True,
|
| 133 |
"category": best_french_category,
|
| 134 |
-
"confidence": round(
|
| 135 |
"colorHex": "#000000",
|
| 136 |
"originalCategory": best_english_category,
|
| 137 |
"method": "modli-api"
|
|
|
|
| 71 |
async def classify_fashion(image_data: dict):
|
| 72 |
"""
|
| 73 |
Endpoint pour Lovable - accepte une URL d'image
|
|
|
|
| 74 |
"""
|
| 75 |
try:
|
| 76 |
if not model or not processor:
|
|
|
|
| 81 |
if not image_url:
|
| 82 |
raise HTTPException(status_code=400, detail="imageUrl is required")
|
| 83 |
|
| 84 |
+
# Télécharger l'image
|
| 85 |
response = requests.get(image_url, timeout=30)
|
| 86 |
response.raise_for_status()
|
| 87 |
|
| 88 |
# Ouvrir et préparer l'image
|
| 89 |
image = Image.open(BytesIO(response.content)).convert("RGB")
|
| 90 |
+
image = image.resize((224, 224)) # Taille standard pour CLIP
|
| 91 |
|
| 92 |
+
# Préparer les catégories
|
| 93 |
all_english_categories = []
|
| 94 |
category_mapping = {}
|
| 95 |
|
|
|
|
| 98 |
for en_cat in en_categories:
|
| 99 |
category_mapping[en_cat] = fr_cat
|
| 100 |
|
| 101 |
+
# === NOUVELLE APPROCHE : Traitement séquentiel ===
|
| 102 |
+
results = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
for category in all_english_categories:
|
| 105 |
+
try:
|
| 106 |
+
# Traiter chaque catégorie individuellement
|
| 107 |
+
inputs = processor(
|
| 108 |
+
text=[category], # Une seule catégorie à la fois
|
| 109 |
+
images=image,
|
| 110 |
+
return_tensors="pt",
|
| 111 |
+
padding=True,
|
| 112 |
+
truncation=True,
|
| 113 |
+
max_length=77,
|
| 114 |
+
return_overflowing_tokens=False
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with torch.no_grad():
|
| 118 |
+
outputs = model(**inputs)
|
| 119 |
+
results[category] = outputs.logits_per_image.item()
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"Erreur avec la catégorie {category}: {e}")
|
| 123 |
+
results[category] = -10.0 # Valeur très basse en cas d'erreur
|
| 124 |
|
| 125 |
+
# Trouver la meilleure catégorie
|
| 126 |
+
if not results:
|
| 127 |
+
raise HTTPException(status_code=500, detail="Aucun résultat obtenu")
|
|
|
|
|
|
|
|
|
|
| 128 |
|
|
|
|
| 129 |
best_english_category = max(results, key=results.get)
|
| 130 |
confidence = results[best_english_category]
|
| 131 |
|
| 132 |
+
# Convertir le score en probabilité (approximative)
|
| 133 |
+
confidence_normalized = 1 / (1 + torch.exp(torch.tensor(-confidence))).item()
|
| 134 |
+
|
| 135 |
+
# Catégorie française
|
| 136 |
best_french_category = category_mapping.get(best_english_category, "autre")
|
| 137 |
|
| 138 |
return {
|
| 139 |
"success": True,
|
| 140 |
"category": best_french_category,
|
| 141 |
+
"confidence": round(confidence_normalized, 4),
|
| 142 |
"colorHex": "#000000",
|
| 143 |
"originalCategory": best_english_category,
|
| 144 |
"method": "modli-api"
|