mike23415 commited on
Commit
dc264e4
·
verified ·
1 Parent(s): d07bca2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -5,33 +5,29 @@ from PIL import Image
5
  import io
6
 
7
  app = Flask(__name__)
8
- CORS(app) # Enable CORS for all routes
 
 
 
 
 
9
 
10
  @app.route('/remove_bg', methods=['POST'])
11
  def remove_bg():
12
- # Check if image file is present
13
  if 'file' not in request.files:
14
  return {'error': 'No file uploaded'}, 400
15
 
16
  file = request.files['file']
17
 
18
- # Check if file is an image
19
- if file.filename == '':
20
- return {'error': 'No selected file'}, 400
21
-
22
  try:
23
- # Read image file
24
  input_image = Image.open(file.stream)
 
 
25
 
26
- # Remove background
27
- output_image = remove(input_image)
28
-
29
- # Convert to bytes
30
  img_byte_arr = io.BytesIO()
31
  output_image.save(img_byte_arr, format='PNG')
32
  img_byte_arr.seek(0)
33
 
34
- # Return result
35
  return send_file(img_byte_arr, mimetype='image/png')
36
 
37
  except Exception as e:
 
5
  import io
6
 
7
  app = Flask(__name__)
8
+ CORS(app)
9
+
10
+ # Health check endpoint
11
+ @app.route('/health')
12
+ def health_check():
13
+ return {'status': 'ready'}, 200
14
 
15
  @app.route('/remove_bg', methods=['POST'])
16
  def remove_bg():
 
17
  if 'file' not in request.files:
18
  return {'error': 'No file uploaded'}, 400
19
 
20
  file = request.files['file']
21
 
 
 
 
 
22
  try:
 
23
  input_image = Image.open(file.stream)
24
+ # Use specific model and only necessary parameters
25
+ output_image = remove(input_image, session_new=remove.new_session('u2net'))
26
 
 
 
 
 
27
  img_byte_arr = io.BytesIO()
28
  output_image.save(img_byte_arr, format='PNG')
29
  img_byte_arr.seek(0)
30
 
 
31
  return send_file(img_byte_arr, mimetype='image/png')
32
 
33
  except Exception as e: