b2bomber commited on
Commit
94cf2c3
·
verified ·
1 Parent(s): 11eb0e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -38
app.py CHANGED
@@ -1,53 +1,31 @@
1
  # app.py
2
- # FINAL STABLE VERSION using gr.Interface and an explicit api_name
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. Please upload an image.")
24
-
25
  try:
26
- # Use the default, stable rembg model
27
- output_image = remove(input_image)
28
- return output_image
29
-
30
  except Exception as e:
31
- print(traceback.format_exc())
32
- raise gr.Error(f"Failed to process image. Error: {e}")
33
 
34
- # --- Gradio Interface Definition ---
 
35
  demo = gr.Interface(
36
  fn=process_image,
37
- inputs=[
38
- gr.Image(type="pil", label="Upload Your Image"),
39
- gr.Textbox(label="Secret Key", type="password", placeholder="API Key Required")
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
- # This explicitly creates the API endpoint at /run/predict
48
- api_name="predict"
49
  )
50
 
51
- # --- Launch the App ---
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()