File size: 1,122 Bytes
d3c69c2
 
d2e7948
 
 
 
 
 
 
d3c69c2
 
 
 
 
 
 
d2e7948
 
 
 
 
 
 
 
d3c69c2
d2e7948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import os
import torch
import gradio as gr
from fastapi import FastAPI, UploadFile, File
import uvicorn
from PIL import Image
import io

# Ensure the `basicsr` directory is in the system path
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_dir, "basicsr"))

# Import the inference function
from inference_codeformer import inference  

app = FastAPI()

# Load the CodeFormer model
model_path = "weights/CodeFormer.pth"
device = "cuda" if torch.cuda.is_available() else "cpu"

@app.post("/enhance")
async def enhance_image(file: UploadFile = File(...), upscale: int = 2, fidelity: float = 0.5):
    """API Endpoint to enhance images using CodeFormer"""
    image = Image.open(io.BytesIO(await file.read()))
    image.save("input.png")

    output_path = inference(
        input_path="input.png",
        upscale=upscale,
        fidelity=fidelity,
        model_path=model_path,
        device=device
    )

    return {"enhanced_image": f"https://your-space-name.hf.space/{output_path}"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)