|
from flask import Flask, request, jsonify |
|
from gradio_client import Client, handle_file |
|
import os |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
client = Client("jallenjia/Change-Clothes-AI") |
|
|
|
@app.route('/tryon', methods=['POST']) |
|
def tryon(): |
|
try: |
|
|
|
data = request.get_json() |
|
|
|
|
|
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 |
|
|
|
|
|
input_dict = { |
|
"background": handle_file(data['background']) if data.get('background') else None, |
|
"layers": data.get('layers', []), |
|
"composite": handle_file(data.get('composite')) if data.get('composite') else None, |
|
"id": data.get('id') |
|
} |
|
|
|
|
|
result = client.predict( |
|
dict=input_dict, |
|
garm_img=handle_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', -1), |
|
category=data.get('category', 'upper_body'), |
|
api_name="/tryon" |
|
) |
|
|
|
|
|
response = { |
|
"output_image": result[0], |
|
"masked_image": result[1] |
|
} |
|
|
|
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=7860) |