giorgio-caparvi commited on
Commit
98777c8
·
1 Parent(s): 194aaac

adding debug error

Browse files
Files changed (1) hide show
  1. api/app.py +21 -13
api/app.py CHANGED
@@ -19,34 +19,35 @@ def index():
19
  @app.route('/generate-design', methods=['GET','POST'])
20
  def generate_design():
21
  try:
 
22
 
23
  # Getting json
24
  json_data_from_req = request.get_json()
25
  if not json_data_from_req:
 
26
  return "Invalid or missing JSON data", 400
27
- print(json_data_from_req)
28
-
29
 
30
  # Getting Image
31
-
32
  if 'image' not in request.files:
 
33
  return "No image file in request", 400
34
 
35
  image_file = request.files['image']
36
  try:
37
  image = Image.open(image_file)
 
38
  except Exception as e:
 
39
  return f"Failed to open the image: {str(e)}", 400
40
-
41
- # Create an in-memory buffer to store the image (instead of saving to disk)
42
  img_sketch_buffer = io.BytesIO()
43
- # Save the image to the buffer in JPEG format
44
  image.save(img_sketch_buffer, format='JPEG')
45
- # Rewind the buffer's position to the beginning
46
  img_sketch_buffer.seek(0)
 
47
 
48
-
49
- # Creiamo una lista di argomenti come quelli che passeresti via CLI
50
  sys.argv = [
51
  'eval.py',
52
  '--dataset_path', '/api/model/assets/data/vitonhd',
@@ -58,31 +59,38 @@ def generate_design():
58
  '--start_cond_rate', '0.0',
59
  '--test_order', 'paired'
60
  ]
 
61
 
62
- print("################################STO ESEGUENDO LO SCRIPT")
63
- # Cattura tutti gli errori generati dalla chiamata a eval.main
64
  try:
65
  final_image = eval.main(img_sketch_buffer, json_data_from_req)
 
66
  except AttributeError as e:
 
67
  return f"AttributeError: {str(e)}", 500
68
  except TypeError as e:
 
69
  return f"TypeError: {str(e)}", 500
70
  except ValueError as e:
 
71
  return f"ValueError: {str(e)}", 500
72
  except IOError as e:
 
73
  return f"IOError: {str(e)}", 500
74
  except Exception as e:
 
75
  return f"Unexpected error: {str(e)}", 500
76
 
77
- # Save the image to a BytesIO buffer to return via HTTP
78
  img_io = io.BytesIO()
79
  final_image.save(img_io, 'JPEG')
80
  img_io.seek(0)
 
81
 
82
- # Return the image as a file download
83
  return send_file(img_io, mimetype='image/jpeg', as_attachment=True, download_name='inmemory_image.jpg')
84
 
85
  except Exception as e:
 
86
  return str(e), 500
87
 
88
  if __name__ == '__main__':
 
19
  @app.route('/generate-design', methods=['GET','POST'])
20
  def generate_design():
21
  try:
22
+ print("### Inizio generate_design ###") # Stampa per il debug
23
 
24
  # Getting json
25
  json_data_from_req = request.get_json()
26
  if not json_data_from_req:
27
+ print("### JSON non ricevuto ###") # Stampa per il debug
28
  return "Invalid or missing JSON data", 400
29
+ print("### JSON ricevuto:", json_data_from_req) # Stampa per il debug
 
30
 
31
  # Getting Image
 
32
  if 'image' not in request.files:
33
+ print("### Nessuna immagine ricevuta ###") # Stampa per il debug
34
  return "No image file in request", 400
35
 
36
  image_file = request.files['image']
37
  try:
38
  image = Image.open(image_file)
39
+ print("### Immagine aperta con successo ###") # Stampa per il debug
40
  except Exception as e:
41
+ print(f"### Errore nell'apertura dell'immagine: {str(e)} ###") # Stampa per il debug
42
  return f"Failed to open the image: {str(e)}", 400
43
+
44
+ # Creazione buffer di immagine
45
  img_sketch_buffer = io.BytesIO()
 
46
  image.save(img_sketch_buffer, format='JPEG')
 
47
  img_sketch_buffer.seek(0)
48
+ print("### Immagine salvata nel buffer ###") # Stampa per il debug
49
 
50
+ # Argomenti per eval.main
 
51
  sys.argv = [
52
  'eval.py',
53
  '--dataset_path', '/api/model/assets/data/vitonhd',
 
59
  '--start_cond_rate', '0.0',
60
  '--test_order', 'paired'
61
  ]
62
+ print("### Esecuzione eval.main ###") # Stampa per il debug
63
 
64
+ # Esecuzione del modello
 
65
  try:
66
  final_image = eval.main(img_sketch_buffer, json_data_from_req)
67
+ print("### eval.main eseguito con successo ###") # Stampa per il debug
68
  except AttributeError as e:
69
+ print(f"### AttributeError: {str(e)} ###") # Stampa per il debug
70
  return f"AttributeError: {str(e)}", 500
71
  except TypeError as e:
72
+ print(f"### TypeError: {str(e)} ###") # Stampa per il debug
73
  return f"TypeError: {str(e)}", 500
74
  except ValueError as e:
75
+ print(f"### ValueError: {str(e)} ###") # Stampa per il debug
76
  return f"ValueError: {str(e)}", 500
77
  except IOError as e:
78
+ print(f"### IOError: {str(e)} ###") # Stampa per il debug
79
  return f"IOError: {str(e)}", 500
80
  except Exception as e:
81
+ print(f"### Unexpected error: {str(e)} ###") # Stampa per il debug
82
  return f"Unexpected error: {str(e)}", 500
83
 
84
+ # Salvataggio immagine e invio come risposta
85
  img_io = io.BytesIO()
86
  final_image.save(img_io, 'JPEG')
87
  img_io.seek(0)
88
+ print("### Immagine finale pronta per il download ###") # Stampa per il debug
89
 
 
90
  return send_file(img_io, mimetype='image/jpeg', as_attachment=True, download_name='inmemory_image.jpg')
91
 
92
  except Exception as e:
93
+ print(f"### Errore globale: {str(e)} ###") # Stampa per il debug
94
  return str(e), 500
95
 
96
  if __name__ == '__main__':