File size: 1,480 Bytes
84fecd7
 
 
85793ab
84fecd7
 
9e53922
84fecd7
85793ab
 
 
 
 
 
 
84fecd7
 
 
 
9e53922
96b9654
84fecd7
 
 
 
 
 
 
 
 
 
 
96b9654
84fecd7
96b9654
84fecd7
9e53922
84fecd7
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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=["*"],
)
@app.post("/make-bald/")
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)}")

@app.get("/")
def home():
    return {"message": "Bald banne aaya? POST /make-bald/ pe image daal!"}