Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,14 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 6 |
from rembg import remove
|
| 7 |
import uvicorn
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# --------------------------
|
| 12 |
# Enable CORS
|
|
@@ -29,7 +36,9 @@ async def root():
|
|
| 29 |
"endpoints": {
|
| 30 |
"POST /upload": "Upload an image file for background removal",
|
| 31 |
"GET /json?url=": "Provide an image URL for background removal"
|
| 32 |
-
}
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
# --------------------------
|
|
@@ -41,20 +50,18 @@ def process_image(img_bytes: bytes):
|
|
| 41 |
output_bytes = remove(img_bytes)
|
| 42 |
|
| 43 |
# Upload to ar-hosting
|
| 44 |
-
files = {"file": ("
|
| 45 |
r = requests.post("https://ar-hosting.pages.dev/upload", files=files, timeout=20)
|
| 46 |
r.raise_for_status()
|
| 47 |
data = r.json()
|
| 48 |
|
| 49 |
if "url" in data:
|
| 50 |
-
|
| 51 |
"status": "success",
|
| 52 |
"url": data["url"],
|
| 53 |
"creator": "Jerrycoder",
|
| 54 |
"telegram": "Oggy_workshop"
|
| 55 |
}
|
| 56 |
-
# Pretty print JSON response
|
| 57 |
-
return json.loads(json.dumps(response, indent=4))
|
| 58 |
return {"status": "error", "message": "Upload failed"}
|
| 59 |
except Exception as e:
|
| 60 |
return {"status": "error", "message": str(e)}
|
|
@@ -65,8 +72,7 @@ def process_image(img_bytes: bytes):
|
|
| 65 |
@app.post("/upload")
|
| 66 |
async def upload_file(file: UploadFile = File(...)):
|
| 67 |
img_bytes = await file.read()
|
| 68 |
-
|
| 69 |
-
return JSONResponse(content=result)
|
| 70 |
|
| 71 |
# --------------------------
|
| 72 |
# GET: From image URL
|
|
@@ -83,10 +89,9 @@ async def from_url(url: str = Query(..., description="Image URL")):
|
|
| 83 |
}
|
| 84 |
resp = requests.get(url, headers=headers, timeout=15)
|
| 85 |
resp.raise_for_status()
|
| 86 |
-
|
| 87 |
-
return JSONResponse(content=result)
|
| 88 |
except Exception as e:
|
| 89 |
-
return
|
| 90 |
|
| 91 |
# --------------------------
|
| 92 |
# HF Entry point
|
|
|
|
| 6 |
from rembg import remove
|
| 7 |
import uvicorn
|
| 8 |
|
| 9 |
+
# --------------------------
|
| 10 |
+
# Custom Pretty JSON Response
|
| 11 |
+
# --------------------------
|
| 12 |
+
class PrettyJSONResponse(JSONResponse):
|
| 13 |
+
def render(self, content) -> bytes:
|
| 14 |
+
return json.dumps(content, indent=4, ensure_ascii=False).encode("utf-8")
|
| 15 |
+
|
| 16 |
+
app = FastAPI(title="AI ClearCut - Background Remover | JerryCoder", default_response_class=PrettyJSONResponse)
|
| 17 |
|
| 18 |
# --------------------------
|
| 19 |
# Enable CORS
|
|
|
|
| 36 |
"endpoints": {
|
| 37 |
"POST /upload": "Upload an image file for background removal",
|
| 38 |
"GET /json?url=": "Provide an image URL for background removal"
|
| 39 |
+
},
|
| 40 |
+
"creator": "Jerrycoder",
|
| 41 |
+
"telegram": "Oggy_workshop"
|
| 42 |
}
|
| 43 |
|
| 44 |
# --------------------------
|
|
|
|
| 50 |
output_bytes = remove(img_bytes)
|
| 51 |
|
| 52 |
# Upload to ar-hosting
|
| 53 |
+
files = {"file": ("output.png", output_bytes, "image/png")}
|
| 54 |
r = requests.post("https://ar-hosting.pages.dev/upload", files=files, timeout=20)
|
| 55 |
r.raise_for_status()
|
| 56 |
data = r.json()
|
| 57 |
|
| 58 |
if "url" in data:
|
| 59 |
+
return {
|
| 60 |
"status": "success",
|
| 61 |
"url": data["url"],
|
| 62 |
"creator": "Jerrycoder",
|
| 63 |
"telegram": "Oggy_workshop"
|
| 64 |
}
|
|
|
|
|
|
|
| 65 |
return {"status": "error", "message": "Upload failed"}
|
| 66 |
except Exception as e:
|
| 67 |
return {"status": "error", "message": str(e)}
|
|
|
|
| 72 |
@app.post("/upload")
|
| 73 |
async def upload_file(file: UploadFile = File(...)):
|
| 74 |
img_bytes = await file.read()
|
| 75 |
+
return process_image(img_bytes)
|
|
|
|
| 76 |
|
| 77 |
# --------------------------
|
| 78 |
# GET: From image URL
|
|
|
|
| 89 |
}
|
| 90 |
resp = requests.get(url, headers=headers, timeout=15)
|
| 91 |
resp.raise_for_status()
|
| 92 |
+
return process_image(resp.content)
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
+
return {"status": "error", "message": str(e)}
|
| 95 |
|
| 96 |
# --------------------------
|
| 97 |
# HF Entry point
|