Minor changes to return two outputs to fix error
Browse files
app.py
CHANGED
|
@@ -73,6 +73,13 @@ def hex_to_rgba(hex_color):
|
|
| 73 |
hex_color += 'FF' # Add full opacity if no alpha is provided
|
| 74 |
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4, 6))
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
def remove_background(input_path, bg_color=None, transparent_bg=True, model_choice="", alpha_matting=False, post_process_mask=False, only_mask=False):
|
| 77 |
print(f"DEBUG: Function called with input_path: {input_path}")
|
| 78 |
print(f"DEBUG: Parameters - bg_color: {bg_color}, transparent_bg: {transparent_bg}, model_choice: {model_choice}")
|
|
@@ -81,7 +88,7 @@ def remove_background(input_path, bg_color=None, transparent_bg=True, model_choi
|
|
| 81 |
# Check if input path is valid
|
| 82 |
if not input_path or not os.path.exists(input_path):
|
| 83 |
print(f"ERROR: Invalid input path: {input_path}")
|
| 84 |
-
return None
|
| 85 |
|
| 86 |
print("DEBUG: Starting background removal...")
|
| 87 |
|
|
@@ -173,17 +180,17 @@ def remove_background(input_path, bg_color=None, transparent_bg=True, model_choi
|
|
| 173 |
if os.path.exists(output_path):
|
| 174 |
file_size = os.path.getsize(output_path)
|
| 175 |
print(f"DEBUG: Output file created successfully, size: {file_size} bytes")
|
| 176 |
-
return output_path
|
| 177 |
else:
|
| 178 |
print("ERROR: Output file was not created")
|
| 179 |
-
return None
|
| 180 |
|
| 181 |
except Exception as e:
|
| 182 |
print(f"ERROR: Exception occurred: {str(e)}")
|
| 183 |
import traceback
|
| 184 |
traceback.print_exc()
|
| 185 |
logging.error(f"An error occurred: {e}")
|
| 186 |
-
return None
|
| 187 |
|
| 188 |
# Fixed examples with proper values for all inputs
|
| 189 |
examples = [
|
|
@@ -198,12 +205,6 @@ examples = [
|
|
| 198 |
]
|
| 199 |
]
|
| 200 |
|
| 201 |
-
bmc_button = """
|
| 202 |
-
<a href="https://coff.ee/rdjarbeng" target="_blank">
|
| 203 |
-
<img src="https://cdn.buymeacoffee.com/buttons/v2/default-orange.png" alt="Buy Me a Coffee" style="height: 41px; width: 174px;">
|
| 204 |
-
</a>
|
| 205 |
-
"""
|
| 206 |
-
|
| 207 |
# Gradio interface
|
| 208 |
iface = gr.Interface(
|
| 209 |
fn=remove_background,
|
|
@@ -214,7 +215,7 @@ iface = gr.Interface(
|
|
| 214 |
gr.Dropdown(
|
| 215 |
choices=[""] + [f"{k} | {v}" for k, v in MODEL_OPTIONS.items() if k != ""],
|
| 216 |
label="Model Selection",
|
| 217 |
-
value="",
|
| 218 |
allow_custom_value=False
|
| 219 |
),
|
| 220 |
gr.Checkbox(label="Enable Alpha Matting", value=False),
|
|
@@ -223,7 +224,7 @@ iface = gr.Interface(
|
|
| 223 |
],
|
| 224 |
outputs=[
|
| 225 |
gr.Image(type="filepath", label="Output Image (PNG)"),
|
| 226 |
-
gr.HTML(
|
| 227 |
],
|
| 228 |
examples=examples,
|
| 229 |
title="Background Remover v2.9",
|
|
|
|
| 73 |
hex_color += 'FF' # Add full opacity if no alpha is provided
|
| 74 |
return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4, 6))
|
| 75 |
|
| 76 |
+
# Define the Buy Me a Coffee button HTML (moved inside the function for consistency)
|
| 77 |
+
bmc_button = """
|
| 78 |
+
<a href="https://coff.ee/rdjarbeng" target="_blank">
|
| 79 |
+
<img src="https://cdn.buymeacoffee.com/buttons/v2/default-orange.png" alt="Buy Me a Coffee" style="height: 41px; width: 174px;">
|
| 80 |
+
</a>
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
def remove_background(input_path, bg_color=None, transparent_bg=True, model_choice="", alpha_matting=False, post_process_mask=False, only_mask=False):
|
| 84 |
print(f"DEBUG: Function called with input_path: {input_path}")
|
| 85 |
print(f"DEBUG: Parameters - bg_color: {bg_color}, transparent_bg: {transparent_bg}, model_choice: {model_choice}")
|
|
|
|
| 88 |
# Check if input path is valid
|
| 89 |
if not input_path or not os.path.exists(input_path):
|
| 90 |
print(f"ERROR: Invalid input path: {input_path}")
|
| 91 |
+
return None, bmc_button # Return None for image and the button HTML
|
| 92 |
|
| 93 |
print("DEBUG: Starting background removal...")
|
| 94 |
|
|
|
|
| 180 |
if os.path.exists(output_path):
|
| 181 |
file_size = os.path.getsize(output_path)
|
| 182 |
print(f"DEBUG: Output file created successfully, size: {file_size} bytes")
|
| 183 |
+
return output_path, bmc_button # Return both the image path and the button HTML
|
| 184 |
else:
|
| 185 |
print("ERROR: Output file was not created")
|
| 186 |
+
return None, bmc_button # Return None for image and the button HTML
|
| 187 |
|
| 188 |
except Exception as e:
|
| 189 |
print(f"ERROR: Exception occurred: {str(e)}")
|
| 190 |
import traceback
|
| 191 |
traceback.print_exc()
|
| 192 |
logging.error(f"An error occurred: {e}")
|
| 193 |
+
return None, bmc_button # Return None for image and the button HTML
|
| 194 |
|
| 195 |
# Fixed examples with proper values for all inputs
|
| 196 |
examples = [
|
|
|
|
| 205 |
]
|
| 206 |
]
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
# Gradio interface
|
| 209 |
iface = gr.Interface(
|
| 210 |
fn=remove_background,
|
|
|
|
| 215 |
gr.Dropdown(
|
| 216 |
choices=[""] + [f"{k} | {v}" for k, v in MODEL_OPTIONS.items() if k != ""],
|
| 217 |
label="Model Selection",
|
| 218 |
+
value="",
|
| 219 |
allow_custom_value=False
|
| 220 |
),
|
| 221 |
gr.Checkbox(label="Enable Alpha Matting", value=False),
|
|
|
|
| 224 |
],
|
| 225 |
outputs=[
|
| 226 |
gr.Image(type="filepath", label="Output Image (PNG)"),
|
| 227 |
+
gr.HTML(label="Support This Project") # Added label for clarity
|
| 228 |
],
|
| 229 |
examples=examples,
|
| 230 |
title="Background Remover v2.9",
|