giorgio-caparvi commited on
Commit
179259d
·
1 Parent(s): 5457fbb

Button to download the image

Browse files
Files changed (3) hide show
  1. api/app.py +8 -29
  2. api/templates/index.html +19 -25
  3. api/templates/output.html +12 -0
api/app.py CHANGED
@@ -1,16 +1,10 @@
1
- from flask import Flask, request, jsonify, render_template, send_from_directory
2
- from flask_cors import CORS
3
  import subprocess
4
  import os
5
 
6
  app = Flask(__name__)
7
- CORS(app)
8
 
9
- # Ensure that Flask looks for the HTML templates in the correct folder
10
- app.template_folder = './templates'
11
-
12
- print(f"Current working directory: {os.getcwd()}")
13
- # Set the directory for the output images
14
  output_dir = '/api/output/generato_paired_paired/images'
15
  image_filename = '03191_00.jpg'
16
 
@@ -20,7 +14,7 @@ def index():
20
 
21
  @app.route('/generate-design', methods=['POST'])
22
  def generate_design():
23
- # Command handling
24
  command = [
25
  'python', '/api/model/src/eval.py',
26
  '--dataset_path', '/api/model/assets/data/vitonhd',
@@ -35,27 +29,12 @@ def generate_design():
35
  '--test_order', 'paired'
36
  ]
37
 
38
- try:
39
- result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
40
- print(f"Command output: {result.stdout}")
41
- print(f"Command errors: {result.stderr}")
42
- #image_path = os.path.join(output_dir, image_filename)
43
- image_path = output_dir#"/api/output/generato_paired_paired/images/03191_00.jpg"
44
- print("immagine creata")
45
- print(f"Directory Exists: {os.path.exists(output_dir)}")
46
- print(f"Can Write: {os.access(output_dir, os.W_OK)}")
47
-
48
- # Check if the image was generated
49
- if os.path.exists(image_path):
50
- print("IMMAGINE CREATA")
51
- return jsonify({"status": "success", "image_url": f"{output_dir}"})
52
- else:
53
- print("IMMAGINE NON CREATA")
54
- return jsonify({"status": "error", "message": "Image generation failed"}), 500
55
- except subprocess.CalledProcessError as e:
56
- return jsonify({"status": "error", "message": str(e)}), 500
57
 
58
- # Route to serve the generated image
59
  @app.route('/api/output/generato_paired_paired/images/<filename>')
60
  def serve_image(filename):
61
  return send_from_directory(output_dir, filename)
 
1
+ from flask import Flask, render_template, send_from_directory
 
2
  import subprocess
3
  import os
4
 
5
  app = Flask(__name__)
 
6
 
7
+ # Directory di output
 
 
 
 
8
  output_dir = '/api/output/generato_paired_paired/images'
9
  image_filename = '03191_00.jpg'
10
 
 
14
 
15
  @app.route('/generate-design', methods=['POST'])
16
  def generate_design():
17
+ # Esegui il comando di generazione
18
  command = [
19
  'python', '/api/model/src/eval.py',
20
  '--dataset_path', '/api/model/assets/data/vitonhd',
 
29
  '--test_order', 'paired'
30
  ]
31
 
32
+ # Esegui il comando senza gestire il JSON, lascia che l'errore venga stampato
33
+ subprocess.run(command, check=True)
34
+
35
+ return '', 204 # Nessun contenuto
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ # Route per servire l'immagine generata
38
  @app.route('/api/output/generato_paired_paired/images/<filename>')
39
  def serve_image(filename):
40
  return send_from_directory(output_dir, filename)
api/templates/index.html CHANGED
@@ -3,48 +3,42 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Generate Design</title>
7
  <script>
8
  function generateDesign() {
9
- // Remove the previous image, if any
10
- document.getElementById("generated-image").src = "";
11
-
12
- // Disable the button to prevent multiple requests
13
  const button = document.getElementById("generate-button");
14
  button.disabled = true;
15
  button.innerText = "Processing...";
16
 
17
- fetch('/generate-design', {
18
- method: 'POST'
19
- })
20
- .then(response => response.json())
21
- .then(data => {
22
- if (data.status === "success") {
23
- // Display the generated image
24
- const imgElement = document.getElementById("generated-image");
25
- imgElement.src = data.image_url;
26
- imgElement.style.display = "block";
27
- } else {
28
- alert("Error: " + data.message);
29
- }
30
  })
31
  .catch(error => {
32
  console.error("Error:", error);
33
- alert("Failed to execute the command.");
34
- })
35
- .finally(() => {
36
- // Re-enable the button
37
  button.disabled = false;
38
  button.innerText = "Run Command";
 
39
  });
40
  }
41
  </script>
42
  </head>
43
  <body>
44
  <h1>Generate Design</h1>
45
- <button id="generate-button" onclick="generateDesign()">Run Command</button>
46
 
47
- <!-- Container for displaying the generated image -->
48
- <img id="generated-image" style="display:none; margin-top: 20px;" alt="Generated Design" />
 
 
 
 
 
 
 
49
  </body>
50
  </html>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Generate and Download Design</title>
7
  <script>
8
  function generateDesign() {
9
+ // Disabilita il pulsante per evitare richieste multiple
 
 
 
10
  const button = document.getElementById("generate-button");
11
  button.disabled = true;
12
  button.innerText = "Processing...";
13
 
14
+ // Esegui la chiamata fetch per attivare il comando
15
+ fetch('/generate-design', { method: 'POST' })
16
+ .then(() => {
17
+ // Abilita il pulsante di download
18
+ document.getElementById("download-button").style.display = "block";
19
+ button.disabled = false;
20
+ button.innerText = "Run Command";
 
 
 
 
 
 
21
  })
22
  .catch(error => {
23
  console.error("Error:", error);
 
 
 
 
24
  button.disabled = false;
25
  button.innerText = "Run Command";
26
+ alert("Failed to execute the command.");
27
  });
28
  }
29
  </script>
30
  </head>
31
  <body>
32
  <h1>Generate Design</h1>
 
33
 
34
+ <!-- Bottone per attivare la generazione -->
35
+ <button id="generate-button" onclick="generateDesign()">Run Command</button>
36
+
37
+ <br><br>
38
+
39
+ <!-- Bottone per il download (nascosto fino a che non è pronto) -->
40
+ <a id="download-button" href="/api/output/generato_paired_paired/images/03191_00.jpg" download style="display:none;">
41
+ Download Image
42
+ </a>
43
  </body>
44
  </html>
api/templates/output.html ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Image Generated</title>
5
+ </head>
6
+ <body>
7
+ <h1>Design Generated Successfully</h1>
8
+ <img src="{{ image_url }}" alt="Generated Image" style="max-width: 100%; height: auto;">
9
+ <br>
10
+ <a href="{{ download_url }}" download>Download Image</a>
11
+ </body>
12
+ </html>