Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import cv2
|
|
@@ -5,81 +8,77 @@ import numpy as np
|
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import warnings
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Suppress specific warnings or all warnings if needed
|
| 10 |
warnings.filterwarnings("ignore")
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
from codeformer import CodeFormer
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
print(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
print(
|
| 28 |
-
|
| 29 |
-
# Initialize CodeFormer - Model weights will be downloaded automatically on first run
|
| 30 |
-
# Ensure you have internet access in the Space for the download.
|
| 31 |
-
print("Initializing CodeFormer model...")
|
| 32 |
-
try:
|
| 33 |
-
# Adjust model path if needed, but pretrained=True should handle downloads
|
| 34 |
-
# Check the documentation for the 'codeformer' package if this fails.
|
| 35 |
-
# Common parameters: bg_upsampler='realesrgan', face_upsample=True
|
| 36 |
-
codeformer_net = CodeFormer(
|
| 37 |
-
dim_embd=512,
|
| 38 |
-
codebook_size=1024,
|
| 39 |
-
n_head=8,
|
| 40 |
-
n_layers=9,
|
| 41 |
-
connect_list=['32', '64', '128', '256']
|
| 42 |
-
).to(device)
|
| 43 |
-
|
| 44 |
-
# Load the pre-trained model weights
|
| 45 |
-
# Adjust the path based on how the package stores weights or if downloaded manually
|
| 46 |
-
# This path assumes the standard download location used by `load_state_dict_from_url`
|
| 47 |
-
# It might differ based on the specific 'codeformer' pip package version.
|
| 48 |
-
# If this fails, check where the package downloads/expects the .pth file.
|
| 49 |
-
model_path = 'weights/CodeFormer/codeformer.pth' # Default path often used
|
| 50 |
-
|
| 51 |
-
# Check if the default path exists, otherwise rely on package's internal loading if possible
|
| 52 |
-
# A robust package might have a load_pretrained() method. Check its usage.
|
| 53 |
-
# This explicit loading might be needed if the package is minimal.
|
| 54 |
-
# Let's assume the package handles loading implicitly or requires a different call.
|
| 55 |
-
# Simpler approach: Rely on package potentially loading during init or a specific method.
|
| 56 |
-
# If the above CodeFormer() init doesn't load weights, check package docs.
|
| 57 |
-
# For now, let's assume the package *might* need explicit loading IF NOT BUILT-IN:
|
| 58 |
-
|
| 59 |
-
# Placeholder checkpoint loading - adjust based on actual package behavior
|
| 60 |
-
# This might be automatically handled by the package; if the app fails here,
|
| 61 |
-
# investigate how the specific `codeformer` pip package loads weights.
|
| 62 |
try:
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
#
|
| 68 |
-
#
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
codeformer_net.eval()
|
| 77 |
-
print("CodeFormer model initialized successfully.")
|
| 78 |
-
except Exception as e:
|
| 79 |
-
print(f"Error initializing CodeFormer model: {e}")
|
| 80 |
-
# Provide helpful error message in the UI if initialization fails
|
| 81 |
-
gr.Error(f"Failed to load CodeFormer model. Check logs. Error: {e}")
|
| 82 |
-
codeformer_net = None # Set to None to prevent processing attempts
|
| 83 |
|
| 84 |
# --- Processing Function ---
|
| 85 |
def enhance_image(input_img, fidelity_weight, background_enhance, face_upsample):
|
|
@@ -91,39 +90,50 @@ def enhance_image(input_img, fidelity_weight, background_enhance, face_upsample)
|
|
| 91 |
background_enhance (bool): Whether to enhance background using RealESRGAN.
|
| 92 |
face_upsample (bool): Whether to further upsample restored faces.
|
| 93 |
Returns:
|
| 94 |
-
np.ndarray: Enhanced image (RGB format).
|
| 95 |
-
str:
|
| 96 |
"""
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
if input_img is None:
|
|
|
|
| 101 |
return None, "Error: No input image provided."
|
| 102 |
|
| 103 |
print(f"Processing image with fidelity: {fidelity_weight}, bg_enhance: {background_enhance}, face_upsample: {face_upsample}")
|
| 104 |
start_time = time.time()
|
| 105 |
|
| 106 |
try:
|
| 107 |
-
#
|
| 108 |
img_bgr = cv2.cvtColor(input_img, cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
#
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
# The exact parameters (like `w`, `adain`) depend on the CodeFormer implementation.
|
| 115 |
-
# `w` typically corresponds to fidelity_weight.
|
| 116 |
with torch.no_grad():
|
| 117 |
output_bgr, _, _ = codeformer_net.enhance(
|
| 118 |
img_bgr,
|
| 119 |
w=fidelity_weight,
|
| 120 |
-
adain=True, #
|
| 121 |
face_upsample=face_upsample,
|
| 122 |
-
bg_upsampler=
|
| 123 |
)
|
|
|
|
| 124 |
|
| 125 |
-
# Convert back to RGB for Gradio display
|
| 126 |
output_rgb = cv2.cvtColor(output_bgr, cv2.COLOR_BGR2RGB)
|
|
|
|
| 127 |
|
| 128 |
end_time = time.time()
|
| 129 |
processing_time = end_time - start_time
|
|
@@ -132,45 +142,71 @@ def enhance_image(input_img, fidelity_weight, background_enhance, face_upsample)
|
|
| 132 |
return output_rgb, time_msg
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
return None,
|
| 139 |
|
| 140 |
-
# --- Gradio Interface ---
|
| 141 |
title = "CodeFormer Image Enhancement (CPU Demo)"
|
| 142 |
-
description = """
|
| 143 |
Upload an image to enhance its quality, particularly for faces, using CodeFormer.
|
| 144 |
**Note:** This demo runs on a free Hugging Face CPU. Processing will be **SLOW** (expect seconds to minutes per image).
|
| 145 |
Adjust the fidelity weight (0 = max quality enhancement, 1 = closer to original). Optionally enhance background and upsample faces.
|
|
|
|
| 146 |
"""
|
| 147 |
article = "<p style='text-align: center'>CodeFormer CPU Demo | <a href='https://github.com/sczhou/CodeFormer' target='_blank'>Official Repo</a></p>"
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
gr.Checkbox(label="Enhance Background (Uses RealESRGAN)", value=True),
|
| 155 |
-
gr.Checkbox(label="Upsample Restored Faces", value=True)
|
| 156 |
-
],
|
| 157 |
-
outputs=[
|
| 158 |
-
gr.Image(label="Enhanced Image", type="numpy"),
|
| 159 |
-
gr.Textbox(label="Processing Time")
|
| 160 |
-
],
|
| 161 |
-
title=title,
|
| 162 |
-
description=description,
|
| 163 |
-
article=article,
|
| 164 |
-
examples=[
|
| 165 |
-
["examples/face1.png", 0.7, True, True], # Add example files to an 'examples' folder in your Space
|
| 166 |
["examples/face2.png", 0.5, True, True],
|
| 167 |
["examples/bg1.png", 0.8, True, False],
|
| 168 |
-
]
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# --- Launch the App ---
|
| 173 |
if __name__ == "__main__":
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# Updated: 2025-04-05 18:43:16 IST (Ludhiana, Punjab, India)
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
import cv2
|
|
|
|
| 8 |
import os
|
| 9 |
import time
|
| 10 |
import warnings
|
| 11 |
+
import traceback # Import traceback for detailed error printing
|
| 12 |
+
|
| 13 |
+
print("--- Script Start ---")
|
| 14 |
+
print(f"Current Time (IST): {time.strftime('%Y-%m-%d %H:%M:%S')}")
|
| 15 |
|
| 16 |
# Suppress specific warnings or all warnings if needed
|
| 17 |
warnings.filterwarnings("ignore")
|
| 18 |
|
| 19 |
+
# --- Globals ---
|
| 20 |
+
codeformer_net = None
|
| 21 |
+
is_initialized = False
|
| 22 |
+
init_error_message = ""
|
| 23 |
+
|
| 24 |
+
# --- Attempt to Import CodeFormer ---
|
| 25 |
try:
|
| 26 |
from codeformer import CodeFormer
|
| 27 |
+
print("Successfully imported CodeFormer.")
|
| 28 |
+
except ImportError as e:
|
| 29 |
+
init_error_message = f"Error: CodeFormer package not found or import failed. Check requirements.txt installation. Details: {e}"
|
| 30 |
+
print(init_error_message)
|
| 31 |
+
# Keep script running so Gradio might load with an error message
|
| 32 |
+
except Exception as e:
|
| 33 |
+
init_error_message = f"An unexpected error occurred during import: {e}\n{traceback.format_exc()}"
|
| 34 |
+
print(init_error_message)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# --- Initialize Model (only if import succeeded) ---
|
| 38 |
+
if 'CodeFormer' in globals(): # Check if import was successful
|
| 39 |
+
print("Attempting to initialize CodeFormer model...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
+
# Use CPU explicitly
|
| 42 |
+
device = torch.device("cpu")
|
| 43 |
+
print(f"Using device: {device}")
|
| 44 |
+
|
| 45 |
+
# Initialize CodeFormer.
|
| 46 |
+
# Relying on the package to handle pretrained weight download/loading.
|
| 47 |
+
# Common parameters: bg_upsampler='realesrgan', face_upsample=True
|
| 48 |
+
# These details might depend on the exact version of the 'codeformer' pip package.
|
| 49 |
+
codeformer_net = CodeFormer(
|
| 50 |
+
dim_embd=512,
|
| 51 |
+
codebook_size=1024,
|
| 52 |
+
n_head=8,
|
| 53 |
+
n_layers=9,
|
| 54 |
+
connect_list=['32', '64', '128', '256']
|
| 55 |
+
# Add other necessary parameters based on the package's specific API if needed
|
| 56 |
+
).to(device)
|
| 57 |
+
|
| 58 |
+
# Load pretrained weights - The package *should* ideally handle this,
|
| 59 |
+
# either during init or via a specific method. If this implicit loading fails,
|
| 60 |
+
# you might need to investigate the specific package's API for loading weights.
|
| 61 |
+
# We are assuming here the class instantiation handles it or makes weights available.
|
| 62 |
+
# Let's skip manual torch.load unless proven necessary by failure.
|
| 63 |
+
|
| 64 |
+
codeformer_net.eval()
|
| 65 |
+
is_initialized = True
|
| 66 |
+
print("CodeFormer model initialized successfully.")
|
| 67 |
+
|
| 68 |
+
except FileNotFoundError as e:
|
| 69 |
+
init_error_message = f"Error: Could not find CodeFormer model weights. The package might have failed to download them automatically, or they are expected at a specific path. Check package docs and Space logs. Details: {e}\n{traceback.format_exc()}"
|
| 70 |
+
print(init_error_message)
|
| 71 |
+
codeformer_net = None # Ensure model is None if init fails
|
| 72 |
except Exception as e:
|
| 73 |
+
init_error_message = f"Error initializing CodeFormer model: {e}\n{traceback.format_exc()}"
|
| 74 |
+
print(init_error_message)
|
| 75 |
+
codeformer_net = None # Ensure model is None if init fails
|
| 76 |
+
else:
|
| 77 |
+
# If import failed, ensure message reflects that
|
| 78 |
+
if not init_error_message: # Safety net if import error wasn't captured somehow
|
| 79 |
+
init_error_message = "CodeFormer could not be imported. Cannot initialize model."
|
| 80 |
+
print("Skipping model initialization due to import failure.")
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
# --- Processing Function ---
|
| 84 |
def enhance_image(input_img, fidelity_weight, background_enhance, face_upsample):
|
|
|
|
| 90 |
background_enhance (bool): Whether to enhance background using RealESRGAN.
|
| 91 |
face_upsample (bool): Whether to further upsample restored faces.
|
| 92 |
Returns:
|
| 93 |
+
np.ndarray: Enhanced image (RGB format) or None on error.
|
| 94 |
+
str: Status or processing time message.
|
| 95 |
"""
|
| 96 |
+
print("--- enhance_image function called ---") # Log function entry
|
| 97 |
+
|
| 98 |
+
if not is_initialized or codeformer_net is None:
|
| 99 |
+
error_msg = f"ERROR: CodeFormer model is not available. Initialization failed. Check logs for details. Message: {init_error_message}"
|
| 100 |
+
print(error_msg)
|
| 101 |
+
# Return None for the image and the error message for the status textbox
|
| 102 |
+
return None, error_msg
|
| 103 |
+
|
| 104 |
if input_img is None:
|
| 105 |
+
print("Error: No input image provided.")
|
| 106 |
return None, "Error: No input image provided."
|
| 107 |
|
| 108 |
print(f"Processing image with fidelity: {fidelity_weight}, bg_enhance: {background_enhance}, face_upsample: {face_upsample}")
|
| 109 |
start_time = time.time()
|
| 110 |
|
| 111 |
try:
|
| 112 |
+
# 1. Convert RGB (from Gradio) to BGR (often expected by OpenCV/CodeFormer backend)
|
| 113 |
img_bgr = cv2.cvtColor(input_img, cv2.COLOR_RGB2BGR)
|
| 114 |
+
print("Input image converted from RGB to BGR.")
|
| 115 |
+
|
| 116 |
+
# 2. Select background upsampler based on checkbox
|
| 117 |
+
bg_upsampler = 'realesrgan' if background_enhance else None
|
| 118 |
+
print(f"Background upsampler selected: {bg_upsampler}")
|
| 119 |
|
| 120 |
+
# 3. Run CodeFormer enhancement
|
| 121 |
+
# Ensure parameters match the CodeFormer API you have installed.
|
| 122 |
+
# `w` corresponds to fidelity_weight.
|
| 123 |
+
print("Starting CodeFormer enhancement...")
|
|
|
|
|
|
|
| 124 |
with torch.no_grad():
|
| 125 |
output_bgr, _, _ = codeformer_net.enhance(
|
| 126 |
img_bgr,
|
| 127 |
w=fidelity_weight,
|
| 128 |
+
adain=True, # AdaIN is commonly used
|
| 129 |
face_upsample=face_upsample,
|
| 130 |
+
bg_upsampler=bg_upsampler
|
| 131 |
)
|
| 132 |
+
print("CodeFormer enhancement finished.")
|
| 133 |
|
| 134 |
+
# 4. Convert BGR output back to RGB for Gradio display
|
| 135 |
output_rgb = cv2.cvtColor(output_bgr, cv2.COLOR_BGR2RGB)
|
| 136 |
+
print("Output image converted from BGR to RGB.")
|
| 137 |
|
| 138 |
end_time = time.time()
|
| 139 |
processing_time = end_time - start_time
|
|
|
|
| 142 |
return output_rgb, time_msg
|
| 143 |
|
| 144 |
except Exception as e:
|
| 145 |
+
# Catch errors during the enhancement process
|
| 146 |
+
error_details = f"Error during enhancement: {e}\n{traceback.format_exc()}"
|
| 147 |
+
print(error_details)
|
| 148 |
+
return None, error_details # Return error message to the status textbox
|
| 149 |
|
| 150 |
+
# --- Gradio Interface Definition ---
|
| 151 |
title = "CodeFormer Image Enhancement (CPU Demo)"
|
| 152 |
+
description = f"""
|
| 153 |
Upload an image to enhance its quality, particularly for faces, using CodeFormer.
|
| 154 |
**Note:** This demo runs on a free Hugging Face CPU. Processing will be **SLOW** (expect seconds to minutes per image).
|
| 155 |
Adjust the fidelity weight (0 = max quality enhancement, 1 = closer to original). Optionally enhance background and upsample faces.
|
| 156 |
+
**Status:** {'Model Loaded Successfully.' if is_initialized else f'Model Load FAILED: {init_error_message}'} Check Logs for details if failed.
|
| 157 |
"""
|
| 158 |
article = "<p style='text-align: center'>CodeFormer CPU Demo | <a href='https://github.com/sczhou/CodeFormer' target='_blank'>Official Repo</a></p>"
|
| 159 |
|
| 160 |
+
# Define examples (Make sure the 'examples' folder and files exist in your Space repo)
|
| 161 |
+
example_list = []
|
| 162 |
+
if os.path.exists("examples"):
|
| 163 |
+
example_list = [
|
| 164 |
+
["examples/face1.png", 0.7, True, True],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
["examples/face2.png", 0.5, True, True],
|
| 166 |
["examples/bg1.png", 0.8, True, False],
|
| 167 |
+
]
|
| 168 |
+
print("Example files found.")
|
| 169 |
+
else:
|
| 170 |
+
print("Note: 'examples' folder not found. Gradio examples will be empty.")
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
print("Defining Gradio interface...")
|
| 174 |
+
try:
|
| 175 |
+
iface = gr.Interface(
|
| 176 |
+
fn=enhance_image,
|
| 177 |
+
inputs=[
|
| 178 |
+
gr.Image(label="Upload Image", type="numpy"),
|
| 179 |
+
gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.7, label="Fidelity Weight (0 = Max Quality, 1 = Max Fidelity)"),
|
| 180 |
+
gr.Checkbox(label="Enhance Background (Uses RealESRGAN)", value=True),
|
| 181 |
+
gr.Checkbox(label="Upsample Restored Faces", value=True)
|
| 182 |
+
],
|
| 183 |
+
outputs=[
|
| 184 |
+
gr.Image(label="Enhanced Image", type="numpy"),
|
| 185 |
+
gr.Textbox(label="Status / Processing Time") # Output for status messages and time
|
| 186 |
+
],
|
| 187 |
+
title=title,
|
| 188 |
+
description=description,
|
| 189 |
+
article=article,
|
| 190 |
+
examples=example_list,
|
| 191 |
+
allow_flagging="never"
|
| 192 |
+
)
|
| 193 |
+
print("Gradio interface defined successfully.")
|
| 194 |
+
except Exception as e:
|
| 195 |
+
print(f"FATAL: Failed to define Gradio interface: {e}\n{traceback.format_exc()}")
|
| 196 |
+
# If interface definition fails, we can't launch.
|
| 197 |
+
# Raising an error might provide more info in logs, or just print and exit.
|
| 198 |
+
raise RuntimeError("Could not create Gradio Interface.") from e
|
| 199 |
+
|
| 200 |
|
| 201 |
# --- Launch the App ---
|
| 202 |
if __name__ == "__main__":
|
| 203 |
+
print("Attempting to launch Gradio app...")
|
| 204 |
+
try:
|
| 205 |
+
iface.launch()
|
| 206 |
+
print("Gradio app launched successfully.")
|
| 207 |
+
print("--- Script End (App Running) ---")
|
| 208 |
+
except Exception as e:
|
| 209 |
+
print(f"FATAL: Failed to launch Gradio interface: {e}\n{traceback.format_exc()}")
|
| 210 |
+
# Exit if launch fails. Logs should show the error.
|
| 211 |
+
exit(1)
|
| 212 |
|