Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import torch
|
| 9 |
+
from realesrgan import RealESRGANer
|
| 10 |
+
|
| 11 |
+
# FastAPI Libraries
|
| 12 |
+
from fastapi import FastAPI, File, UploadFile
|
| 13 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 14 |
+
import uvicorn
|
| 15 |
+
import gradio as gr
|
| 16 |
+
|
| 17 |
+
# --- 1. Model Loading (Free Tier Optimized) ---
|
| 18 |
+
|
| 19 |
+
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 20 |
+
print(f"Model will run on: {DEVICE}")
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# Real-ESRGAN ka lightweight, optimized model use kar rahe hain
|
| 24 |
+
model_path = RealESRGANer.model_path_from_name('RealESRGAN_x4plus')
|
| 25 |
+
|
| 26 |
+
# Model ko load karna (yeh memory mein rahega)
|
| 27 |
+
UPSCALER = RealESRGANer(
|
| 28 |
+
scale=4,
|
| 29 |
+
model_path=model_path,
|
| 30 |
+
dni_weight=None,
|
| 31 |
+
model_name='RealESRGAN_x4plus',
|
| 32 |
+
device=DEVICE
|
| 33 |
+
)
|
| 34 |
+
print("Real-ESRGAN model loaded successfully.")
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"ERROR: Model load nahi ho paya. Error: {e}")
|
| 38 |
+
UPSCALER = None
|
| 39 |
+
|
| 40 |
+
def run_upscaler(img_np: np.ndarray):
|
| 41 |
+
"""Core upscaling logic."""
|
| 42 |
+
if UPSCALER is None:
|
| 43 |
+
raise Exception("Model is not initialized.")
|
| 44 |
+
|
| 45 |
+
# Upscaling (yahan time lagta hai)
|
| 46 |
+
output_np, _ = UPSCALER.enhance(img_np, outscale=4)
|
| 47 |
+
|
| 48 |
+
return output_np
|
| 49 |
+
|
| 50 |
+
# --- 2. FastAPI Setup ---
|
| 51 |
+
|
| 52 |
+
# FastAPI application ko initialize karein
|
| 53 |
+
app = FastAPI(title="Real-ESRGAN Custom Upscaler API")
|
| 54 |
+
|
| 55 |
+
# --- 3. Custom API Endpoint ---
|
| 56 |
+
|
| 57 |
+
# Image file upload ke zariye upscaling
|
| 58 |
+
@app.post("/api/upscale/file")
|
| 59 |
+
async def upscale_image_api(image: UploadFile = File(...)):
|
| 60 |
+
"""
|
| 61 |
+
Image file ko upload karein aur 4x upscaled image wapas hasil karein.
|
| 62 |
+
"""
|
| 63 |
+
try:
|
| 64 |
+
# File ko PIL Image mein padhna
|
| 65 |
+
image_bytes = await image.read()
|
| 66 |
+
input_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 67 |
+
|
| 68 |
+
# PIL image ko numpy array mein convert karna
|
| 69 |
+
img_np = np.array(input_image)
|
| 70 |
+
|
| 71 |
+
# Upscaling
|
| 72 |
+
output_np = run_upscaler(img_np)
|
| 73 |
+
|
| 74 |
+
# NumPy array ko wapas PIL Image mein convert karna
|
| 75 |
+
output_image = Image.fromarray(output_np)
|
| 76 |
+
|
| 77 |
+
# Image ko BytesIO mein save karna
|
| 78 |
+
img_io = io.BytesIO()
|
| 79 |
+
output_image.save(img_io, format='PNG')
|
| 80 |
+
img_io.seek(0)
|
| 81 |
+
|
| 82 |
+
# StreamingResponse se image ko wapas bhejna
|
| 83 |
+
return StreamingResponse(img_io, media_type="image/png")
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return JSONResponse(status_code=500, content={"message": f"Processing error: {str(e)}"})
|
| 87 |
+
|
| 88 |
+
# --- 4. Gradio UI Integration ---
|
| 89 |
+
|
| 90 |
+
def upscale_for_gradio(input_image: Image.Image):
|
| 91 |
+
"""Gradio UI ke liye wrapper function."""
|
| 92 |
+
try:
|
| 93 |
+
img_np = np.array(input_image.convert("RGB"))
|
| 94 |
+
output_np = run_upscaler(img_np)
|
| 95 |
+
return Image.fromarray(output_np)
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return f"Error: {str(e)}"
|
| 98 |
+
|
| 99 |
+
# Gradio Interface define karna
|
| 100 |
+
gr_interface = gr.Interface(
|
| 101 |
+
fn=upscale_for_gradio,
|
| 102 |
+
inputs=gr.Image(type="pil", label="Low-Resolution Image Upload Karein"),
|
| 103 |
+
outputs=gr.Image(type="pil", label="4x Upscaled (High-Quality) Image"),
|
| 104 |
+
title="⭐ Real-ESRGAN: AI Image Upscaler (UI & Custom API)",
|
| 105 |
+
description="Apni images ko 4x size mein badhayein. Yeh app Custom REST API aur Gradio UI dono offer karta hai.",
|
| 106 |
+
allow_flagging="never"
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Gradio ko FastAPI app mein mount karna
|
| 110 |
+
# '/gradio' path par UI available hoga
|
| 111 |
+
app = gr.mount_gradio_app(app, gr_interface, path="/")
|
| 112 |
+
|
| 113 |
+
# --- 5. Uvicorn Server Setup ---
|
| 114 |
+
|
| 115 |
+
# Yeh tabhi run hoga jab aap file ko directly chalayenge (lekin Docker mein yeh entry point hoga)
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
# Hugging Face Spaces Docker mein port 7860 par chalne ki umeed rakhta hai.
|
| 118 |
+
# Hamara server isi port par run hoga.
|
| 119 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 120 |
+
|