Spaces:
Sleeping
Sleeping
File size: 1,371 Bytes
15702c2 0a7c51f 15702c2 38b6081 15702c2 dba317c 15702c2 38b6081 dba317c 15702c2 dba317c 15702c2 dba317c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import FileResponse, HTMLResponse
from fastapi import HTTPException
from fastapi.staticfiles import StaticFiles
import subprocess
import uuid
import os
import shutil
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
OUTPUT_DIR = "outputs"
os.makedirs(OUTPUT_DIR, exist_ok=True)
REALESRGAN_PATH = "realesrgan-ncnn-vulkan" # should be in PATH
@app.get("/", response_class=HTMLResponse)
def home():
with open("static/index.html") as f:
return f.read()
@app.post("/enhance")
async def enhance_image(file: UploadFile = File(...)):
input_name = f"{uuid.uuid4()}_{file.filename}"
input_path = os.path.join(OUTPUT_DIR, input_name)
with open(input_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
output_path = os.path.join(OUTPUT_DIR, f"enhanced_{input_name}")
cmd = [
"realesrgan-ncnn-vulkan",
"-i", input_path,
"-o", output_path,
"-s", "2",
"-m", "/opt/realesrgan/models"
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise HTTPException(status_code=500, detail=result.stderr)
enhanced = os.path.join(OUTPUT_DIR, f"enhanced_{input_name}")
return FileResponse(enhanced, media_type="image/png")
|