Rahul-Samedavar commited on
Commit
15702c2
·
1 Parent(s): c2f60b2
Files changed (4) hide show
  1. Dockerfile +33 -0
  2. app.py +49 -0
  3. index.html +39 -0
  4. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ RUN apt-get update && apt-get install -y \
13
+ wget \
14
+ unzip \
15
+ libvulkan1 \
16
+ vulkan-tools \
17
+ libomp5 \
18
+ && rm -rf /var/lib/apt/lists/*
19
+
20
+ RUN wget https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan/releases/download/v0.2.0/realesrgan-ncnn-vulkan-v0.2.0-ubuntu.zip \
21
+ && unzip realesrgan-ncnn-vulkan-v0.2.0-ubuntu.zip \
22
+ && rm realesrgan-ncnn-vulkan-v0.2.0-ubuntu.zip \
23
+ && mv realesrgan-ncnn-vulkan-*-ubuntu realesrgan-ncnn-vulkan \
24
+ && chmod +x realesrgan-ncnn-vulkan/realesrgan-ncnn-vulkan \
25
+ && mv realesrgan-ncnn-vulkan /usr/local/bin/ \
26
+ && mkdir -p /usr/local/share/realesrgan-ncnn-vulkan/models \
27
+ && wget -O /usr/local/share/realesrgan-ncnn-vulkan/models/realesr-animevideov3.bin https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan/releases/download/v0.2.0/realesr-animevideov3.bin \
28
+ && wget -O /usr/local/share/realesrgan-ncnn-vulkan/models/realesr-animevideov3.param https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan/releases/download/v0.2.0/realesr-animevideov3.param \
29
+
30
+
31
+
32
+ COPY --chown=user . /app
33
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import FileResponse, HTMLResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ import subprocess
5
+ import uuid
6
+ import os
7
+ import shutil
8
+
9
+ app = FastAPI()
10
+
11
+ app.mount("/static", StaticFiles(directory="static"), name="static")
12
+
13
+ OUTPUT_DIR = "outputs"
14
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
15
+
16
+ REALESRGAN_PATH = "realesrgan-ncnn-vulkan" # should be in PATH
17
+
18
+
19
+ @app.get("/", response_class=HTMLResponse)
20
+ def home():
21
+ with open("static/index.html") as f:
22
+ return f.read()
23
+
24
+
25
+ @app.post("/enhance")
26
+ async def enhance_image(file: UploadFile = File(...)):
27
+ input_name = f"{uuid.uuid4()}_{file.filename}"
28
+ output_name = f"enhanced_{input_name}"
29
+
30
+ input_path = os.path.join(OUTPUT_DIR, input_name)
31
+ output_path = os.path.join(OUTPUT_DIR, output_name)
32
+
33
+ with open(input_path, "wb") as buffer:
34
+ shutil.copyfileobj(file.file, buffer)
35
+
36
+ cmd = [
37
+ REALESRGAN_PATH,
38
+ "-i", input_path,
39
+ "-o", output_path,
40
+ "-s", "2"
41
+ ]
42
+
43
+ subprocess.run(cmd, check=True)
44
+
45
+ return FileResponse(
46
+ output_path,
47
+ media_type="image/png",
48
+ filename=output_name
49
+ )
index.html ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Image Enhancer</title>
5
+ <style>
6
+ body { font-family: Arial; padding: 40px; }
7
+ img { max-width: 400px; margin-top: 20px; }
8
+ </style>
9
+ </head>
10
+ <body>
11
+
12
+ <h2>Real-ESRGAN Image Enhancer</h2>
13
+
14
+ <input type="file" id="fileInput">
15
+ <button onclick="upload()">Enhance</button>
16
+
17
+ <br>
18
+ <img id="result"/>
19
+
20
+ <script>
21
+ async function upload() {
22
+ const file = document.getElementById("fileInput").files[0];
23
+ if (!file) return alert("Select image");
24
+
25
+ const form = new FormData();
26
+ form.append("file", file);
27
+
28
+ const res = await fetch("/enhance", {
29
+ method: "POST",
30
+ body: form
31
+ });
32
+
33
+ const blob = await res.blob();
34
+ document.getElementById("result").src = URL.createObjectURL(blob);
35
+ }
36
+ </script>
37
+
38
+ </body>
39
+ </html>
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ uvicorn[standard]