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