Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
from fastapi import FastAPI, UploadFile, Form, File, HTTPException
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from gradio_client import Client, file
|
|
|
5 |
import os
|
6 |
import shutil
|
7 |
import base64
|
@@ -29,8 +30,7 @@ os.makedirs(RESULT_FOLDER, exist_ok=True)
|
|
29 |
|
30 |
@app.post("/")
|
31 |
async def hello():
|
32 |
-
return {"Wearon":"wearon model is running"}
|
33 |
-
|
34 |
|
35 |
@app.post("/process")
|
36 |
async def predict(product_image_url: str = Form(...), model_image: UploadFile = File(...)):
|
@@ -40,18 +40,19 @@ async def predict(product_image_url: str = Form(...), model_image: UploadFile =
|
|
40 |
|
41 |
# Save the uploaded file to the upload directory
|
42 |
filename = os.path.join(UPLOAD_FOLDER, model_image.filename)
|
43 |
-
with open(filename, "wb") as buffer:
|
44 |
-
|
|
|
45 |
|
46 |
base_path = os.getcwd()
|
47 |
full_filename = os.path.normpath(os.path.join(base_path, filename))
|
48 |
|
49 |
-
print("Product image =
|
50 |
-
print("Model image =
|
51 |
|
52 |
# Perform prediction
|
53 |
try:
|
54 |
-
result =
|
55 |
dict={"background": file(full_filename), "layers": [], "composite": None},
|
56 |
garm_img=file(product_image_url),
|
57 |
garment_des="Hello!!",
|
@@ -78,8 +79,8 @@ async def predict(product_image_url: str = Form(...), model_image: UploadFile =
|
|
78 |
os.remove(filename)
|
79 |
|
80 |
# Encode the output image in base64
|
81 |
-
with open(local_output_path, "rb") as image_file:
|
82 |
-
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
|
83 |
|
84 |
# Return the output image in JSON format
|
85 |
return JSONResponse(content={"image": encoded_image}, status_code=200)
|
@@ -94,4 +95,4 @@ async def uploaded_file(filename: str):
|
|
94 |
if os.path.exists(file_path):
|
95 |
return FileResponse(file_path)
|
96 |
else:
|
97 |
-
raise HTTPException(status_code=404, detail="File not found")
|
|
|
1 |
from fastapi import FastAPI, UploadFile, Form, File, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse, FileResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
from gradio_client import Client, file
|
5 |
+
import aiofiles
|
6 |
import os
|
7 |
import shutil
|
8 |
import base64
|
|
|
30 |
|
31 |
@app.post("/")
|
32 |
async def hello():
|
33 |
+
return {"Wearon": "wearon model is running"}
|
|
|
34 |
|
35 |
@app.post("/process")
|
36 |
async def predict(product_image_url: str = Form(...), model_image: UploadFile = File(...)):
|
|
|
40 |
|
41 |
# Save the uploaded file to the upload directory
|
42 |
filename = os.path.join(UPLOAD_FOLDER, model_image.filename)
|
43 |
+
async with aiofiles.open(filename, "wb") as buffer:
|
44 |
+
content = await model_image.read()
|
45 |
+
await buffer.write(content)
|
46 |
|
47 |
base_path = os.getcwd()
|
48 |
full_filename = os.path.normpath(os.path.join(base_path, filename))
|
49 |
|
50 |
+
print("Product image =", product_image_url)
|
51 |
+
print("Model image =", full_filename)
|
52 |
|
53 |
# Perform prediction
|
54 |
try:
|
55 |
+
result = client.predict(
|
56 |
dict={"background": file(full_filename), "layers": [], "composite": None},
|
57 |
garm_img=file(product_image_url),
|
58 |
garment_des="Hello!!",
|
|
|
79 |
os.remove(filename)
|
80 |
|
81 |
# Encode the output image in base64
|
82 |
+
async with aiofiles.open(local_output_path, "rb") as image_file:
|
83 |
+
encoded_image = base64.b64encode(await image_file.read()).decode('utf-8')
|
84 |
|
85 |
# Return the output image in JSON format
|
86 |
return JSONResponse(content={"image": encoded_image}, status_code=200)
|
|
|
95 |
if os.path.exists(file_path):
|
96 |
return FileResponse(file_path)
|
97 |
else:
|
98 |
+
raise HTTPException(status_code=404, detail="File not found")
|