|
@@ -11,18 +11,32 @@ from ..bg import remove
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
[email protected]('/', methods=['GET', 'POST'])
|
|
|
def index():
|
|
|
model = request.args.get("model", type=str, default="u2net")
|
|
|
if model not in ("u2net", "u2netp"):
|
|
|
return {"error": "invalid query param 'model'"}, 400
|
|
|
|
|
|
- url = request.args.get("url", type=str)
|
|
|
- if url is None:
|
|
|
- return {"error": "missing query param 'url'"}, 400
|
|
|
+ file_content = ''
|
|
|
+ if request.method == 'POST':
|
|
|
+ if 'file' not in request.files:
|
|
|
+ return {"error": "missing post form param 'file'"}, 400
|
|
|
+
|
|
|
+ file_content = request.files['file'].read();
|
|
|
+
|
|
|
+ if request.method == 'GET':
|
|
|
+ url = request.args.get("url", type=str)
|
|
|
+ if url is None:
|
|
|
+ return {"error": "missing query param 'url'"}, 400
|
|
|
+
|
|
|
+ file_content = urlopen(unquote_plus(url)).read();
|
|
|
+
|
|
|
+ if file_content == '':
|
|
|
+ return {"error": "File content is empty"}, 400
|
|
|
|
|
|
try:
|
|
|
return send_file(
|
|
|
- BytesIO(remove(urlopen(unquote_plus(url)).read(), model)),
|
|
|
+ BytesIO(remove(file_content, model)),
|
|
|
mimetype="image/png",
|
|
|
)
|
|
|
except Exception as e:
|