Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# app.py
|
| 2 |
-
# Updated: 2025-04-05 18:
|
| 3 |
-
# Fix:
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
|
@@ -37,34 +38,44 @@ except Exception as e:
|
|
| 37 |
|
| 38 |
# --- Initialize Model (only if import succeeded) ---
|
| 39 |
if 'CodeFormer' in globals(): # Check if import was successful
|
| 40 |
-
print("Attempting to initialize CodeFormer model
|
| 41 |
try:
|
| 42 |
-
#
|
| 43 |
device = torch.device("cpu")
|
| 44 |
-
print(f"
|
| 45 |
|
| 46 |
# *** MODIFIED LINE BELOW ***
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
codeformer_net.eval()
|
| 54 |
is_initialized = True
|
| 55 |
-
print("CodeFormer model
|
| 56 |
|
| 57 |
except FileNotFoundError as e:
|
| 58 |
-
|
|
|
|
| 59 |
print(init_error_message)
|
| 60 |
codeformer_net = None # Ensure model is None if init fails
|
| 61 |
except TypeError as e:
|
| 62 |
# Catch TypeErrors specifically from the init call
|
| 63 |
-
init_error_message = f"TypeError during CodeFormer init. The constructor signature might be
|
| 64 |
print(init_error_message)
|
| 65 |
codeformer_net = None
|
| 66 |
except Exception as e:
|
| 67 |
-
|
|
|
|
| 68 |
print(init_error_message)
|
| 69 |
codeformer_net = None # Ensure model is None if init fails
|
| 70 |
else:
|
|
@@ -115,9 +126,8 @@ def enhance_image(input_img, fidelity_weight, background_enhance, face_upsample)
|
|
| 115 |
# Ensure parameters match the CodeFormer API you have installed.
|
| 116 |
# `w` corresponds to fidelity_weight.
|
| 117 |
print("Starting CodeFormer enhancement...")
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
# Assuming enhance(image, w, face_upsample, bg_upsampler, adain=True) is correct
|
| 121 |
output_bgr, _, _ = codeformer_net.enhance(
|
| 122 |
img_bgr,
|
| 123 |
w=fidelity_weight,
|
|
@@ -146,7 +156,7 @@ def enhance_image(input_img, fidelity_weight, background_enhance, face_upsample)
|
|
| 146 |
# --- Gradio Interface Definition ---
|
| 147 |
title = "CodeFormer Image Enhancement (CPU Demo)"
|
| 148 |
# Dynamically update description based on model load status
|
| 149 |
-
status_message = 'Model
|
| 150 |
description = f"""
|
| 151 |
Upload an image to enhance its quality, particularly for faces, using CodeFormer.
|
| 152 |
**Note:** This demo runs on a free Hugging Face CPU. Processing will be **SLOW** (expect seconds to minutes per image).
|
|
@@ -206,3 +216,4 @@ if __name__ == "__main__":
|
|
| 206 |
print(f"FATAL: Failed to launch Gradio interface: {e}\n{traceback.format_exc()}")
|
| 207 |
# Exit if launch fails. Logs should show the error.
|
| 208 |
exit(1)
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# Updated: 2025-04-05 18:56:50 PM IST (Ludhiana, Punjab, India)
|
| 3 |
+
# Fix: Removed .to(device) call after CodeFormer() initialization due to AttributeError.
|
| 4 |
+
# Added check for .eval() method.
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
|
|
|
| 38 |
|
| 39 |
# --- Initialize Model (only if import succeeded) ---
|
| 40 |
if 'CodeFormer' in globals(): # Check if import was successful
|
| 41 |
+
print("Attempting to initialize CodeFormer model (simplified init, no .to(device))...")
|
| 42 |
try:
|
| 43 |
+
# Define the target device (though we won't call .to() on the model object)
|
| 44 |
device = torch.device("cpu")
|
| 45 |
+
print(f"Target device check: {device}")
|
| 46 |
|
| 47 |
# *** MODIFIED LINE BELOW ***
|
| 48 |
+
# Initialize WITHOUT .to(device)
|
| 49 |
+
codeformer_net = CodeFormer()
|
| 50 |
+
print("CodeFormer() object created.")
|
| 51 |
+
|
| 52 |
+
# Check if the object has an eval method (common for models)
|
| 53 |
+
# If it doesn't, it might not be a standard torch.nn.Module
|
| 54 |
+
try:
|
| 55 |
+
codeformer_net.eval()
|
| 56 |
+
print("codeformer_net.eval() called successfully.")
|
| 57 |
+
except AttributeError:
|
| 58 |
+
print("Note: codeformer_net object does not have an eval() method. Assuming it's not needed or handled internally.")
|
| 59 |
+
except Exception as e_eval:
|
| 60 |
+
print(f"Warning: Calling codeformer_net.eval() failed: {e_eval}")
|
| 61 |
+
|
| 62 |
|
|
|
|
| 63 |
is_initialized = True
|
| 64 |
+
print("CodeFormer model object initialization appears successful (removed .to(device)).")
|
| 65 |
|
| 66 |
except FileNotFoundError as e:
|
| 67 |
+
# This error might occur if CodeFormer() tries to load weights internally and fails
|
| 68 |
+
init_error_message = f"Error: Could not find CodeFormer model weights during init. The package might have failed to download them automatically. Check logs. Details: {e}\n{traceback.format_exc()}"
|
| 69 |
print(init_error_message)
|
| 70 |
codeformer_net = None # Ensure model is None if init fails
|
| 71 |
except TypeError as e:
|
| 72 |
# Catch TypeErrors specifically from the init call
|
| 73 |
+
init_error_message = f"TypeError during CodeFormer init. The constructor signature might be wrong. Details: {e}\n{traceback.format_exc()}"
|
| 74 |
print(init_error_message)
|
| 75 |
codeformer_net = None
|
| 76 |
except Exception as e:
|
| 77 |
+
# Catch any other initialization errors
|
| 78 |
+
init_error_message = f"Error initializing CodeFormer model object: {e}\n{traceback.format_exc()}"
|
| 79 |
print(init_error_message)
|
| 80 |
codeformer_net = None # Ensure model is None if init fails
|
| 81 |
else:
|
|
|
|
| 126 |
# Ensure parameters match the CodeFormer API you have installed.
|
| 127 |
# `w` corresponds to fidelity_weight.
|
| 128 |
print("Starting CodeFormer enhancement...")
|
| 129 |
+
# *** Assuming enhance method exists and works on CPU by default ***
|
| 130 |
+
with torch.no_grad(): # Keep torch.no_grad() as internal ops might use Torch
|
|
|
|
| 131 |
output_bgr, _, _ = codeformer_net.enhance(
|
| 132 |
img_bgr,
|
| 133 |
w=fidelity_weight,
|
|
|
|
| 156 |
# --- Gradio Interface Definition ---
|
| 157 |
title = "CodeFormer Image Enhancement (CPU Demo)"
|
| 158 |
# Dynamically update description based on model load status
|
| 159 |
+
status_message = 'Model initialization appears successful.' if is_initialized else f'Model Load FAILED: {init_error_message}'
|
| 160 |
description = f"""
|
| 161 |
Upload an image to enhance its quality, particularly for faces, using CodeFormer.
|
| 162 |
**Note:** This demo runs on a free Hugging Face CPU. Processing will be **SLOW** (expect seconds to minutes per image).
|
|
|
|
| 216 |
print(f"FATAL: Failed to launch Gradio interface: {e}\n{traceback.format_exc()}")
|
| 217 |
# Exit if launch fails. Logs should show the error.
|
| 218 |
exit(1)
|
| 219 |
+
|