VINU NAYAK commited on
Commit
28eb2ef
·
verified ·
1 Parent(s): 9841475

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -88
app.py CHANGED
@@ -1,91 +1,4 @@
1
  # Import app from main.py for backward compatibility
2
  from main import app
3
 
4
- # This file ensures compatibility with both app:app and main:app entrypoints
5
-
6
- from fastapi import FastAPI, File, UploadFile, HTTPException
7
- from fastapi.responses import StreamingResponse, FileResponse, RedirectResponse
8
- from fastapi.middleware.cors import CORSMiddleware
9
- from fastapi.staticfiles import StaticFiles
10
- import io
11
- from PIL import Image
12
- from rembg import remove
13
- import os
14
- import sys
15
- import platform
16
-
17
- # Print debug information
18
- print("Python version:", sys.version)
19
- print("Current directory:", os.getcwd())
20
- print("Directory contents:", os.listdir())
21
-
22
- app = FastAPI(title="Background Removal API")
23
-
24
- # Add CORS middleware to allow requests from any origin
25
- app.add_middleware(
26
- CORSMiddleware,
27
- allow_origins=["*"],
28
- allow_credentials=True,
29
- allow_methods=["*"],
30
- allow_headers=["*"],
31
- )
32
-
33
- # Mount static files directory
34
- if os.path.exists("static"):
35
- app.mount("/static", StaticFiles(directory="static"), name="static")
36
- print("Static directory mounted successfully")
37
- else:
38
- print("Warning: Static directory not found")
39
-
40
- @app.get("/")
41
- def read_root():
42
- # Redirect to static HTML if it exists
43
- if os.path.exists("static/index.html"):
44
- return RedirectResponse(url="/static/index.html")
45
- else:
46
- return {"message": "Background Removal API is running. Use POST /remove-bg to remove background from an image."}
47
-
48
- @app.get("/health")
49
- def health_check():
50
- """Health check endpoint to verify the API is running correctly"""
51
- return {
52
- "status": "ok",
53
- "python_version": platform.python_version(),
54
- "system": platform.system(),
55
- "dependencies": {
56
- "fastapi": "installed",
57
- "pillow": "installed",
58
- "rembg": "installed"
59
- },
60
- "directories": os.listdir(),
61
- "static_exists": os.path.exists("static"),
62
- "static_files": os.listdir("static") if os.path.exists("static") else []
63
- }
64
-
65
- @app.post("/remove-bg")
66
- async def remove_background(file: UploadFile = File(...)):
67
- # Check if the file is an image
68
- if not file.content_type.startswith("image/"):
69
- raise HTTPException(status_code=400, detail="File must be an image")
70
-
71
- try:
72
- # Read the image
73
- contents = await file.read()
74
- input_image = Image.open(io.BytesIO(contents))
75
-
76
- # Remove the background
77
- output_image = remove(input_image)
78
-
79
- # Convert to bytes
80
- img_byte_arr = io.BytesIO()
81
- output_image.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)}")
 
1
  # Import app from main.py for backward compatibility
2
  from main import app
3
 
4
+ # This file ensures compatibility with both app:app and main:app entrypoints