Spaces:
Running
Running
| # main_fastapi.py | |
| from fastapi import FastAPI, UploadFile, File, HTTPException | |
| from fastapi.responses import StreamingResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import io | |
| from bald_processor import make_realistic_bald | |
| app = FastAPI(title="Make Me Bald API ๐") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Ya specific domains daal dena | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| async def bald_endpoint(file: UploadFile = File(...)): | |
| if not file.content_type.startswith("image/"): | |
| raise HTTPException(status_code=400, detail="Sirf image file upload kar!") | |
| try: | |
| contents = await file.read() | |
| from PIL import Image | |
| image = Image.open(io.BytesIO(contents)).convert("RGB") | |
| bald_img = make_realistic_bald(image) | |
| buf = io.BytesIO() | |
| bald_img.save(buf, format="JPEG") | |
| buf.seek(0) | |
| return StreamingResponse(buf, media_type="image/jpeg", headers={"Content-Disposition":"attachment; filename=bald.jpg"}) | |
| except ValueError as ve: | |
| if str(ve) == "NO_HAIR_DETECTED": | |
| raise HTTPException(400, detail="NO_HAIR_DETECTED") | |
| else: | |
| raise HTTPException(400, detail=str(ve)) | |
| except Exception as e: | |
| raise HTTPException(500, detail=f"Processing failed: {str(e)}") | |
| def home(): | |
| return {"message": "Bald banne aaya? POST /make-bald/ pe image daal!"} | |