Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 3 |
-
from fastapi.responses import StreamingResponse
|
| 4 |
from PIL import Image
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
|
@@ -9,6 +9,16 @@ from io import BytesIO
|
|
| 9 |
import requests
|
| 10 |
import uvicorn
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# -------------------------
|
| 13 |
# Model Setup
|
| 14 |
# -------------------------
|
|
@@ -82,7 +92,6 @@ async def remove_background(file: UploadFile = File(None), image_url: str = Form
|
|
| 82 |
raise HTTPException(status_code=400, detail="Provide either 'file' or 'image_url'.")
|
| 83 |
|
| 84 |
result = process_image(image)
|
| 85 |
-
|
| 86 |
buf = BytesIO()
|
| 87 |
result.save(buf, format="PNG")
|
| 88 |
buf.seek(0)
|
|
@@ -90,6 +99,85 @@ async def remove_background(file: UploadFile = File(None), image_url: str = Form
|
|
| 90 |
except Exception as e:
|
| 91 |
raise HTTPException(status_code=500, detail=str(e))
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# -------------------------
|
| 94 |
# Run App on Spaces
|
| 95 |
# -------------------------
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 3 |
+
from fastapi.responses import StreamingResponse, HTMLResponse
|
| 4 |
from PIL import Image
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
|
|
|
| 9 |
import requests
|
| 10 |
import uvicorn
|
| 11 |
|
| 12 |
+
# -------------------------
|
| 13 |
+
# Optional HEIC Support
|
| 14 |
+
# -------------------------
|
| 15 |
+
try:
|
| 16 |
+
import pillow_heif
|
| 17 |
+
pillow_heif.register_heif_opener()
|
| 18 |
+
print("✅ HEIC/HEIF format supported.")
|
| 19 |
+
except ImportError:
|
| 20 |
+
print("⚠️ Install pillow-heif for HEIC support: pip install pillow-heif")
|
| 21 |
+
|
| 22 |
# -------------------------
|
| 23 |
# Model Setup
|
| 24 |
# -------------------------
|
|
|
|
| 92 |
raise HTTPException(status_code=400, detail="Provide either 'file' or 'image_url'.")
|
| 93 |
|
| 94 |
result = process_image(image)
|
|
|
|
| 95 |
buf = BytesIO()
|
| 96 |
result.save(buf, format="PNG")
|
| 97 |
buf.seek(0)
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
raise HTTPException(status_code=500, detail=str(e))
|
| 101 |
|
| 102 |
+
# -------------------------
|
| 103 |
+
# Developer Test Page (Bootstrap)
|
| 104 |
+
# -------------------------
|
| 105 |
+
@app.get("/", response_class=HTMLResponse)
|
| 106 |
+
async def index():
|
| 107 |
+
html = """
|
| 108 |
+
<!DOCTYPE html>
|
| 109 |
+
<html lang="en">
|
| 110 |
+
<head>
|
| 111 |
+
<meta charset="UTF-8">
|
| 112 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 113 |
+
<title>Background Remover API Test</title>
|
| 114 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
| 115 |
+
<style>
|
| 116 |
+
body { background-color: #f8f9fa; padding-top: 40px; }
|
| 117 |
+
.container { max-width: 700px; }
|
| 118 |
+
img { max-width: 100%; margin-top: 20px; border-radius: 10px; }
|
| 119 |
+
</style>
|
| 120 |
+
</head>
|
| 121 |
+
<body>
|
| 122 |
+
<div class="container text-center">
|
| 123 |
+
<h2 class="mb-4">Background Remover API Tester</h2>
|
| 124 |
+
|
| 125 |
+
<form id="uploadForm" class="mb-4" enctype="multipart/form-data">
|
| 126 |
+
<div class="mb-3">
|
| 127 |
+
<label for="fileInput" class="form-label">Upload Image (any format, e.g. JPG, PNG, HEIC):</label>
|
| 128 |
+
<input class="form-control" type="file" id="fileInput" name="file" accept="image/*">
|
| 129 |
+
</div>
|
| 130 |
+
<button class="btn btn-primary" type="submit">Remove Background</button>
|
| 131 |
+
</form>
|
| 132 |
+
|
| 133 |
+
<div class="mb-4">OR</div>
|
| 134 |
+
|
| 135 |
+
<form id="urlForm" class="mb-4">
|
| 136 |
+
<div class="mb-3">
|
| 137 |
+
<label for="urlInput" class="form-label">Enter Image URL:</label>
|
| 138 |
+
<input class="form-control" type="text" id="urlInput" placeholder="https://example.com/image.jpg">
|
| 139 |
+
</div>
|
| 140 |
+
<button class="btn btn-success" type="submit">Remove Background</button>
|
| 141 |
+
</form>
|
| 142 |
+
|
| 143 |
+
<div id="resultContainer" class="mt-4">
|
| 144 |
+
<h5>Result:</h5>
|
| 145 |
+
<img id="resultImg" src="" alt="">
|
| 146 |
+
</div>
|
| 147 |
+
</div>
|
| 148 |
+
|
| 149 |
+
<script>
|
| 150 |
+
const uploadForm = document.getElementById("uploadForm");
|
| 151 |
+
const urlForm = document.getElementById("urlForm");
|
| 152 |
+
const resultImg = document.getElementById("resultImg");
|
| 153 |
+
|
| 154 |
+
uploadForm.addEventListener("submit", async e => {
|
| 155 |
+
e.preventDefault();
|
| 156 |
+
const fileInput = document.getElementById("fileInput");
|
| 157 |
+
if (!fileInput.files.length) return alert("Please select a file!");
|
| 158 |
+
const formData = new FormData();
|
| 159 |
+
formData.append("file", fileInput.files[0]);
|
| 160 |
+
const res = await fetch("/remove-background", { method: "POST", body: formData });
|
| 161 |
+
const blob = await res.blob();
|
| 162 |
+
resultImg.src = URL.createObjectURL(blob);
|
| 163 |
+
});
|
| 164 |
+
|
| 165 |
+
urlForm.addEventListener("submit", async e => {
|
| 166 |
+
e.preventDefault();
|
| 167 |
+
const url = document.getElementById("urlInput").value.trim();
|
| 168 |
+
if (!url) return alert("Please enter an image URL!");
|
| 169 |
+
const formData = new FormData();
|
| 170 |
+
formData.append("image_url", url);
|
| 171 |
+
const res = await fetch("/remove-background", { method: "POST", body: formData });
|
| 172 |
+
const blob = await res.blob();
|
| 173 |
+
resultImg.src = URL.createObjectURL(blob);
|
| 174 |
+
});
|
| 175 |
+
</script>
|
| 176 |
+
</body>
|
| 177 |
+
</html>
|
| 178 |
+
"""
|
| 179 |
+
return HTMLResponse(html)
|
| 180 |
+
|
| 181 |
# -------------------------
|
| 182 |
# Run App on Spaces
|
| 183 |
# -------------------------
|