Spaces:
Sleeping
Sleeping
ahmedmbutt
commited on
Commit
•
bd94aa2
0
Parent(s):
Initial Commit
Browse files- Dockerfile +11 -0
- main.py +111 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request, Form, File, UploadFile
|
2 |
+
from fastapi.responses import StreamingResponse
|
3 |
+
from contextlib import asynccontextmanager
|
4 |
+
from starlette.middleware.cors import CORSMiddleware
|
5 |
+
|
6 |
+
import torch
|
7 |
+
from PIL import Image
|
8 |
+
from io import BytesIO
|
9 |
+
from diffusers import (
|
10 |
+
AutoPipelineForText2Image,
|
11 |
+
AutoPipelineForImage2Image,
|
12 |
+
AutoPipelineForInpainting,
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
@asynccontextmanager
|
17 |
+
async def lifespan(app: FastAPI):
|
18 |
+
text2img = AutoPipelineForText2Image.from_pretrained(
|
19 |
+
"stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16"
|
20 |
+
).to("cpu")
|
21 |
+
|
22 |
+
img2img = AutoPipelineForImage2Image.from_pipe(text2img).to("cpu")
|
23 |
+
|
24 |
+
inpaint = AutoPipelineForInpainting.from_pipe(text2img).to("cpu")
|
25 |
+
|
26 |
+
yield {"text2img": text2img, "img2img": img2img, "inpaint": inpaint}
|
27 |
+
|
28 |
+
del text2img
|
29 |
+
del img2img
|
30 |
+
del inpaint
|
31 |
+
|
32 |
+
|
33 |
+
app = FastAPI(lifespan=lifespan)
|
34 |
+
|
35 |
+
origins = ["*"]
|
36 |
+
|
37 |
+
app.add_middleware(
|
38 |
+
CORSMiddleware,
|
39 |
+
allow_origins=origins,
|
40 |
+
allow_credentials=True,
|
41 |
+
allow_methods=["*"],
|
42 |
+
allow_headers=["*"],
|
43 |
+
)
|
44 |
+
|
45 |
+
|
46 |
+
@app.get("/")
|
47 |
+
async def root():
|
48 |
+
return {"Hello": "World"}
|
49 |
+
|
50 |
+
|
51 |
+
@app.post("/text-to-image/")
|
52 |
+
async def text_to_image(request: Request, prompt: str = Form(...)):
|
53 |
+
image = request.state.text2img(
|
54 |
+
prompt=prompt, num_inference_steps=1, guidance_scale=0.0
|
55 |
+
).images[0]
|
56 |
+
|
57 |
+
bytes = BytesIO()
|
58 |
+
image.save(bytes, "PNG")
|
59 |
+
bytes.seek(0)
|
60 |
+
return StreamingResponse(bytes, media_type="image/png")
|
61 |
+
|
62 |
+
|
63 |
+
@app.post("/image-to-image/")
|
64 |
+
async def image_to_image(
|
65 |
+
request: Request, prompt: str = Form(...), init_image: UploadFile = File(...)
|
66 |
+
):
|
67 |
+
bytes = await init_image.read()
|
68 |
+
init_image = Image.open(BytesIO(bytes))
|
69 |
+
init_image = init_image.convert("RGB").resize((512, 512))
|
70 |
+
|
71 |
+
image = request.state.img2img.pipe(
|
72 |
+
prompt,
|
73 |
+
image=init_image,
|
74 |
+
num_inference_steps=2,
|
75 |
+
strength=0.5,
|
76 |
+
guidance_scale=0.0,
|
77 |
+
).images[0]
|
78 |
+
|
79 |
+
bytes = BytesIO()
|
80 |
+
image.save(bytes, "PNG")
|
81 |
+
bytes.seek(0)
|
82 |
+
return StreamingResponse(bytes, media_type="image/png")
|
83 |
+
|
84 |
+
|
85 |
+
@app.post("/inpainting/")
|
86 |
+
async def inpainting(
|
87 |
+
request: Request,
|
88 |
+
prompt: str = Form(...),
|
89 |
+
init_image: UploadFile = File(...),
|
90 |
+
mask_image: UploadFile = File(...),
|
91 |
+
):
|
92 |
+
bytes = await init_image.read()
|
93 |
+
init_image = Image.open(BytesIO(bytes))
|
94 |
+
init_image = init_image.convert("RGB").resize((512, 512))
|
95 |
+
bytes = await mask_image.read()
|
96 |
+
mask_image = Image.open(BytesIO(bytes))
|
97 |
+
mask_image = mask_image.convert("RGB").resize((512, 512))
|
98 |
+
|
99 |
+
image = request.state.inpaint.pipe(
|
100 |
+
prompt,
|
101 |
+
image=init_image,
|
102 |
+
mask_image=mask_image,
|
103 |
+
num_inference_steps=3,
|
104 |
+
strength=0.5,
|
105 |
+
guidance_scale=0.0,
|
106 |
+
).images[0]
|
107 |
+
|
108 |
+
bytes = BytesIO()
|
109 |
+
image.save(bytes, "PNG")
|
110 |
+
bytes.seek(0)
|
111 |
+
return StreamingResponse(bytes, media_type="image/png")
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
torch
|
4 |
+
diffusers
|
5 |
+
accelerate
|