Spaces:
Paused
Paused
giorgio-caparvi
commited on
Commit
·
554a6f2
1
Parent(s):
ce8fac2
added button to trigger image generation
Browse files- Dockerfile +4 -3
- api/app.py +30 -5
- api/templates/index.html +50 -0
Dockerfile
CHANGED
|
@@ -15,8 +15,9 @@ WORKDIR /api
|
|
| 15 |
COPY --chown=user api/environment.yml ./
|
| 16 |
RUN conda env create -f environment.yml && conda clean -a -y
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
# Copy the backend application code
|
| 22 |
COPY --chown=user api/ .
|
|
@@ -25,4 +26,4 @@ COPY --chown=user api/ .
|
|
| 25 |
EXPOSE 7860
|
| 26 |
|
| 27 |
# Set default command to run the backend model using the specified port
|
| 28 |
-
CMD ["
|
|
|
|
| 15 |
COPY --chown=user api/environment.yml ./
|
| 16 |
RUN conda env create -f environment.yml && conda clean -a -y
|
| 17 |
|
| 18 |
+
# Activate the conda environment explicitly in the same RUN layer
|
| 19 |
+
RUN echo "conda activate mgd" >> ~/.bashrc
|
| 20 |
+
SHELL ["/bin/bash", "--login", "-c"]
|
| 21 |
|
| 22 |
# Copy the backend application code
|
| 23 |
COPY --chown=user api/ .
|
|
|
|
| 26 |
EXPOSE 7860
|
| 27 |
|
| 28 |
# Set default command to run the backend model using the specified port
|
| 29 |
+
CMD ["bash", "-c", "source ~/.bashrc && conda activate mgd && python app.py --port 7860"]
|
api/app.py
CHANGED
|
@@ -1,19 +1,31 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
import subprocess
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
CORS(app)
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def generate_design():
|
| 10 |
-
#
|
| 11 |
command = [
|
| 12 |
'python', 'app/model/src/eval.py',
|
| 13 |
'--dataset_path', './assets/data/vitonhd',
|
| 14 |
'--batch_size', '1',
|
| 15 |
'--mixed_precision', 'fp16',
|
| 16 |
-
'--output_dir',
|
| 17 |
'--save_name', 'generato_paired',
|
| 18 |
'--num_workers_test', '4',
|
| 19 |
'--sketch_cond_rate', '0.2',
|
|
@@ -24,9 +36,22 @@ def generate_design():
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
subprocess.run(command, check=True)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
except subprocess.CalledProcessError as e:
|
| 29 |
return jsonify({"status": "error", "message": str(e)}), 500
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if __name__ == '__main__':
|
| 32 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 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 |
+
# Set the directory for the output images
|
| 13 |
+
output_dir = 'api/output'
|
| 14 |
+
image_filename = 'generato_paired.png'
|
| 15 |
+
|
| 16 |
+
@app.route('/')
|
| 17 |
+
def index():
|
| 18 |
+
return render_template('index.html')
|
| 19 |
+
|
| 20 |
+
@app.route('/generate-design', methods=['POST'])
|
| 21 |
def generate_design():
|
| 22 |
+
# Command handling
|
| 23 |
command = [
|
| 24 |
'python', 'app/model/src/eval.py',
|
| 25 |
'--dataset_path', './assets/data/vitonhd',
|
| 26 |
'--batch_size', '1',
|
| 27 |
'--mixed_precision', 'fp16',
|
| 28 |
+
'--output_dir', output_dir,
|
| 29 |
'--save_name', 'generato_paired',
|
| 30 |
'--num_workers_test', '4',
|
| 31 |
'--sketch_cond_rate', '0.2',
|
|
|
|
| 36 |
|
| 37 |
try:
|
| 38 |
subprocess.run(command, check=True)
|
| 39 |
+
image_path = os.path.join(output_dir, image_filename)
|
| 40 |
+
|
| 41 |
+
# Check if the image was generated
|
| 42 |
+
if os.path.exists(image_path):
|
| 43 |
+
print("IMMAGINE CREATA")
|
| 44 |
+
return jsonify({"status": "success", "image_url": f"/output/{image_filename}"})
|
| 45 |
+
else:
|
| 46 |
+
print("IMMAGINE NON CREATA")
|
| 47 |
+
return jsonify({"status": "error", "message": "Image generation failed"}), 500
|
| 48 |
except subprocess.CalledProcessError as e:
|
| 49 |
return jsonify({"status": "error", "message": str(e)}), 500
|
| 50 |
|
| 51 |
+
# Route to serve the generated image
|
| 52 |
+
@app.route('/output/<filename>')
|
| 53 |
+
def serve_image(filename):
|
| 54 |
+
return send_from_directory(output_dir, filename)
|
| 55 |
+
|
| 56 |
if __name__ == '__main__':
|
| 57 |
app.run(host='0.0.0.0', port=7860)
|
api/templates/index.html
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 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>
|