Upload 3 files
Browse files- Dockerfile +12 -0
- app.py +111 -0
- requirements.txt +14 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y ffmpeg git && rm -rf /var/lib/apt/lists/*
|
| 6 |
+
|
| 7 |
+
COPY requirements.txt .
|
| 8 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 9 |
+
|
| 10 |
+
COPY . .
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
+
import uuid, os
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
from rembg import remove
|
| 6 |
+
from realesrgan import RealESRGAN
|
| 7 |
+
from gfpgan import GFPGANer
|
| 8 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
UPLOAD_DIR = "uploads"
|
| 14 |
+
OUTPUT_DIR = "outputs"
|
| 15 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 16 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
|
| 20 |
+
# ===== Load models ONCE =====
|
| 21 |
+
|
| 22 |
+
# Real-ESRGAN
|
| 23 |
+
upscaler = RealESRGAN(torch.device(DEVICE), scale=4)
|
| 24 |
+
upscaler.load_weights("RealESRGAN_x4.pth")
|
| 25 |
+
|
| 26 |
+
# GFPGAN
|
| 27 |
+
gfpgan = GFPGANer(
|
| 28 |
+
model_path=None,
|
| 29 |
+
upscale=1,
|
| 30 |
+
arch="clean",
|
| 31 |
+
channel_multiplier=2,
|
| 32 |
+
device=DEVICE
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Stable Diffusion Img2Img
|
| 36 |
+
sd_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 37 |
+
"runwayml/stable-diffusion-v1-5",
|
| 38 |
+
torch_dtype=torch.float16
|
| 39 |
+
).to(DEVICE)
|
| 40 |
+
|
| 41 |
+
# ===== Endpoints =====
|
| 42 |
+
|
| 43 |
+
@app.post("/remove-bg")
|
| 44 |
+
async def remove_bg(file: UploadFile = File(...)):
|
| 45 |
+
img = Image.open(file.file).convert("RGBA")
|
| 46 |
+
out = remove(img)
|
| 47 |
+
|
| 48 |
+
fname = f"{uuid.uuid4().hex}.png"
|
| 49 |
+
path = os.path.join(OUTPUT_DIR, fname)
|
| 50 |
+
out.save(path)
|
| 51 |
+
|
| 52 |
+
return {"file": fname}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.post("/enhance")
|
| 56 |
+
async def enhance(file: UploadFile = File(...)):
|
| 57 |
+
img = Image.open(file.file).convert("RGB")
|
| 58 |
+
out = upscaler.predict(img)
|
| 59 |
+
|
| 60 |
+
fname = f"{uuid.uuid4().hex}.png"
|
| 61 |
+
out.save(os.path.join(OUTPUT_DIR, fname))
|
| 62 |
+
return {"file": fname}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@app.post("/upscale")
|
| 66 |
+
async def upscale(file: UploadFile = File(...), scale: int = Form(2)):
|
| 67 |
+
img = Image.open(file.file).convert("RGB")
|
| 68 |
+
upscaler.scale = scale
|
| 69 |
+
out = upscaler.predict(img)
|
| 70 |
+
|
| 71 |
+
fname = f"{uuid.uuid4().hex}.png"
|
| 72 |
+
out.save(os.path.join(OUTPUT_DIR, fname))
|
| 73 |
+
return {"file": fname}
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@app.post("/retouch")
|
| 77 |
+
async def retouch(file: UploadFile = File(...)):
|
| 78 |
+
img = Image.open(file.file).convert("RGB")
|
| 79 |
+
_, _, out = gfpgan.enhance(
|
| 80 |
+
img,
|
| 81 |
+
has_aligned=False,
|
| 82 |
+
only_center_face=False,
|
| 83 |
+
paste_back=True
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
fname = f"{uuid.uuid4().hex}.png"
|
| 87 |
+
Image.fromarray(out).save(os.path.join(OUTPUT_DIR, fname))
|
| 88 |
+
return {"file": fname}
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
@app.post("/edit")
|
| 92 |
+
async def edit_image(
|
| 93 |
+
file: UploadFile = File(...),
|
| 94 |
+
prompt: str = Form(...),
|
| 95 |
+
strength: float = Form(0.6)
|
| 96 |
+
):
|
| 97 |
+
img = Image.open(file.file).convert("RGB").resize((512, 512))
|
| 98 |
+
|
| 99 |
+
result = sd_pipe(
|
| 100 |
+
prompt=prompt,
|
| 101 |
+
image=img,
|
| 102 |
+
strength=strength,
|
| 103 |
+
guidance_scale=7.5
|
| 104 |
+
).images[0]
|
| 105 |
+
|
| 106 |
+
fname = f"{uuid.uuid4().hex}.png"
|
| 107 |
+
result.save(os.path.join(OUTPUT_DIR, fname))
|
| 108 |
+
return {
|
| 109 |
+
"prompt": prompt,
|
| 110 |
+
"file": fname
|
| 111 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
diffusers
|
| 6 |
+
transformers
|
| 7 |
+
accelerate
|
| 8 |
+
opencv-python
|
| 9 |
+
pillow
|
| 10 |
+
numpy
|
| 11 |
+
scipy
|
| 12 |
+
rembg
|
| 13 |
+
realesrgan
|
| 14 |
+
gfpgan
|