Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,31 @@
|
|
| 1 |
# app.py
|
| 2 |
-
#
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from rembg import remove
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import os
|
| 8 |
-
import traceback
|
| 9 |
-
|
| 10 |
-
# --- Configuration ---
|
| 11 |
-
MY_SECRET_KEY = os.environ.get("MY_SECRET_KEY", "Mbdh837hdjnfjsfujfeifjjweiojfe")
|
| 12 |
-
|
| 13 |
-
# --- Core Function ---
|
| 14 |
-
def process_image(input_image, secret_key):
|
| 15 |
-
"""
|
| 16 |
-
The main function that processes the image.
|
| 17 |
-
"""
|
| 18 |
-
# Security Check
|
| 19 |
-
if secret_key != MY_SECRET_KEY:
|
| 20 |
-
raise gr.Error("Access Denied: The secret key is incorrect.")
|
| 21 |
|
|
|
|
|
|
|
| 22 |
if input_image is None:
|
| 23 |
-
raise gr.Error("No image provided.
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
-
# Use the default
|
| 27 |
-
|
| 28 |
-
return output_image
|
| 29 |
-
|
| 30 |
except Exception as e:
|
| 31 |
-
|
| 32 |
-
raise gr.Error(f"Failed to process image. Error: {e}")
|
| 33 |
|
| 34 |
-
#
|
|
|
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=process_image,
|
| 37 |
-
inputs=
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
outputs=[
|
| 42 |
-
gr.Image(type="pil", label="Background Removed")
|
| 43 |
-
],
|
| 44 |
-
title="Avinya.Space Background Remover API",
|
| 45 |
-
description="This is the API backend for Avinya.Space. It is not intended for direct use.",
|
| 46 |
allow_flagging="never",
|
| 47 |
-
|
| 48 |
-
api_name="predict"
|
| 49 |
)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
if __name__ == "__main__":
|
| 53 |
-
demo.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# A simple and reliable Gradio interface for background removal.
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
from rembg import remove
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# The core function that performs the background removal.
|
| 8 |
+
def process_image(input_image):
|
| 9 |
if input_image is None:
|
| 10 |
+
raise gr.Error("No image provided.")
|
|
|
|
| 11 |
try:
|
| 12 |
+
# Use the default rembg model for stability.
|
| 13 |
+
return remove(input_image)
|
|
|
|
|
|
|
| 14 |
except Exception as e:
|
| 15 |
+
raise gr.Error(f"Failed to process image: {e}")
|
|
|
|
| 16 |
|
| 17 |
+
# Create the Gradio interface.
|
| 18 |
+
# The `api_name="predict"` explicitly creates an endpoint at `/run/predict`.
|
| 19 |
demo = gr.Interface(
|
| 20 |
fn=process_image,
|
| 21 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 22 |
+
outputs=gr.Image(type="pil", label="Background Removed"),
|
| 23 |
+
title="Background Remover API",
|
| 24 |
+
description="A simple API for removing image backgrounds.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
allow_flagging="never",
|
| 26 |
+
api_name="predict"
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# Launch the app.
|
| 30 |
if __name__ == "__main__":
|
| 31 |
+
demo.launch()
|