Janeka commited on
Commit
fe31561
·
verified ·
1 Parent(s): 12c1ac0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -6,15 +6,7 @@ import numpy as np
6
 
7
  def remove_bg(image):
8
  try:
9
- # Convert input to PIL Image
10
- if isinstance(image, str): # file path
11
- img = Image.open(image)
12
- elif isinstance(image, np.ndarray): # numpy array from Gradio
13
- img = Image.fromarray(image)
14
- else:
15
- raise ValueError("Unsupported input type")
16
-
17
- # Process with rembg
18
  img_byte_arr = io.BytesIO()
19
  img.save(img_byte_arr, format='PNG')
20
  output_bytes = remove(img_byte_arr.getvalue())
@@ -22,16 +14,16 @@ def remove_bg(image):
22
  except Exception as e:
23
  raise gr.Error(f"Error processing image: {str(e)}")
24
 
25
- # Create interface with correct flagging mode
26
- iface = gr.Interface(
27
- fn=remove_bg,
28
- inputs=gr.Image(type="filepath", label="Input Image"),
29
- outputs=gr.Image(label="Output Image", type="pil"),
30
- title="Background Remover",
31
- description="Upload an image to remove its background",
32
- flagging_mode="never" # Correct way to disable flagging
33
- )
 
34
 
35
- # Launch the app (API is automatically enabled in newer Gradio versions)
36
  if __name__ == "__main__":
37
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
6
 
7
  def remove_bg(image):
8
  try:
9
+ img = Image.open(image) if isinstance(image, str) else Image.fromarray(image)
 
 
 
 
 
 
 
 
10
  img_byte_arr = io.BytesIO()
11
  img.save(img_byte_arr, format='PNG')
12
  output_bytes = remove(img_byte_arr.getvalue())
 
14
  except Exception as e:
15
  raise gr.Error(f"Error processing image: {str(e)}")
16
 
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown("# Background Remover")
19
+ with gr.Row():
20
+ with gr.Column():
21
+ input_image = gr.Image(type="filepath", label="Input Image")
22
+ submit_btn = gr.Button("Remove Background")
23
+ with gr.Column():
24
+ output_image = gr.Image(label="Output Image", type="pil")
25
+
26
+ submit_btn.click(remove_bg, inputs=input_image, outputs=output_image)
27
 
 
28
  if __name__ == "__main__":
29
+ demo.launch(server_name="0.0.0.0", server_port=7860)