Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,21 +15,26 @@ model_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/Rea
|
|
| 15 |
model_path = 'RealESRGAN_x4plus.pth'
|
| 16 |
|
| 17 |
if not os.path.exists(model_path):
|
| 18 |
-
print("Downloading AI model...")
|
| 19 |
response = requests.get(model_url)
|
| 20 |
with open(model_path, 'wb') as f:
|
| 21 |
f.write(response.content)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
scale=4
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
@app.get("/")
|
| 35 |
def home():
|
|
@@ -38,22 +43,19 @@ def home():
|
|
| 38 |
@app.post("/upscale")
|
| 39 |
async def upscale(file: UploadFile = File(...)):
|
| 40 |
try:
|
| 41 |
-
# Read image
|
| 42 |
data = await file.read()
|
| 43 |
nparr = np.frombuffer(data, np.uint8)
|
| 44 |
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
# outscale=4 makes it 4x bigger and sharper
|
| 48 |
output, _ = upsampler.enhance(img, outscale=4)
|
| 49 |
|
| 50 |
-
# Convert
|
| 51 |
-
_, encoded_img = cv2.imencode('.jpg', output, [int(cv2.IMWRITE_JPEG_QUALITY),
|
| 52 |
return Response(content=encoded_img.tobytes(), media_type="image/jpeg")
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
|
| 56 |
-
return {"error": "AI Engine busy or image too large"}
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
import uvicorn
|
|
|
|
| 15 |
model_path = 'RealESRGAN_x4plus.pth'
|
| 16 |
|
| 17 |
if not os.path.exists(model_path):
|
| 18 |
+
print("Downloading AI model... please wait.")
|
| 19 |
response = requests.get(model_url)
|
| 20 |
with open(model_path, 'wb') as f:
|
| 21 |
f.write(response.content)
|
| 22 |
|
| 23 |
+
# Initialize the AI Engine
|
| 24 |
+
try:
|
| 25 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 26 |
+
upsampler = RealESRGANer(
|
| 27 |
+
scale=4,
|
| 28 |
+
model_path=model_path,
|
| 29 |
+
model=model,
|
| 30 |
+
tile=400,
|
| 31 |
+
tile_pad=10,
|
| 32 |
+
pre_pad=0,
|
| 33 |
+
half=False
|
| 34 |
+
)
|
| 35 |
+
print("✅ AI Engine Loaded Successfully")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"❌ Initialization Error: {e}")
|
| 38 |
|
| 39 |
@app.get("/")
|
| 40 |
def home():
|
|
|
|
| 43 |
@app.post("/upscale")
|
| 44 |
async def upscale(file: UploadFile = File(...)):
|
| 45 |
try:
|
|
|
|
| 46 |
data = await file.read()
|
| 47 |
nparr = np.frombuffer(data, np.uint8)
|
| 48 |
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 49 |
|
| 50 |
+
# Process image (4x Upscale)
|
|
|
|
| 51 |
output, _ = upsampler.enhance(img, outscale=4)
|
| 52 |
|
| 53 |
+
# Convert to high-quality JPG
|
| 54 |
+
_, encoded_img = cv2.imencode('.jpg', output, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
|
| 55 |
return Response(content=encoded_img.tobytes(), media_type="image/jpeg")
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
+
return {"error": str(e)}
|
|
|
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
import uvicorn
|