Spaces:
Paused
Paused
sachin
commited on
Commit
·
be6ce26
1
Parent(s):
e546f76
adffrim?
Browse files- Dockerfile +1 -1
- runway.py +74 -0
Dockerfile
CHANGED
@@ -35,4 +35,4 @@ USER appuser
|
|
35 |
EXPOSE 7860
|
36 |
|
37 |
# Run the server
|
38 |
-
CMD ["python", "/app/
|
|
|
35 |
EXPOSE 7860
|
36 |
|
37 |
# Run the server
|
38 |
+
CMD ["python", "/app/runway.py"]
|
runway.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import torch
|
6 |
+
from diffusers import StableDiffusionInpaintPipeline
|
7 |
+
|
8 |
+
# Initialize FastAPI app
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# Load the pre-trained inpainting model (Stable Diffusion)
|
12 |
+
model_id = "runwayml/stable-diffusion-inpainting"
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
|
15 |
+
try:
|
16 |
+
pipe = StableDiffusionInpaintPipeline.from_pretrained(model_id)
|
17 |
+
pipe.to(device)
|
18 |
+
except Exception as e:
|
19 |
+
raise RuntimeError(f"Failed to load model: {e}")
|
20 |
+
|
21 |
+
@app.get("/")
|
22 |
+
def read_root():
|
23 |
+
return {"message": "Welcome to the Image Inpainting API!"}
|
24 |
+
|
25 |
+
@app.post("/inpaint/")
|
26 |
+
async def inpaint_image(
|
27 |
+
image: UploadFile = File(...),
|
28 |
+
mask: UploadFile = File(...),
|
29 |
+
prompt: str = "Fill the masked area with appropriate content."
|
30 |
+
):
|
31 |
+
"""
|
32 |
+
Endpoint for image inpainting.
|
33 |
+
- `image`: Original image file (PNG/JPG).
|
34 |
+
- `mask`: Mask file indicating areas to inpaint (black for masked areas, white for unmasked).
|
35 |
+
- `prompt`: Text prompt describing the desired output.
|
36 |
+
"""
|
37 |
+
try:
|
38 |
+
# Load the uploaded image and mask
|
39 |
+
image_bytes = await image.read()
|
40 |
+
mask_bytes = await mask.read()
|
41 |
+
|
42 |
+
original_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
43 |
+
mask_image = Image.open(io.BytesIO(mask_bytes)).convert("L")
|
44 |
+
|
45 |
+
# Ensure dimensions match between image and mask
|
46 |
+
if original_image.size != mask_image.size:
|
47 |
+
raise HTTPException(status_code=400, detail="Image and mask dimensions must match.")
|
48 |
+
|
49 |
+
# Perform inpainting using the pipeline
|
50 |
+
result = pipe(prompt=prompt, image=original_image, mask_image=mask_image).images[0]
|
51 |
+
|
52 |
+
# Convert result to bytes for response
|
53 |
+
result_bytes = io.BytesIO()
|
54 |
+
result.save(result_bytes, format="PNG")
|
55 |
+
result_bytes.seek(0)
|
56 |
+
|
57 |
+
return JSONResponse(content={"message": "Inpainting successful!"}, media_type="image/png")
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
raise HTTPException(status_code=500, detail=f"Error during inpainting: {e}")
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
@app.get("/")
|
66 |
+
async def root():
|
67 |
+
"""
|
68 |
+
Root endpoint for basic health check.
|
69 |
+
"""
|
70 |
+
return {"message": "InstructPix2Pix API is running. Use POST /edit-image/ or /inpaint/ to edit images."}
|
71 |
+
|
72 |
+
if __name__ == "__main__":
|
73 |
+
import uvicorn
|
74 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|