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gradio as gr
|
| 8 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 9 |
+
from realesrgan import RealESRGANer
|
| 10 |
+
|
| 11 |
+
# ==========================================
|
| 12 |
+
# 1. AUTO-PATCHER (Bedah Kode Otomatis)
|
| 13 |
+
# ==========================================
|
| 14 |
+
# Mengatasi error 'torchvision' yang sering terjadi di server baru
|
| 15 |
+
def patch_basicsr():
|
| 16 |
+
try:
|
| 17 |
+
import basicsr
|
| 18 |
+
pkg_path = os.path.dirname(basicsr.__file__)
|
| 19 |
+
target_file = os.path.join(pkg_path, 'data', 'degradations.py')
|
| 20 |
+
|
| 21 |
+
if os.path.exists(target_file):
|
| 22 |
+
with open(target_file, 'r') as f: data = f.read()
|
| 23 |
+
|
| 24 |
+
old_str = 'from torchvision.transforms.functional_tensor import rgb_to_grayscale'
|
| 25 |
+
new_str = 'from torchvision.transforms.functional import rgb_to_grayscale'
|
| 26 |
+
|
| 27 |
+
if old_str in data:
|
| 28 |
+
data = data.replace(old_str, new_str)
|
| 29 |
+
with open(target_file, 'w') as f: f.write(data)
|
| 30 |
+
print("✅ Auto-Patch: Library basicsr berhasil diperbaiki.")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"⚠️ Auto-Patch Warning: {e}")
|
| 33 |
+
|
| 34 |
+
patch_basicsr()
|
| 35 |
+
|
| 36 |
+
# ==========================================
|
| 37 |
+
# 2. PERSIAPAN MODEL
|
| 38 |
+
# ==========================================
|
| 39 |
+
# Cek ketersediaan GPU (Hugging Face Free Tier biasanya CPU, Paid dapat GPU)
|
| 40 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 41 |
+
print(f"🚀 Running on: {device}")
|
| 42 |
+
|
| 43 |
+
# Download Model Pre-trained jika belum ada
|
| 44 |
+
model_url = 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'
|
| 45 |
+
model_path = 'RealESRGAN_x4plus.pth'
|
| 46 |
+
|
| 47 |
+
if not os.path.exists(model_path):
|
| 48 |
+
print("⏳ Mendownload model AI...")
|
| 49 |
+
response = requests.get(model_url)
|
| 50 |
+
with open(model_path, 'wb') as f:
|
| 51 |
+
f.write(response.content)
|
| 52 |
+
print("✅ Model siap!")
|
| 53 |
+
|
| 54 |
+
# Load Model ke Memory
|
| 55 |
+
def load_model():
|
| 56 |
+
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
|
| 57 |
+
netscale = 4
|
| 58 |
+
|
| 59 |
+
upsampler = RealESRGANer(
|
| 60 |
+
scale=netscale,
|
| 61 |
+
model_path=model_path,
|
| 62 |
+
dni_weight=None,
|
| 63 |
+
model=model,
|
| 64 |
+
tile=200, # Tile kecil agar aman untuk CPU/RAM kecil
|
| 65 |
+
tile_pad=10,
|
| 66 |
+
pre_pad=0,
|
| 67 |
+
half=False, # False agar aman di CPU (fp32)
|
| 68 |
+
device=device
|
| 69 |
+
)
|
| 70 |
+
return upsampler
|
| 71 |
+
|
| 72 |
+
# Inisialisasi Upsampler Global
|
| 73 |
+
upsampler = load_model()
|
| 74 |
+
|
| 75 |
+
# ==========================================
|
| 76 |
+
# 3. LOGIKA UTAMA (PIPELINE)
|
| 77 |
+
# ==========================================
|
| 78 |
+
def process_image(img, scale, face_enhance):
|
| 79 |
+
if img is None:
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
# --- MODE DOWNSCALE (MENGECILKAN) ---
|
| 83 |
+
if scale < 1.0:
|
| 84 |
+
h, w = img.shape[:2]
|
| 85 |
+
new_w, new_h = int(w * scale), int(h * scale)
|
| 86 |
+
# Gunakan INTER_AREA untuk hasil pengecilan terbaik (tajam)
|
| 87 |
+
output = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_AREA)
|
| 88 |
+
return output
|
| 89 |
+
|
| 90 |
+
# --- MODE UPSCALE (MEMBESARKAN) ---
|
| 91 |
+
else:
|
| 92 |
+
try:
|
| 93 |
+
# Upscale menggunakan Real-ESRGAN
|
| 94 |
+
# outscale=scale akan otomatis resize hasil akhir sesuai keinginan user
|
| 95 |
+
output, _ = upsampler.enhance(img, outscale=scale)
|
| 96 |
+
|
| 97 |
+
# Fitur Perbaikan Wajah (Optional)
|
| 98 |
+
if face_enhance:
|
| 99 |
+
from gfpgan import GFPGANer
|
| 100 |
+
face_enhancer = GFPGANer(
|
| 101 |
+
model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth',
|
| 102 |
+
upscale=scale,
|
| 103 |
+
arch='clean',
|
| 104 |
+
channel_multiplier=2,
|
| 105 |
+
bg_upsampler=upsampler
|
| 106 |
+
)
|
| 107 |
+
_, _, output = face_enhancer.enhance(output, has_aligned=False, only_center_face=False, paste_back=True)
|
| 108 |
+
|
| 109 |
+
return output
|
| 110 |
+
|
| 111 |
+
except RuntimeError as e:
|
| 112 |
+
# Tangani error jika RAM tidak cukup
|
| 113 |
+
if 'out of memory' in str(e):
|
| 114 |
+
return None
|
| 115 |
+
raise e
|
| 116 |
+
|
| 117 |
+
# ==========================================
|
| 118 |
+
# 4. ANTARMUKA GRADIO
|
| 119 |
+
# ==========================================
|
| 120 |
+
title = "Smart Image Resizer & Upscaler"
|
| 121 |
+
description = """
|
| 122 |
+
**Fitur Utama:**
|
| 123 |
+
1. **Upscale Cerdas:** Menggunakan Real-ESRGAN untuk memperbesar gambar tanpa pecah.
|
| 124 |
+
2. **Downscale Tajam:** Menggunakan algoritma resampling area untuk memperkecil gambar dengan detail tinggi.
|
| 125 |
+
3. **Face Restore:** Memperbaiki wajah yang buram (opsional).
|
| 126 |
+
|
| 127 |
+
*Catatan: Jika berjalan di CPU (HF Free Tier), proses upscale gambar besar mungkin memakan waktu 30-60 detik.*
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
iface = gr.Interface(
|
| 131 |
+
fn=process_image,
|
| 132 |
+
inputs=[
|
| 133 |
+
gr.Image(label="Upload Gambar", type="numpy"),
|
| 134 |
+
gr.Slider(0.1, 4.0, value=2.0, step=0.1, label="Skala Resolusi (0.5 = Kecil, 2.0 = Besar)"),
|
| 135 |
+
gr.Checkbox(label="Perbaiki Wajah (Face Restoration)", value=False)
|
| 136 |
+
],
|
| 137 |
+
outputs=gr.Image(label="Hasil Proses"),
|
| 138 |
+
title=title,
|
| 139 |
+
description=description,
|
| 140 |
+
allow_flagging="never"
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
iface.launch()
|