Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,47 +30,88 @@ def predict(image):
|
|
| 30 |
probabilities = torch.nn.functional.softmax(logits, dim=-1)[0]
|
| 31 |
top_probs, top_indices = torch.topk(probabilities, 5) # Top 5 predictions
|
| 32 |
|
| 33 |
-
# Formatage des résultats
|
| 34 |
-
|
| 35 |
-
for
|
| 36 |
pred_label = model.config.id2label[idx.item()]
|
| 37 |
confidence = prob.item()
|
| 38 |
-
if confidence > 0.
|
| 39 |
-
|
| 40 |
|
| 41 |
-
if not
|
| 42 |
-
return "Je ne suis pas sûr de reconnaître cet item. Essayez avec une image plus claire."
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
return f"Une erreur s'est produite
|
| 48 |
-
|
| 49 |
-
# Configuration de l'interface Gradio
|
| 50 |
-
title = "Fashion Item Classifier"
|
| 51 |
-
description = (
|
| 52 |
-
"Upload an image of a clothing item, and I will classify it. "
|
| 53 |
-
"This is a general-purpose model (ImageNet). For better accuracy on fashion items, "
|
| 54 |
-
"a specialized model is needed."
|
| 55 |
-
)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# Lancement de l'application
|
| 72 |
if __name__ == "__main__":
|
| 73 |
-
# Cette configuration est cruciale pour Hugging Face Spaces
|
| 74 |
demo.launch(
|
| 75 |
debug=True,
|
| 76 |
server_name="0.0.0.0",
|
|
|
|
| 30 |
probabilities = torch.nn.functional.softmax(logits, dim=-1)[0]
|
| 31 |
top_probs, top_indices = torch.topk(probabilities, 5) # Top 5 predictions
|
| 32 |
|
| 33 |
+
# Formatage des résultats sous forme de dictionnaire pour l'affichage
|
| 34 |
+
results = {}
|
| 35 |
+
for prob, idx in zip(top_probs, top_indices):
|
| 36 |
pred_label = model.config.id2label[idx.item()]
|
| 37 |
confidence = prob.item()
|
| 38 |
+
if confidence > 0.01: # Seuil de confiance à 1%
|
| 39 |
+
results[pred_label] = confidence
|
| 40 |
|
| 41 |
+
if not results:
|
| 42 |
+
return {"Aucune prédiction fiable": 0.0}, "Je ne suis pas sûr de reconnaître cet item. Essayez avec une image plus claire."
|
| 43 |
|
| 44 |
+
# Créer un message de résultat
|
| 45 |
+
top_prediction = list(results.items())[0]
|
| 46 |
+
message = f"🏷️ Prédiction principale: {top_prediction[0]} ({top_prediction[1]:.2%})"
|
| 47 |
+
|
| 48 |
+
return results, message
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
+
return {"Erreur": 0.0}, f"Une erreur s'est produite: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Interface Gradio améliorée
|
| 54 |
+
with gr.Blocks(title="Fashion Classifier", theme=gr.themes.Soft()) as demo:
|
| 55 |
+
gr.Markdown("# 👗 Fashion Item Classifier")
|
| 56 |
+
gr.Markdown("Téléchargez une image de vêtement pour le classer automatiquement")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column(scale=1):
|
| 60 |
+
image_input = gr.Image(
|
| 61 |
+
type="pil",
|
| 62 |
+
label="Image du vêtement",
|
| 63 |
+
height=300,
|
| 64 |
+
sources=["upload", "webcam", "clipboard"]
|
| 65 |
+
)
|
| 66 |
+
upload_btn = gr.Button("🚀 Analyser l'image", variant="primary")
|
| 67 |
+
|
| 68 |
+
with gr.Column(scale=1):
|
| 69 |
+
label_output = gr.Label(
|
| 70 |
+
label="Résultats de classification",
|
| 71 |
+
num_top_classes=5,
|
| 72 |
+
height=300
|
| 73 |
+
)
|
| 74 |
+
text_output = gr.Textbox(
|
| 75 |
+
label="Conclusion",
|
| 76 |
+
interactive=False
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Exemples
|
| 80 |
+
gr.Examples(
|
| 81 |
+
examples=[
|
| 82 |
+
["https://images.unsplash.com/photo-1552374196-c4e7ffc6e126?w=300"], # T-shirt
|
| 83 |
+
["https://images.unsplash.com/photo-1543163521-1bf539c55dd2?w=300"], # Chaussures
|
| 84 |
+
["https://images.unsplash.com/photo-1594633312681-425c7b97ccd1?w=300"] # Robe
|
| 85 |
+
],
|
| 86 |
+
inputs=image_input,
|
| 87 |
+
label="Exemples d'images à tester"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Instructions
|
| 91 |
+
gr.Markdown("""
|
| 92 |
+
### 📋 Instructions
|
| 93 |
+
- Téléchargez une image claire d'un vêtement
|
| 94 |
+
- L'image doit montrer le vêtement de face
|
| 95 |
+
- Fond uni recommandé pour de meilleurs résultats
|
| 96 |
+
- Cliquez sur 'Analyser l'image' pour obtenir la classification
|
| 97 |
+
""")
|
| 98 |
+
|
| 99 |
+
# Liaison du bouton
|
| 100 |
+
upload_btn.click(
|
| 101 |
+
fn=predict,
|
| 102 |
+
inputs=image_input,
|
| 103 |
+
outputs=[label_output, text_output]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Liaison aussi quand on upload une image
|
| 107 |
+
image_input.upload(
|
| 108 |
+
fn=predict,
|
| 109 |
+
inputs=image_input,
|
| 110 |
+
outputs=[label_output, text_output]
|
| 111 |
+
)
|
| 112 |
|
| 113 |
+
# Lancement de l'application
|
| 114 |
if __name__ == "__main__":
|
|
|
|
| 115 |
demo.launch(
|
| 116 |
debug=True,
|
| 117 |
server_name="0.0.0.0",
|