Spaces:
Paused
Paused
VINU NAYAK commited on
Delete main.py
Browse files
main.py
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
# Disable Numba caching to avoid the error
|
| 3 |
-
os.environ["NUMBA_DISABLE_JIT"] = "1"
|
| 4 |
-
|
| 5 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 6 |
-
from fastapi.responses import StreamingResponse, FileResponse, RedirectResponse
|
| 7 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
-
from fastapi.staticfiles import StaticFiles
|
| 9 |
-
import io
|
| 10 |
-
import sys
|
| 11 |
-
from PIL import Image
|
| 12 |
-
from rembg import remove
|
| 13 |
-
|
| 14 |
-
# Print debug information
|
| 15 |
-
print("Python version:", sys.version)
|
| 16 |
-
print("Current directory:", os.getcwd())
|
| 17 |
-
print("Directory contents:", os.listdir())
|
| 18 |
-
|
| 19 |
-
# Create the FastAPI app
|
| 20 |
-
app = FastAPI()
|
| 21 |
-
|
| 22 |
-
# Add CORS middleware
|
| 23 |
-
app.add_middleware(
|
| 24 |
-
CORSMiddleware,
|
| 25 |
-
allow_origins=["*"],
|
| 26 |
-
allow_credentials=True,
|
| 27 |
-
allow_methods=["*"],
|
| 28 |
-
allow_headers=["*"],
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# Mount static files directory if it exists
|
| 32 |
-
if os.path.exists("static"):
|
| 33 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 34 |
-
print("Static directory mounted successfully")
|
| 35 |
-
else:
|
| 36 |
-
print("Warning: Static directory not found")
|
| 37 |
-
|
| 38 |
-
@app.get("/")
|
| 39 |
-
def read_root():
|
| 40 |
-
"""Root endpoint that redirects to the static HTML or returns a status message"""
|
| 41 |
-
if os.path.exists("static/index.html"):
|
| 42 |
-
return RedirectResponse(url="/static/index.html")
|
| 43 |
-
else:
|
| 44 |
-
return {"message": "Background Removal API is running. Use POST /remove-bg to remove background from an image."}
|
| 45 |
-
|
| 46 |
-
@app.get("/health")
|
| 47 |
-
def health_check():
|
| 48 |
-
"""Health check endpoint to verify the API is running correctly"""
|
| 49 |
-
return {
|
| 50 |
-
"status": "ok",
|
| 51 |
-
"environment": {
|
| 52 |
-
"python_version": sys.version,
|
| 53 |
-
"directory": os.getcwd(),
|
| 54 |
-
"files": os.listdir(),
|
| 55 |
-
"numba_disabled": os.environ.get("NUMBA_DISABLE_JIT", "Not set")
|
| 56 |
-
}
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
@app.post("/remove-bg")
|
| 60 |
-
async def remove_background(file: UploadFile = File(...)):
|
| 61 |
-
"""Remove the background from an uploaded image"""
|
| 62 |
-
# Check if the file is an image
|
| 63 |
-
if not file.content_type.startswith("image/"):
|
| 64 |
-
raise HTTPException(status_code=400, detail="File must be an image")
|
| 65 |
-
|
| 66 |
-
try:
|
| 67 |
-
# Read the image
|
| 68 |
-
contents = await file.read()
|
| 69 |
-
input_image = Image.open(io.BytesIO(contents))
|
| 70 |
-
|
| 71 |
-
# Remove the background
|
| 72 |
-
output_image = remove(input_image)
|
| 73 |
-
|
| 74 |
-
# Convert to bytes
|
| 75 |
-
img_byte_arr = io.BytesIO()
|
| 76 |
-
output_image.save(img_byte_arr, format="PNG")
|
| 77 |
-
img_byte_arr.seek(0)
|
| 78 |
-
|
| 79 |
-
# Return the processed image
|
| 80 |
-
return StreamingResponse(
|
| 81 |
-
content=img_byte_arr,
|
| 82 |
-
media_type="image/png"
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
except Exception as e:
|
| 86 |
-
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 87 |
-
|
| 88 |
-
if __name__ == "__main__":
|
| 89 |
-
import uvicorn
|
| 90 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|