Spaces:
Paused
Paused
File size: 4,500 Bytes
cea6160 1e330bd 67b8057 554a6f2 9abe70d 487c322 cc3c167 5b474cb bcbc057 5b474cb 9abe70d bed9a40 a212e09 bed9a40 554a6f2 cc3c167 bed9a40 9abe70d 98777c8 cc3c167 98777c8 cc3c167 98777c8 a3d4937 5b474cb 98777c8 cc3c167 a3d4937 5b474cb cc3c167 5b474cb 98777c8 cc3c167 98777c8 cc3c167 98777c8 cc3c167 98777c8 a3d4937 98777c8 487c322 9abe70d 98777c8 896542a bcbc057 98777c8 33377b9 98777c8 33377b9 98777c8 bcbc057 33377b9 98777c8 bcbc057 33377b9 98777c8 bcbc057 33377b9 98777c8 bcbc057 33377b9 98777c8 bcbc057 9abe70d 98777c8 cb14b48 98777c8 e2edca9 764f9ac e2edca9 9abe70d 98777c8 9abe70d cea6160 bed9a40 194aaac |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
from flask import Flask, render_template, send_from_directory, send_file, request, jsonify
from flask_cors import CORS
import subprocess
import os
import io
from model.src.utils.arg_parser import eval_parse_args # Nuovo import corretto
import sys
from PIL import Image
import base64
import traceback
from model.src import eval
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/generate-design', methods=['GET','POST'])
def generate_design():
try:
print("### Inizio generate_design ###") # Stampa per il debug
# Getting json
json_data_from_req = request.get_json()
if not json_data_from_req:
print("### JSON non ricevuto ###") # Stampa per il debug
return "Invalid or missing JSON data", 400
print("### JSON ricevuto:", json_data_from_req) # Stampa per il debug
# Getting Image
if 'image' not in json_data_from_req:
print("### Nessuna immagine ricevuta ###") # Stampa per il debug
return "No image file in request", 400
encoded_image = json_data_from_req['image']
try:
image_data = base64.b64decode(encoded_image)
image = Image.open(io.BytesIO(image_data))
print("### Immagine aperta con successo ###") # Stampa per il debug
except Exception as e:
print(f"### Errore nell'apertura dell'immagine: {str(e)} ###") # Stampa per il debug
return f"Failed to open the image: {str(e)}", 400
# Creazione buffer di immagine
img_sketch_buffer = io.BytesIO()
image.save(img_sketch_buffer, format='JPEG')
img_sketch_buffer.seek(0)
print("### Immagine salvata nel buffer ###") # Stampa per il debug
# Argomenti per eval.main
sys.argv = [
'eval.py',
'--dataset_path', '/api/model/assets/data/vitonhd',
'--batch_size', '1',
'--mixed_precision', 'fp16',
'--num_workers_test', '4',
'--sketch_cond_rate', '0.2',
'--dataset', 'vitonhd',
'--start_cond_rate', '0.0',
'--test_order', 'paired'
]
print("### Esecuzione eval.main ###") # Stampa per il debug
import traceback
# Esecuzione del modello
try:
final_image = eval.main(img_sketch_buffer, json_data_from_req)
print("### eval.main eseguito con successo ###") # Stampa per il debug
except AttributeError as e:
print(f"### AttributeError: {str(e)} ###") # Stampa per il debug
return f"AttributeError: {traceback.format_exc()}", 500
except TypeError as e:
print(f"### TypeError: {str(e)} ###") # Stampa per il debug
return f"TypeError: {traceback.format_exc()}", 500
except ValueError as e:
print(f"### ValueError: {str(e)} ###") # Stampa per il debug
return f"ValueError: {traceback.format_exc()}", 500
except IOError as e:
print(f"### IOError: {str(e)} ###") # Stampa per il debug
return f"IOError: {traceback.format_exc()}", 500
except Exception as e:
print(f"### Unexpected error: {str(e)} ###") # Stampa per il debug
return f"Unexpected error: {traceback.format_exc()}", 500
# Salvataggio immagine e invio come risposta
img_io = io.BytesIO()
final_image.save(img_io, 'JPEG')
img_io.seek(0)
print("### Immagine finale pronta per il download ###") # Stampa per il debug
return send_file(img_io, mimetype='image/jpeg', as_attachment=True, download_name='inmemory_image.jpg')
except Exception as e:
print(f"### Errore globale: {str(e)} ###") # Stampa per il debug
return str(e), 500
@app.route('/test_post', methods=['POST'])
def test_post():
# Verifica se la richiesta è POST
if request.method == 'POST':
# Ottieni i dati dal corpo della richiesta
data = request.get_json()
# Se i dati non sono presenti o il formato non è corretto
if not data:
return jsonify({'message': 'Nessun dato ricevuto o dati non validi'}), 400
# Restituisci una risposta con i dati ricevuti
return jsonify({'message': 'POST funzionante', 'data_received': data}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True) |