Spaces:
Running
Running
File size: 2,854 Bytes
91b5ad5 fa6dda0 b6d3c97 31cdd9f fa6dda0 31cdd9f 91b5ad5 fa6dda0 31cdd9f a6b2a49 b6d3c97 31cdd9f a6b2a49 31cdd9f fa6dda0 a6b2a49 fa6dda0 31cdd9f 1337a8b 31cdd9f f689134 31cdd9f b3d9d3e 31cdd9f b3d9d3e d809552 b3d9d3e f8f1b61 31cdd9f b6d3c97 0739b36 9ea3117 639c7cc 9ea3117 31cdd9f 91b5ad5 31cdd9f 9037417 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
from flask import Flask, request, jsonify
from transformers import AutoProcessor, CLIPModel
from PIL import Image
import base64
import io
# Charger le modèle CLIP et le processeur
model = CLIPModel.from_pretrained("patrickjohncyh/fashion-clip")
processor = AutoProcessor.from_pretrained("patrickjohncyh/fashion-clip")
# Créer une instance Flask
app = Flask(__name__)
# Fonction pour la classification d'image avec du texte en entrée
def classify_image_with_text(text, image):
keywords = text.split(',')
image = decode_image_from_base64(image)
inputs = processor(text=keywords, images=image, return_tensors="pt", padding=True)
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image # score de similarité image-texte
probs = logits_per_image.softmax(dim=1)
predicted_class_index = probs.argmax(dim=1).item()
predicted_label = keywords[predicted_class_index]
return predicted_label
# Fonction pour la classification d'image avec des propriétés et options
def classify_image_with_properties(properties, image):
image = decode_image_from_base64(image)
result = []
for prop in properties:
property_name = prop['property']
options = prop['options']
keywords = options.split(',')
# Effectuer la classification pour chaque ensemble propriété-options
inputs = processor(text=keywords, images=image, return_tensors="pt", padding=True)
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1)
predicted_class_index = probs.argmax(dim=1).item()
# Obtenir l'option complète correspondant à l'indice prédit
predicted_label = keywords[predicted_class_index]
result.append({ "property": property_name, "value": predicted_label })
return result
# Fonction pour décoder une image encodée en base64 en objet PIL.Image.Image
def decode_image_from_base64(image_data):
image_data = base64.b64decode(image_data)
image = Image.open(io.BytesIO(image_data))
return image
@app.get("/")
def root():
return "Welcome to the Fashion Clip API!"
# Route pour l'API REST de classification simple
@app.route('/api/classify', methods=['POST'])
def classify():
data = request.json
text = data['text']
image = data['image']
result = classify_image_with_text(text, image)
return jsonify({'result': result})
# Route pour l'API REST de classification avec propriétés et options
@app.route('/api/classify-properties', methods=['POST'])
def classify_properties():
data = request.json
properties = data['properties']
image = data['image']
result = classify_image_with_properties(properties, image)
return jsonify({'result': result})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
|