Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,24 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
from hf_diffusion_service import HFDiffusionService
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
|
|
|
| 5 |
service = HFDiffusionService()
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
from hf_diffusion_service import HFDiffusionService
|
| 4 |
from PIL import Image
|
| 5 |
+
import io, base64
|
| 6 |
|
| 7 |
+
app = FastAPI()
|
| 8 |
service = HFDiffusionService()
|
| 9 |
|
| 10 |
+
@app.post("/generate")
|
| 11 |
+
async def generate(file: UploadFile):
|
| 12 |
+
# Read file bytes
|
| 13 |
+
image_bytes = await file.read()
|
| 14 |
+
mask = Image.open(io.BytesIO(image_bytes)).convert("L")
|
| 15 |
+
|
| 16 |
+
# Generate CT
|
| 17 |
+
result = service.generate_image(mask)
|
| 18 |
+
|
| 19 |
+
# Convert to base64
|
| 20 |
+
buffer = io.BytesIO()
|
| 21 |
+
result.save(buffer, format="PNG")
|
| 22 |
+
base64_img = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 23 |
+
|
| 24 |
+
return JSONResponse(content={"image": f"data:image/png;base64,{base64_img}"})
|