Spaces:
Runtime error
Runtime error
Sangare Adama
commited on
Commit
·
4417555
1
Parent(s):
3868182
refresh files
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from fastapi import FastAPI, UploadFile, File
|
2 |
from fastapi.responses import Response
|
|
|
3 |
import shutil
|
4 |
import uuid
|
5 |
import os
|
@@ -13,26 +14,27 @@ modnet_path = model_manager.get_model_path("modnet")
|
|
13 |
processor = create_background_remover(model_type="modnet", model_path=modnet_path)
|
14 |
|
15 |
@app.post("/remove-bg/")
|
16 |
-
async def
|
17 |
-
|
18 |
-
|
19 |
-
)
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
try:
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
# Sauvegarder en mémoire
|
28 |
-
img_byte_arr = io.BytesIO()
|
29 |
-
result.save(img_byte_arr, format='PNG')
|
30 |
-
img_byte_arr.seek(0)
|
31 |
-
|
32 |
-
return Response(content=img_byte_arr.getvalue(), media_type="image/png")
|
33 |
except Exception as e:
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
# Ajouter cette partie à la fin de votre app.py existant
|
37 |
if __name__ == "__main__":
|
38 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
1 |
from fastapi import FastAPI, UploadFile, File
|
2 |
from fastapi.responses import Response
|
3 |
+
import uvicorn
|
4 |
import shutil
|
5 |
import uuid
|
6 |
import os
|
|
|
14 |
processor = create_background_remover(model_type="modnet", model_path=modnet_path)
|
15 |
|
16 |
@app.post("/remove-bg/")
|
17 |
+
async def remove_bg(file: UploadFile = File(...)):
|
18 |
+
# Sauvegarde temporaire
|
19 |
+
input_path = f"/tmp/{uuid.uuid4()}.png"
|
20 |
+
output_path = f"/tmp/{uuid.uuid4()}_output.png"
|
21 |
+
|
22 |
+
with open(input_path, "wb") as f:
|
23 |
+
shutil.copyfileobj(file.file, f)
|
24 |
+
|
25 |
+
# Traitement de l'image
|
26 |
try:
|
27 |
+
processor.remove_background(image_path=input_path, output_path=output_path)
|
28 |
+
with open(output_path, "rb") as out_file:
|
29 |
+
return Response(content=out_file.read(), media_type="image/png")
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
except Exception as e:
|
31 |
+
return {"error": str(e)}
|
32 |
+
finally:
|
33 |
+
# Nettoyage
|
34 |
+
if os.path.exists(input_path):
|
35 |
+
os.remove(input_path)
|
36 |
+
if os.path.exists(output_path):
|
37 |
+
os.remove(output_path)
|
38 |
|
|
|
39 |
if __name__ == "__main__":
|
40 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|