Spaces:
Build error
Build error
Gunter
commited on
Commit
•
8403882
1
Parent(s):
24f50ed
Add application file
Browse files- Dockerfile +14 -0
- main.py +68 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File,BackgroundTasks
|
2 |
+
from rembg import remove
|
3 |
+
|
4 |
+
from fastapi.responses import FileResponse
|
5 |
+
import os
|
6 |
+
from starlette.middleware.cors import CORSMiddleware
|
7 |
+
# from fastapi.staticfiles import StaticFiles
|
8 |
+
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
origins = [
|
16 |
+
"http://localhost:3000",
|
17 |
+
]
|
18 |
+
|
19 |
+
app.add_middleware(
|
20 |
+
CORSMiddleware,
|
21 |
+
allow_origins=origins,
|
22 |
+
allow_credentials=True,
|
23 |
+
allow_methods=["*"],
|
24 |
+
allow_headers=["*"],
|
25 |
+
)
|
26 |
+
|
27 |
+
# Directory to store processed images
|
28 |
+
output_dir = "output"
|
29 |
+
os.makedirs(output_dir, exist_ok=True)
|
30 |
+
# app.mount("/processed_images", StaticFiles(directory=output_dir), name="processed_images")
|
31 |
+
|
32 |
+
@app.post("/remove-background/")
|
33 |
+
async def remove_background(file: UploadFile):
|
34 |
+
# Ensure the uploaded file is an image (you can add more validation)
|
35 |
+
if not file.content_type.startswith("image/"):
|
36 |
+
return {"error": "Invalid file format"}
|
37 |
+
|
38 |
+
# Read the uploaded image file
|
39 |
+
image_bytes = await file.read()
|
40 |
+
|
41 |
+
# Process the image to remove the background using Rembg
|
42 |
+
output_image_bytes = remove(image_bytes)
|
43 |
+
|
44 |
+
# Save the processed image to a file
|
45 |
+
output_filename = f"{output_dir}/{file.filename}"
|
46 |
+
|
47 |
+
# output_filename = f"processed_images/{file.filename.split('/')[-1]}"
|
48 |
+
with open(output_filename, "wb") as output_file:
|
49 |
+
output_file.write(output_image_bytes)
|
50 |
+
|
51 |
+
return {"result": "Background removed successfully", "output_filename": output_filename}
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
@app.get("/download/{filename}")
|
56 |
+
async def download_processed_image(filename: str):
|
57 |
+
file_path = os.path.join(output_dir, filename)
|
58 |
+
# file_path = f"/output/{filename}"
|
59 |
+
if not os.path.exists(file_path):
|
60 |
+
return {"error": "File not found"}
|
61 |
+
|
62 |
+
return FileResponse(file_path)
|
63 |
+
|
64 |
+
|
65 |
+
@app.get("/delete")
|
66 |
+
def deleteAll():
|
67 |
+
os.rmdir("output")
|
68 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
rembg
|
2 |
+
fastapi
|
3 |
+
os
|
4 |
+
uvicorn
|