Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import base64
|
|
@@ -8,93 +8,58 @@ import base64
|
|
| 8 |
class FashionClassifier:
|
| 9 |
def __init__(self):
|
| 10 |
self.api_url = "https://api.marqo.ai/classify"
|
| 11 |
-
|
| 12 |
-
self.api_key = "your_marqo_api_key" # À remplacer par les secrets
|
| 13 |
|
| 14 |
def classify_image(self, image, max_categories=5, confidence_threshold=0.3):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"max_categories": max_categories,
|
| 32 |
-
"confidence_threshold": confidence_threshold
|
| 33 |
-
}
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return response.json()
|
| 40 |
-
else:
|
| 41 |
-
return {"error": f"Erreur API: {response.status_code}", "details": response.text}
|
| 42 |
-
|
| 43 |
-
except Exception as e:
|
| 44 |
-
return {"error": f"Erreur: {str(e)}"}
|
| 45 |
|
| 46 |
-
# Initialiser le classifieur
|
| 47 |
classifier = FashionClassifier()
|
| 48 |
|
| 49 |
def process_image(image, max_categories=5, confidence=0.3):
|
| 50 |
-
"""Fonction de traitement pour Gradio"""
|
| 51 |
result = classifier.classify_image(image, max_categories, confidence)
|
| 52 |
|
| 53 |
-
if "error" in result:
|
| 54 |
-
return result["error"]
|
| 55 |
-
|
| 56 |
-
# Formater les résultats
|
| 57 |
if "predictions" in result:
|
| 58 |
-
output = "## Résultats
|
| 59 |
-
for
|
| 60 |
-
output += f"
|
| 61 |
-
|
| 62 |
-
if "processing_time" in result:
|
| 63 |
-
output += f"\n⏱️ Temps de traitement: {result['processing_time']}s"
|
| 64 |
-
|
| 65 |
return output
|
| 66 |
else:
|
| 67 |
-
return "
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
gr.Markdown("# 🎨 Classificateur de Vêtements Marqo")
|
| 72 |
-
gr.Markdown("Uploadez une image de vêtement pour la classifier")
|
| 73 |
-
|
| 74 |
with gr.Row():
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
with gr.Column():
|
| 82 |
-
output_text = gr.Markdown(label="Résultats")
|
| 83 |
|
| 84 |
submit_btn.click(
|
| 85 |
fn=process_image,
|
| 86 |
inputs=[image_input, max_categories, confidence],
|
| 87 |
outputs=output_text
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
# Exemples
|
| 91 |
-
gr.Examples(
|
| 92 |
-
examples=[
|
| 93 |
-
["https://example.com/image1.jpg", 5, 0.3],
|
| 94 |
-
["https://example.com/image2.jpg", 3, 0.4]
|
| 95 |
-
],
|
| 96 |
-
inputs=[image_input, max_categories, confidence]
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import base64
|
|
|
|
| 8 |
class FashionClassifier:
|
| 9 |
def __init__(self):
|
| 10 |
self.api_url = "https://api.marqo.ai/classify"
|
| 11 |
+
self.api_key = os.getenv("MARQO_API_KEY") # Clé récupérée des secrets
|
|
|
|
| 12 |
|
| 13 |
def classify_image(self, image, max_categories=5, confidence_threshold=0.3):
|
| 14 |
+
# Convertir image en base64
|
| 15 |
+
buffered = io.BytesIO()
|
| 16 |
+
image.save(buffered, format="JPEG")
|
| 17 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 18 |
+
|
| 19 |
+
headers = {
|
| 20 |
+
"Content-Type": "application/json",
|
| 21 |
+
"Authorization": f"Bearer {self.api_key}"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
payload = {
|
| 25 |
+
"model": "Marqo/Marqo-FashionSigLIP-Classification",
|
| 26 |
+
"image_data": img_str,
|
| 27 |
+
"parameters": {
|
| 28 |
+
"max_categories": max_categories,
|
| 29 |
+
"confidence_threshold": confidence_threshold
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
response = requests.post(self.api_url, headers=headers, json=payload)
|
| 34 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
| 36 |
classifier = FashionClassifier()
|
| 37 |
|
| 38 |
def process_image(image, max_categories=5, confidence=0.3):
|
|
|
|
| 39 |
result = classifier.classify_image(image, max_categories, confidence)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if "predictions" in result:
|
| 42 |
+
output = "## Résultats :\n\n"
|
| 43 |
+
for pred in result["predictions"]:
|
| 44 |
+
output += f"- **{pred['label']}** : {pred['score']*100:.1f}%\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
return output
|
| 46 |
else:
|
| 47 |
+
return "Erreur de classification"
|
| 48 |
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# 🎨 Classificateur de Vêtements")
|
|
|
|
|
|
|
|
|
|
| 51 |
with gr.Row():
|
| 52 |
+
image_input = gr.Image(type="pil")
|
| 53 |
+
output_text = gr.Markdown()
|
| 54 |
+
|
| 55 |
+
max_categories = gr.Slider(1, 10, value=5)
|
| 56 |
+
confidence = gr.Slider(0.1, 1.0, value=0.3)
|
| 57 |
+
submit_btn = gr.Button("Classifier")
|
|
|
|
|
|
|
| 58 |
|
| 59 |
submit_btn.click(
|
| 60 |
fn=process_image,
|
| 61 |
inputs=[image_input, max_categories, confidence],
|
| 62 |
outputs=output_text
|
| 63 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
demo.launch()
|
|
|