Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,7 +67,6 @@ def health_check():
|
|
| 67 |
"status": "ready" if model else "loading"
|
| 68 |
}
|
| 69 |
|
| 70 |
-
# --- NOUVELLE ROUTE POUR LOVABLE ---
|
| 71 |
@app.post("/classify")
|
| 72 |
async def classify_fashion(image_data: dict):
|
| 73 |
"""
|
|
@@ -84,7 +83,7 @@ async def classify_fashion(image_data: dict):
|
|
| 84 |
raise HTTPException(status_code=400, detail="imageUrl is required")
|
| 85 |
|
| 86 |
# Télécharger l'image depuis l'URL
|
| 87 |
-
response = requests.get(image_url)
|
| 88 |
response.raise_for_status()
|
| 89 |
|
| 90 |
# Ouvrir et préparer l'image
|
|
@@ -93,43 +92,48 @@ async def classify_fashion(image_data: dict):
|
|
| 93 |
|
| 94 |
# Préparer toutes les catégories en anglais
|
| 95 |
all_english_categories = []
|
|
|
|
|
|
|
| 96 |
for fr_cat, en_categories in CATEGORIES_FR.items():
|
| 97 |
all_english_categories.extend(en_categories)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
truncation=True
|
| 108 |
-
)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
results[category] = outputs.logits_per_image.item()
|
| 113 |
|
| 114 |
-
# Trouver la catégorie
|
| 115 |
best_english_category = max(results, key=results.get)
|
| 116 |
confidence = results[best_english_category]
|
| 117 |
|
| 118 |
# Convertir en catégorie française
|
| 119 |
-
best_french_category = "autre"
|
| 120 |
-
for fr_cat, en_categories in CATEGORIES_FR.items():
|
| 121 |
-
if best_english_category in en_categories:
|
| 122 |
-
best_french_category = fr_cat
|
| 123 |
-
break
|
| 124 |
-
|
| 125 |
-
# Normaliser la confiance entre 0 et 1
|
| 126 |
-
confidence_normalized = 1 / (1 + torch.exp(torch.tensor(-confidence))).item()
|
| 127 |
|
| 128 |
# Format de réponse exact pour Lovable
|
| 129 |
return {
|
| 130 |
"success": True,
|
| 131 |
"category": best_french_category,
|
| 132 |
-
"confidence": round(
|
| 133 |
"colorHex": "#000000",
|
| 134 |
"originalCategory": best_english_category,
|
| 135 |
"method": "modli-api"
|
|
@@ -139,7 +143,7 @@ async def classify_fashion(image_data: dict):
|
|
| 139 |
raise HTTPException(status_code=400, detail=f"Invalid image URL: {str(e)}")
|
| 140 |
except Exception as e:
|
| 141 |
raise HTTPException(status_code=500, detail=f"Classification error: {str(e)}")
|
| 142 |
-
|
| 143 |
# Ancienne route pour compatibilité (si nécessaire)
|
| 144 |
@app.post("/analyze")
|
| 145 |
async def analyze_image_old():
|
|
|
|
| 67 |
"status": "ready" if model else "loading"
|
| 68 |
}
|
| 69 |
|
|
|
|
| 70 |
@app.post("/classify")
|
| 71 |
async def classify_fashion(image_data: dict):
|
| 72 |
"""
|
|
|
|
| 83 |
raise HTTPException(status_code=400, detail="imageUrl is required")
|
| 84 |
|
| 85 |
# Télécharger l'image depuis l'URL
|
| 86 |
+
response = requests.get(image_url, timeout=10)
|
| 87 |
response.raise_for_status()
|
| 88 |
|
| 89 |
# Ouvrir et préparer l'image
|
|
|
|
| 92 |
|
| 93 |
# Préparer toutes les catégories en anglais
|
| 94 |
all_english_categories = []
|
| 95 |
+
category_mapping = {} # Pour mapper vers le français
|
| 96 |
+
|
| 97 |
for fr_cat, en_categories in CATEGORIES_FR.items():
|
| 98 |
all_english_categories.extend(en_categories)
|
| 99 |
+
for en_cat in en_categories:
|
| 100 |
+
category_mapping[en_cat] = fr_cat
|
| 101 |
+
|
| 102 |
+
# Traitement par lots de toutes les catégories
|
| 103 |
+
inputs = processor(
|
| 104 |
+
text=all_english_categories,
|
| 105 |
+
images=image,
|
| 106 |
+
return_tensors="pt",
|
| 107 |
+
padding=True,
|
| 108 |
+
truncation=True,
|
| 109 |
+
max_length=77,
|
| 110 |
+
return_overflowing_tokens=False
|
| 111 |
+
)
|
| 112 |
|
| 113 |
+
# Déplacer sur GPU si disponible
|
| 114 |
+
if torch.cuda.is_available():
|
| 115 |
+
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 116 |
+
|
| 117 |
+
with torch.no_grad():
|
| 118 |
+
outputs = model(**inputs)
|
| 119 |
+
logits_per_image = outputs.logits_per_image
|
| 120 |
+
probs = logits_per_image.softmax(dim=1)
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
# Convertir en dictionnaire de résultats
|
| 123 |
+
results = {cat: prob.item() for cat, prob in zip(all_english_categories, probs[0])}
|
|
|
|
| 124 |
|
| 125 |
+
# Trouver la catégorie avec le meilleur score
|
| 126 |
best_english_category = max(results, key=results.get)
|
| 127 |
confidence = results[best_english_category]
|
| 128 |
|
| 129 |
# Convertir en catégorie française
|
| 130 |
+
best_french_category = category_mapping.get(best_english_category, "autre")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Format de réponse exact pour Lovable
|
| 133 |
return {
|
| 134 |
"success": True,
|
| 135 |
"category": best_french_category,
|
| 136 |
+
"confidence": round(confidence, 4),
|
| 137 |
"colorHex": "#000000",
|
| 138 |
"originalCategory": best_english_category,
|
| 139 |
"method": "modli-api"
|
|
|
|
| 143 |
raise HTTPException(status_code=400, detail=f"Invalid image URL: {str(e)}")
|
| 144 |
except Exception as e:
|
| 145 |
raise HTTPException(status_code=500, detail=f"Classification error: {str(e)}")
|
| 146 |
+
|
| 147 |
# Ancienne route pour compatibilité (si nécessaire)
|
| 148 |
@app.post("/analyze")
|
| 149 |
async def analyze_image_old():
|