""" Labasni VTO Service - Hugging Face Space ✅ Compatible avec Gradio Client pour WebSocket temps réel """ import gradio as gr import json import base64 from vto_model import process_frame_vto def vto_api(frame_base64: str, clothes_json: str): """ API endpoint pour le Virtual Try-On ✅ CORRIGÉ : Accepte directement le base64 Args: frame_base64: Image encodée en base64 (STRING) clothes_json: JSON des vêtements à essayer Returns: JSON string avec {success: true, frame: "base64..."} """ try: print(f"🔍 VTO Request received") print(f" Frame length: {len(frame_base64)} chars") print(f" Clothes JSON length: {len(clothes_json)} chars") # Parser le JSON des vêtements clothes_data = json.loads(clothes_json) print(f" ✅ Parsed {len(clothes_data)} clothing item(s)") # Appeler le modèle VTO result = process_frame_vto(frame_base64, clothes_data) print(f" ✅ VTO processing: {result.get('success', False)}") # Retourner en JSON string (pour Gradio Client) return json.dumps(result, ensure_ascii=False) except json.JSONDecodeError as e: error_response = { "success": False, "error": f"Invalid JSON: {str(e)}" } print(f" ❌ JSON Error: {str(e)}") return json.dumps(error_response) except Exception as e: error_response = { "success": False, "error": str(e) } print(f" ❌ Error: {str(e)}") return json.dumps(error_response) # ✅ Interface Gradio (pour tests manuels ET Gradio Client) demo = gr.Interface( fn=vto_api, inputs=[ gr.Textbox( label="Frame (Base64)", placeholder="data:image/jpeg;base64,/9j/4AAQ...", lines=3, max_lines=5 ), gr.Textbox( label="Clothes to Try (JSON)", placeholder='[{"imageURL":"https://...","category":"top"}]', lines=5, value='[{"imageURL":"https://res.cloudinary.com/dechk1ohr/image/upload/v1765128119/istockphoto-483960103-612x612-removebg-preview_fnh1r4.png","category":"top"}]' ) ], outputs=gr.Textbox( label="VTO Result (JSON)", lines=10 ), title="👕 Labasni Virtual Try-On", description="Essayez des vêtements virtuellement avec MediaPipe + OpenCV", examples=[ [ "", # Laissez vide pour test manuel '[{"imageURL":"https://res.cloudinary.com/dechk1ohr/image/upload/v1765128119/istockphoto-483960103-612x612-removebg-preview_fnh1r4.png","category":"top"}]' ] ], api_name="predict", cache_examples=False ) if __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=7860, share=False )