Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from gradio_client import Client, file
|
3 |
+
import os
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# Initialize Gradio Client
|
8 |
+
client = Client("ronniechoyy/IDM-VTON-20250428")
|
9 |
+
|
10 |
+
@app.route('/tryon', methods=['POST'])
|
11 |
+
def tryon():
|
12 |
+
try:
|
13 |
+
# Extract parameters from JSON request
|
14 |
+
data = request.get_json()
|
15 |
+
|
16 |
+
# Validate required parameters
|
17 |
+
if not data or not all(key in data for key in ['background', 'garm_img', 'garment_des']):
|
18 |
+
return jsonify({"error": "Missing required parameters: background, garm_img, garment_des"}), 400
|
19 |
+
|
20 |
+
# Prepare input dictionary
|
21 |
+
input_dict = {
|
22 |
+
"background": file(data.get('background')) if data.get('background') else None,
|
23 |
+
"layers": data.get('layers', []),
|
24 |
+
"composite": file(data.get('composite')) if data.get('composite') else None
|
25 |
+
}
|
26 |
+
|
27 |
+
# Call Gradio API
|
28 |
+
result = client.predict(
|
29 |
+
dict=input_dict,
|
30 |
+
garm_img=file(data['garm_img']),
|
31 |
+
garment_des=data['garment_des'],
|
32 |
+
is_checked=data.get('is_checked', True),
|
33 |
+
is_checked_crop=data.get('is_checked_crop', False),
|
34 |
+
denoise_steps=data.get('denoise_steps', 30),
|
35 |
+
seed=data.get('seed', 42),
|
36 |
+
num_images=data.get('num_images', 1),
|
37 |
+
api_name="/tryon"
|
38 |
+
)
|
39 |
+
|
40 |
+
# Format response
|
41 |
+
response = {
|
42 |
+
"generated_images": result[0], # List of dictionaries with image and caption
|
43 |
+
"masked_image": result[1] # Filepath of masked image
|
44 |
+
}
|
45 |
+
|
46 |
+
return jsonify(response), 200
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
return jsonify({"error": str(e)}), 500
|
50 |
+
|
51 |
+
if __name__ == '__main__':
|
52 |
+
app.run(host='0.0.0.0', port=5000)
|