Files
change_cloth/change_proxy/change_cloth_proxy.py
T
2026-03-10 08:07:17 +00:00

131 lines
4.7 KiB
Python

import os
import requests
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import logging
from datetime import datetime
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_FOLDER = os.path.join(APP_ROOT, 'static')
app = Flask(__name__, static_folder=STATIC_FOLDER, static_url_path='/static')
CORS(app)
CHANGE_APP_BASE_URL = "http://127.0.0.1:28888"
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def log_message(msg):
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}")
logger.info(msg)
@app.route('/')
def index():
return send_from_directory(STATIC_FOLDER, 'index.html')
@app.route('/health', methods=['GET'])
def health():
return jsonify({"status": "ok", "service": "change_cloth_proxy"})
@app.route('/change_cloth', methods=['POST'])
def change_cloth():
log_message("change_cloth called")
data = request.get_json()
if not data:
return jsonify({"ret": -1, "state": -1, "msg": "No JSON data provided"}), 400
required_fields = ["human_url", "cloth_url", "output_format"]
for field in required_fields:
if field not in data:
return jsonify({"ret": -1, "state": -1, "msg": f"Missing required field: {field}"}), 400
log_message(f"Forwarding change_cloth request: human_url={data.get('human_url')}, cloth_url={data.get('cloth_url')}")
try:
response = requests.post(
f"{CHANGE_APP_BASE_URL}/change_cloth",
json=data,
timeout=600
)
log_message(f"change_cloth response status: {response.status_code}")
return jsonify(response.json()), response.status_code
except requests.exceptions.Timeout:
log_message("change_cloth request timeout")
return jsonify({"ret": -1, "state": -1, "msg": "Request timeout"}), 504
except requests.exceptions.RequestException as e:
log_message(f"change_cloth request error: {str(e)}")
return jsonify({"ret": -1, "state": -1, "msg": f"Request failed: {str(e)}"}), 500
@app.route('/change_cloth_base64', methods=['POST'])
def change_cloth_base64():
log_message("change_cloth_base64 called")
data = request.get_json()
if not data:
return jsonify({"ret": -1, "state": -1, "msg": "No JSON data provided"}), 400
required_fields = ["human_img", "cloth_img", "output_format"]
for field in required_fields:
if field not in data:
return jsonify({"ret": -1, "state": -1, "msg": f"Missing required field: {field}"}), 400
log_message("Forwarding change_cloth_base64 request")
try:
response = requests.post(
f"{CHANGE_APP_BASE_URL}/change_cloth_base64",
json=data,
timeout=600
)
log_message(f"change_cloth_base64 response status: {response.status_code}")
return jsonify(response.json()), response.status_code
except requests.exceptions.Timeout:
log_message("change_cloth_base64 request timeout")
return jsonify({"ret": -1, "state": -1, "msg": "Request timeout"}), 504
except requests.exceptions.RequestException as e:
log_message(f"change_cloth_base64 request error: {str(e)}")
return jsonify({"ret": -1, "state": -1, "msg": f"Request failed: {str(e)}"}), 500
@app.route('/do_change_cloth', methods=['POST'])
def do_change_cloth():
log_message("do_change_cloth called")
data = request.get_json()
if not data:
return jsonify({"ret": -1, "state": -1, "msg": "No JSON data provided"}), 400
required_fields = ["human_url", "cloth_url", "output_format"]
for field in required_fields:
if field not in data:
return jsonify({"ret": -1, "state": -1, "msg": f"Missing required field: {field}"}), 400
log_message(f"Forwarding do_change_cloth request: human_url={data.get('human_url')}, cloth_url={data.get('cloth_url')}")
try:
response = requests.post(
f"{CHANGE_APP_BASE_URL}/do_change_cloth",
json=data,
timeout=600
)
log_message(f"do_change_cloth response status: {response.status_code}")
return jsonify(response.json()), response.status_code
except requests.exceptions.Timeout:
log_message("do_change_cloth request timeout")
return jsonify({"ret": -1, "state": -1, "msg": "Request timeout"}), 504
except requests.exceptions.RequestException as e:
log_message(f"do_change_cloth request error: {str(e)}")
return jsonify({"ret": -1, "state": -1, "msg": f"Request failed: {str(e)}"}), 500
if __name__ == '__main__':
log_message(f"Starting change_cloth_proxy service on port 5000, forwarding to {CHANGE_APP_BASE_URL}")
app.run(host="0.0.0.0", port=5000, debug=True)