File size: 1,764 Bytes
1e9483f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
from gradio_client import Client, file
import os

app = Flask(__name__)

# Initialize Gradio Client
client = Client("ronniechoyy/IDM-VTON-20250428")

@app.route('/tryon', methods=['POST'])
def tryon():
    try:
        # Extract parameters from JSON request
        data = request.get_json()
        
        # Validate required parameters
        if not data or not all(key in data for key in ['background', 'garm_img', 'garment_des']):
            return jsonify({"error": "Missing required parameters: background, garm_img, garment_des"}), 400

        # Prepare input dictionary
        input_dict = {
            "background": file(data.get('background')) if data.get('background') else None,
            "layers": data.get('layers', []),
            "composite": file(data.get('composite')) if data.get('composite') else None
        }

        # Call Gradio API
        result = client.predict(
            dict=input_dict,
            garm_img=file(data['garm_img']),
            garment_des=data['garment_des'],
            is_checked=data.get('is_checked', True),
            is_checked_crop=data.get('is_checked_crop', False),
            denoise_steps=data.get('denoise_steps', 30),
            seed=data.get('seed', 42),
            num_images=data.get('num_images', 1),
            api_name="/tryon"
        )

        # Format response
        response = {
            "generated_images": result[0],  # List of dictionaries with image and caption
            "masked_image": result[1]      # Filepath of masked image
        }

        return jsonify(response), 200

    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)