Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,100 +1,71 @@
|
|
1 |
-
from
|
2 |
-
from quart_cors import cors
|
3 |
from gradio_client import Client, file
|
|
|
4 |
import os
|
5 |
import traceback
|
6 |
import shutil
|
7 |
import base64
|
8 |
import asyncio
|
9 |
-
import aiofiles
|
10 |
|
11 |
-
app =
|
12 |
-
|
13 |
|
14 |
client = Client("kadirnar/IDM-VTON")
|
15 |
|
16 |
-
# Directory to save uploaded and processed files
|
17 |
UPLOAD_FOLDER = 'static/uploads'
|
18 |
RESULT_FOLDER = 'static/results'
|
19 |
-
|
20 |
-
|
21 |
-
if not os.path.exists(RESULT_FOLDER):
|
22 |
-
os.makedirs(RESULT_FOLDER)
|
23 |
-
|
24 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
25 |
app.config['RESULT_FOLDER'] = RESULT_FOLDER
|
26 |
|
27 |
-
@app.route('/')
|
28 |
-
async def home():
|
29 |
-
return {"wearon": "wearon model is running!"}
|
30 |
-
|
31 |
@app.route('/process', methods=['POST'])
|
32 |
async def predict():
|
33 |
try:
|
34 |
-
|
35 |
-
|
36 |
-
product_image_url = form.get('product_image_url')
|
37 |
-
|
38 |
-
# Handle the uploaded model image
|
39 |
if 'model_image' not in request.files:
|
40 |
return jsonify(error='No model image file provided'), 400
|
41 |
-
|
42 |
-
model_image =
|
43 |
if model_image.filename == '':
|
44 |
return jsonify(error='No selected file'), 400
|
45 |
-
|
46 |
-
# Save the uploaded file to the upload directory
|
47 |
filename = os.path.join(app.config['UPLOAD_FOLDER'], model_image.filename)
|
48 |
-
|
49 |
-
|
50 |
base_path = os.getcwd()
|
51 |
full_filename = os.path.normpath(os.path.join(base_path, filename))
|
52 |
-
|
53 |
-
print("Product image =", product_image_url)
|
54 |
-
print("Model image =", full_filename)
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
garment_des="Hello!!",
|
62 |
-
is_checked=True,
|
63 |
-
is_checked_crop=False,
|
64 |
-
denoise_steps=30,
|
65 |
-
seed=42,
|
66 |
-
api_name="/tryon"
|
67 |
-
)
|
68 |
-
except Exception as e:
|
69 |
-
traceback.print_exc()
|
70 |
-
raise
|
71 |
-
|
72 |
print(result)
|
73 |
-
# Extract the path of the first output image
|
74 |
output_image_path = result[0]
|
75 |
-
|
76 |
-
# Copy the output image to the RESULT_FOLDER
|
77 |
output_image_filename = os.path.basename(output_image_path)
|
78 |
local_output_path = os.path.join(app.config['RESULT_FOLDER'], output_image_filename)
|
79 |
-
|
80 |
-
|
81 |
-
# Remove the uploaded file after processing
|
82 |
os.remove(filename)
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
# Return the output image in JSON format
|
89 |
return jsonify(image=encoded_image), 200
|
90 |
-
|
91 |
except Exception as e:
|
92 |
traceback.print_exc()
|
93 |
return jsonify(error=str(e)), 500
|
94 |
|
95 |
@app.route('/uploads/<filename>')
|
96 |
-
|
97 |
-
return
|
98 |
|
99 |
if __name__ == '__main__':
|
100 |
app.run(host='0.0.0.0', port=5000)
|
|
|
1 |
+
from flask import Flask, request, jsonify, send_from_directory
|
|
|
2 |
from gradio_client import Client, file
|
3 |
+
from flask_cors import CORS
|
4 |
import os
|
5 |
import traceback
|
6 |
import shutil
|
7 |
import base64
|
8 |
import asyncio
|
|
|
9 |
|
10 |
+
app = Flask(__name__)
|
11 |
+
CORS(app)
|
12 |
|
13 |
client = Client("kadirnar/IDM-VTON")
|
14 |
|
|
|
15 |
UPLOAD_FOLDER = 'static/uploads'
|
16 |
RESULT_FOLDER = 'static/results'
|
17 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
18 |
+
os.makedirs(RESULT_FOLDER, exist_ok=True)
|
|
|
|
|
|
|
19 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
20 |
app.config['RESULT_FOLDER'] = RESULT_FOLDER
|
21 |
|
|
|
|
|
|
|
|
|
22 |
@app.route('/process', methods=['POST'])
|
23 |
async def predict():
|
24 |
try:
|
25 |
+
product_image_url = request.form.get('product_image_url')
|
26 |
+
|
|
|
|
|
|
|
27 |
if 'model_image' not in request.files:
|
28 |
return jsonify(error='No model image file provided'), 400
|
29 |
+
|
30 |
+
model_image = request.files['model_image']
|
31 |
if model_image.filename == '':
|
32 |
return jsonify(error='No selected file'), 400
|
33 |
+
|
|
|
34 |
filename = os.path.join(app.config['UPLOAD_FOLDER'], model_image.filename)
|
35 |
+
model_image.save(filename)
|
36 |
+
|
37 |
base_path = os.getcwd()
|
38 |
full_filename = os.path.normpath(os.path.join(base_path, filename))
|
39 |
+
|
40 |
+
print("Product image = ", product_image_url)
|
41 |
+
print("Model image = ", full_filename)
|
42 |
+
|
43 |
+
loop = asyncio.get_event_loop()
|
44 |
+
result = await loop.run_in_executor(None, client.predict, {
|
45 |
+
"background": file(full_filename), "layers": [], "composite": None
|
46 |
+
}, file(product_image_url), "Hello!!", True, False, 30, 42, "/tryon")
|
47 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
print(result)
|
|
|
49 |
output_image_path = result[0]
|
50 |
+
|
|
|
51 |
output_image_filename = os.path.basename(output_image_path)
|
52 |
local_output_path = os.path.join(app.config['RESULT_FOLDER'], output_image_filename)
|
53 |
+
shutil.copy(output_image_path, local_output_path)
|
54 |
+
|
|
|
55 |
os.remove(filename)
|
56 |
+
|
57 |
+
with open(local_output_path, "rb") as image_file:
|
58 |
+
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
|
59 |
+
|
|
|
|
|
60 |
return jsonify(image=encoded_image), 200
|
61 |
+
|
62 |
except Exception as e:
|
63 |
traceback.print_exc()
|
64 |
return jsonify(error=str(e)), 500
|
65 |
|
66 |
@app.route('/uploads/<filename>')
|
67 |
+
def uploaded_file(filename):
|
68 |
+
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
69 |
|
70 |
if __name__ == '__main__':
|
71 |
app.run(host='0.0.0.0', port=5000)
|