Spaces:
Paused
Paused
from flask import Flask, request, jsonify, render_template, send_from_directory | |
from flask_cors import CORS | |
import subprocess | |
import os | |
app = Flask(__name__) | |
CORS(app) | |
# Ensure that Flask looks for the HTML templates in the correct folder | |
app.template_folder = './templates' | |
# Set the directory for the output images | |
output_dir = 'api/output' | |
image_filename = 'generato_paired.png' | |
def index(): | |
return render_template('index.html') | |
def generate_design(): | |
# Command handling | |
command = [ | |
'python', 'app/model/src/eval.py', | |
'--dataset_path', './assets/data/vitonhd', | |
'--batch_size', '1', | |
'--mixed_precision', 'fp16', | |
'--output_dir', output_dir, | |
'--save_name', 'generato_paired', | |
'--num_workers_test', '4', | |
'--sketch_cond_rate', '0.2', | |
'--dataset', 'vitonhd', | |
'--start_cond_rate', '0.0', | |
'--test_order', 'paired' | |
] | |
try: | |
subprocess.run(command, check=True) | |
image_path = os.path.join(output_dir, image_filename) | |
# Check if the image was generated | |
if os.path.exists(image_path): | |
print("IMMAGINE CREATA") | |
return jsonify({"status": "success", "image_url": f"/output/{image_filename}"}) | |
else: | |
print("IMMAGINE NON CREATA") | |
return jsonify({"status": "error", "message": "Image generation failed"}), 500 | |
except subprocess.CalledProcessError as e: | |
return jsonify({"status": "error", "message": str(e)}), 500 | |
# Route to serve the generated image | |
def serve_image(filename): | |
return send_from_directory(output_dir, filename) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=7860) | |