VINU NAYAK commited on
Commit
b6bab69
·
verified ·
1 Parent(s): e59885c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -2
app.py CHANGED
@@ -1,11 +1,13 @@
1
  from fastapi import FastAPI, File, UploadFile, HTTPException
2
- from fastapi.responses import StreamingResponse
3
  from fastapi.middleware.cors import CORSMiddleware
 
4
  import io
5
  from PIL import Image
6
  from rembg import remove
7
  import os
8
  import sys
 
9
 
10
  # Print debug information
11
  print("Python version:", sys.version)
@@ -23,9 +25,37 @@ app.add_middleware(
23
  allow_headers=["*"],
24
  )
25
 
 
 
 
 
 
 
 
26
  @app.get("/")
27
  def read_root():
28
- return {"message": "Background Removal API is running. Use POST /remove-bg to remove background from an image."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  @app.post("/remove-bg")
31
  async def remove_background(file: UploadFile = File(...)):
 
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
  from PIL import Image
7
  from rembg import remove
8
  import os
9
  import sys
10
+ import platform
11
 
12
  # Print debug information
13
  print("Python version:", sys.version)
 
25
  allow_headers=["*"],
26
  )
27
 
28
+ # Mount static files directory
29
+ if os.path.exists("static"):
30
+ app.mount("/static", StaticFiles(directory="static"), name="static")
31
+ print("Static directory mounted successfully")
32
+ else:
33
+ print("Warning: Static directory not found")
34
+
35
  @app.get("/")
36
  def read_root():
37
+ # Redirect to static HTML if it exists
38
+ if os.path.exists("static/index.html"):
39
+ return RedirectResponse(url="/static/index.html")
40
+ else:
41
+ return {"message": "Background Removal API is running. Use POST /remove-bg to remove background from an image."}
42
+
43
+ @app.get("/health")
44
+ def health_check():
45
+ """Health check endpoint to verify the API is running correctly"""
46
+ return {
47
+ "status": "ok",
48
+ "python_version": platform.python_version(),
49
+ "system": platform.system(),
50
+ "dependencies": {
51
+ "fastapi": "installed",
52
+ "pillow": "installed",
53
+ "rembg": "installed"
54
+ },
55
+ "directories": os.listdir(),
56
+ "static_exists": os.path.exists("static"),
57
+ "static_files": os.listdir("static") if os.path.exists("static") else []
58
+ }
59
 
60
  @app.post("/remove-bg")
61
  async def remove_background(file: UploadFile = File(...)):