Spaces:
Sleeping
Sleeping
Add 6
Browse files- Dockerfile +4 -32
- app.py +27 -6
Dockerfile
CHANGED
|
@@ -1,42 +1,14 @@
|
|
| 1 |
-
FROM
|
| 2 |
|
| 3 |
-
# ======================
|
| 4 |
-
# Environment
|
| 5 |
-
# ======================
|
| 6 |
-
ENV DEBIAN_FRONTEND=noninteractive
|
| 7 |
-
ENV PYTHONUNBUFFERED=1
|
| 8 |
-
ENV PIP_NO_CACHE_DIR=1
|
| 9 |
-
|
| 10 |
-
# ======================
|
| 11 |
-
# System dependencies
|
| 12 |
-
# ======================
|
| 13 |
-
RUN apt-get update && apt-get install -y \
|
| 14 |
-
python3 \
|
| 15 |
-
python3-pip \
|
| 16 |
-
git \
|
| 17 |
-
ffmpeg \
|
| 18 |
-
libgl1 \
|
| 19 |
-
ca-certificates \
|
| 20 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 21 |
-
|
| 22 |
-
# python -> python3
|
| 23 |
-
RUN ln -s /usr/bin/python3 /usr/bin/python
|
| 24 |
-
|
| 25 |
-
# ======================
|
| 26 |
-
# App setup
|
| 27 |
-
# ======================
|
| 28 |
WORKDIR /app
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
COPY requirements.txt .
|
| 32 |
-
RUN pip install --
|
| 33 |
|
| 34 |
-
# Copy app
|
| 35 |
COPY app.py .
|
| 36 |
|
| 37 |
-
# ======================
|
| 38 |
-
# Runtime
|
| 39 |
-
# ======================
|
| 40 |
EXPOSE 7860
|
| 41 |
|
| 42 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
+
RUN apt-get update && apt-get install -y 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 app.py .
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
EXPOSE 7860
|
| 13 |
|
| 14 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -13,43 +13,63 @@ from diffusers import (
|
|
| 13 |
EulerAncestralDiscreteScheduler
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
MODEL_ID = "peter-sushko/RealEdit"
|
| 17 |
-
|
| 18 |
FIXED_STEPS = 50
|
| 19 |
FIXED_GUIDANCE_SCALE = 2.0
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
app = FastAPI(title="RealEdit API")
|
| 22 |
|
| 23 |
print("Loading RealEdit model...")
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
| 26 |
MODEL_ID,
|
| 27 |
-
torch_dtype=
|
| 28 |
safety_checker=None
|
| 29 |
)
|
| 30 |
|
| 31 |
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(
|
| 32 |
pipe.scheduler.config
|
| 33 |
)
|
| 34 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 35 |
-
pipe = pipe.to(device)
|
| 36 |
|
|
|
|
| 37 |
|
| 38 |
print("Model loaded successfully!")
|
| 39 |
|
| 40 |
-
|
| 41 |
# =========================
|
| 42 |
# Core inference
|
| 43 |
# =========================
|
| 44 |
@torch.inference_mode()
|
| 45 |
def run_inference(image, prompt):
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
result = pipe(
|
| 48 |
prompt=prompt,
|
| 49 |
image=image,
|
| 50 |
num_inference_steps=FIXED_STEPS,
|
| 51 |
image_guidance_scale=FIXED_GUIDANCE_SCALE
|
| 52 |
).images[0]
|
|
|
|
| 53 |
return result
|
| 54 |
|
| 55 |
|
|
@@ -122,4 +142,5 @@ gradio_ui = gr.Interface(
|
|
| 122 |
)
|
| 123 |
)
|
| 124 |
|
|
|
|
| 125 |
app = gr.mount_gradio_app(app, gradio_ui, path="/")
|
|
|
|
| 13 |
EulerAncestralDiscreteScheduler
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# =========================
|
| 17 |
+
# Config
|
| 18 |
+
# =========================
|
| 19 |
MODEL_ID = "peter-sushko/RealEdit"
|
|
|
|
| 20 |
FIXED_STEPS = 50
|
| 21 |
FIXED_GUIDANCE_SCALE = 2.0
|
| 22 |
|
| 23 |
+
# =========================
|
| 24 |
+
# App
|
| 25 |
+
# =========================
|
| 26 |
app = FastAPI(title="RealEdit API")
|
| 27 |
|
| 28 |
print("Loading RealEdit model...")
|
| 29 |
|
| 30 |
+
# Detect device
|
| 31 |
+
use_cuda = torch.cuda.is_available()
|
| 32 |
+
device = "cuda" if use_cuda else "cpu"
|
| 33 |
+
dtype = torch.float16 if use_cuda else torch.float32
|
| 34 |
+
|
| 35 |
+
print(f"Using device: {device}, dtype: {dtype}")
|
| 36 |
+
|
| 37 |
+
# Load pipeline
|
| 38 |
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
| 39 |
MODEL_ID,
|
| 40 |
+
torch_dtype=dtype,
|
| 41 |
safety_checker=None
|
| 42 |
)
|
| 43 |
|
| 44 |
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(
|
| 45 |
pipe.scheduler.config
|
| 46 |
)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
pipe = pipe.to(device)
|
| 49 |
|
| 50 |
print("Model loaded successfully!")
|
| 51 |
|
|
|
|
| 52 |
# =========================
|
| 53 |
# Core inference
|
| 54 |
# =========================
|
| 55 |
@torch.inference_mode()
|
| 56 |
def run_inference(image, prompt):
|
| 57 |
+
if device == "cuda":
|
| 58 |
+
with torch.autocast("cuda"):
|
| 59 |
+
result = pipe(
|
| 60 |
+
prompt=prompt,
|
| 61 |
+
image=image,
|
| 62 |
+
num_inference_steps=FIXED_STEPS,
|
| 63 |
+
image_guidance_scale=FIXED_GUIDANCE_SCALE
|
| 64 |
+
).images[0]
|
| 65 |
+
else:
|
| 66 |
result = pipe(
|
| 67 |
prompt=prompt,
|
| 68 |
image=image,
|
| 69 |
num_inference_steps=FIXED_STEPS,
|
| 70 |
image_guidance_scale=FIXED_GUIDANCE_SCALE
|
| 71 |
).images[0]
|
| 72 |
+
|
| 73 |
return result
|
| 74 |
|
| 75 |
|
|
|
|
| 142 |
)
|
| 143 |
)
|
| 144 |
|
| 145 |
+
# ⚠️ Mount UI at ROOT for Hugging Face Spaces
|
| 146 |
app = gr.mount_gradio_app(app, gradio_ui, path="/")
|