Spaces:
Sleeping
Sleeping
Commit
·
d2505cb
1
Parent(s):
c5b1ef7
Add enhancer model
Browse files- .gitignore +25 -0
- Dockerfile +28 -0
- app.py +28 -0
- requirements.txt +9 -0
.gitignore
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python artifacts
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*.env
|
| 5 |
+
.venv/
|
| 6 |
+
env/
|
| 7 |
+
venv/
|
| 8 |
+
|
| 9 |
+
# Data / model weights (you generally load these at runtime)
|
| 10 |
+
weights/
|
| 11 |
+
*.pth
|
| 12 |
+
*.ckpt
|
| 13 |
+
*.h5
|
| 14 |
+
*.npz
|
| 15 |
+
|
| 16 |
+
# IDE / editor configs
|
| 17 |
+
.vscode/
|
| 18 |
+
.idea/
|
| 19 |
+
|
| 20 |
+
# OS files
|
| 21 |
+
.DS_Store
|
| 22 |
+
Thumbs.db
|
| 23 |
+
|
| 24 |
+
# Hugging Face tokens (if you store any locally)
|
| 25 |
+
*.hf_token
|
Dockerfile
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 1. Base image
|
| 2 |
+
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
# 2. Install system deps for RealESRGAN & PIL
|
| 5 |
+
RUN apt-get update && \
|
| 6 |
+
apt-get install -y --no-install-recommends \
|
| 7 |
+
git build-essential \
|
| 8 |
+
libsm6 libxext6 libxrender1 \
|
| 9 |
+
libgl1-mesa-glx && \
|
| 10 |
+
rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
# 3. Create non-root user (for pip cache ownership)
|
| 13 |
+
RUN useradd -m -u 1000 user
|
| 14 |
+
USER user
|
| 15 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 16 |
+
|
| 17 |
+
# 4. Workdir & Python deps
|
| 18 |
+
WORKDIR /app
|
| 19 |
+
COPY --chown=user requirements.txt .
|
| 20 |
+
RUN pip install --no-cache-dir --upgrade pip \
|
| 21 |
+
&& pip install --no-cache-dir -r requirements.txt
|
| 22 |
+
|
| 23 |
+
# 5. Copy app code
|
| 24 |
+
COPY --chown=user . .
|
| 25 |
+
|
| 26 |
+
# 6. Expose & run
|
| 27 |
+
EXPOSE 7860
|
| 28 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--log-level", "debug"]
|
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 5 |
+
from fastapi.responses import StreamingResponse
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from RealESRGAN import RealESRGAN
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
model = RealESRGAN(device, scale=4)
|
| 13 |
+
model.load_weights("weights/RealESRGAN_x4.pth", download=True)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@app.post("/enhance-img")
|
| 17 |
+
async def enhance_img(file: UploadFile = File(...)):
|
| 18 |
+
try:
|
| 19 |
+
data = await file.read()
|
| 20 |
+
img = Image.open(io.BytesIO(data)).convert("RGB")
|
| 21 |
+
sr = model.predict(img)
|
| 22 |
+
buf = io.BytesIO()
|
| 23 |
+
sr.save(buf, format="PNG")
|
| 24 |
+
buf.seek(0)
|
| 25 |
+
return StreamingResponse(buf, media_type="image/png")
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
raise HTTPException(500, detail=str(e))
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
torch>=1.7
|
| 4 |
+
torchvision>=0.8.0
|
| 5 |
+
Pillow
|
| 6 |
+
numpy
|
| 7 |
+
git+https://github.com/sberbank-ai/Real-ESRGAN.git
|
| 8 |
+
huggingface_hub<0.13.0
|
| 9 |
+
python-multipart
|