Spaces:
Build error
Build error
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
load_dotenv()
|
| 4 |
+
from fastapi import FastAPI, UploadFile, File
|
| 5 |
+
from fastapi.staticfiles import StaticFiles
|
| 6 |
+
from typing import List
|
| 7 |
+
from processor.pipeline import run_card_pipeline
|
| 8 |
+
from processor.zipper import standardize_and_zip
|
| 9 |
+
from fastapi.responses import FileResponse, JSONResponse, Response
|
| 10 |
+
|
| 11 |
+
app = FastAPI(title="CardProcessor-Pro")
|
| 12 |
+
|
| 13 |
+
# Serve static files for the UI
|
| 14 |
+
os.makedirs("static", exist_ok=True)
|
| 15 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def serve_index():
|
| 19 |
+
return FileResponse("static/index.html")
|
| 20 |
+
|
| 21 |
+
@app.post("/api/process")
|
| 22 |
+
async def process_images(files: List[UploadFile] = File(...)):
|
| 23 |
+
"""
|
| 24 |
+
Receives multiple images, processes them (bg removal, crop, upscale, redact),
|
| 25 |
+
and returns a ZIP file containing the processed outputs.
|
| 26 |
+
"""
|
| 27 |
+
processed_images = []
|
| 28 |
+
|
| 29 |
+
for file in files:
|
| 30 |
+
try:
|
| 31 |
+
img_bytes = await file.read()
|
| 32 |
+
processed_img = run_card_pipeline(img_bytes)
|
| 33 |
+
processed_images.append((file.filename, processed_img))
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error processing {file.filename}: {e}")
|
| 36 |
+
continue
|
| 37 |
+
|
| 38 |
+
if not processed_images:
|
| 39 |
+
return JSONResponse(status_code=400, content={"error": "No valid images could be processed."})
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
zip_stream = standardize_and_zip(processed_images)
|
| 43 |
+
return Response(
|
| 44 |
+
content=zip_stream.getvalue(),
|
| 45 |
+
media_type="application/zip",
|
| 46 |
+
headers={"Content-Disposition": "attachment; filename=processed_cards.zip"}
|
| 47 |
+
)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return JSONResponse(status_code=500, content={"error": f"Failed to zip images: {str(e)}"})
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
import uvicorn
|
| 53 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True)
|