-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57d95d8
commit 2bc9b90
Showing
5 changed files
with
35 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,48 @@ | ||
import os | ||
import threading | ||
import argparse | ||
import getpass | ||
|
||
from flask import Flask | ||
from flask_cors import CORS | ||
from flask_ngrok import run_with_ngrok | ||
from flask import Flask | ||
from pyngrok import ngrok, conf | ||
|
||
from backend.routes import set_routes | ||
from backend.constants import UPLOAD_FOLDER, CSV_FOLDER, DETECTION_FOLDER, SEGMENTATION_FOLDER, METADATA_FOLDER | ||
|
||
parser = argparse.ArgumentParser('Online Food Recognition') | ||
parser.add_argument('--ngrok', action='store_true', | ||
default=False, help="Run on local or ngrok") | ||
parser.add_argument('--host', type=str, | ||
default='localhost:8000', help="Local IP") | ||
parser.add_argument('--host', type=str, default='localhost', help="Local IP") | ||
parser.add_argument('--port', type=int, default=5000, help="Local port") | ||
parser.add_argument('--debug', action='store_true', | ||
default=False, help="Run app in debug mode") | ||
|
||
|
||
app = Flask(__name__, template_folder='templates', static_folder='static') | ||
CORS(app, resources={r"/api/*": {"origins": "*"}}) | ||
|
||
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1 | ||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | ||
app.config['CSV_FOLDER'] = CSV_FOLDER | ||
app.config['DETECTION_FOLDER'] = DETECTION_FOLDER | ||
app.config['SEGMENTATION_FOLDER'] = SEGMENTATION_FOLDER | ||
|
||
set_routes(app) | ||
args = parser.parse_args() | ||
|
||
if __name__ == '__main__': | ||
if not os.path.exists(UPLOAD_FOLDER): | ||
os.makedirs(UPLOAD_FOLDER, exist_ok=True) | ||
if not os.path.exists(DETECTION_FOLDER): | ||
os.makedirs(DETECTION_FOLDER, exist_ok=True) | ||
if not os.path.exists(SEGMENTATION_FOLDER): | ||
os.makedirs(SEGMENTATION_FOLDER, exist_ok=True) | ||
if not os.path.exists(CSV_FOLDER): | ||
os.makedirs(CSV_FOLDER, exist_ok=True) | ||
if not os.path.exists(METADATA_FOLDER): | ||
os.makedirs(METADATA_FOLDER, exist_ok=True) | ||
|
||
args = parser.parse_args() | ||
app = Flask(__name__, template_folder='templates', static_folder='static') | ||
|
||
CORS(app, resources={r"/api/*": {"origins": "*"}}) | ||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | ||
app.config['CSV_FOLDER'] = CSV_FOLDER | ||
app.config['DETECTION_FOLDER'] = DETECTION_FOLDER | ||
app.config['SEGMENTATION_FOLDER'] = SEGMENTATION_FOLDER | ||
|
||
if args.ngrok: | ||
run_with_ngrok(app) | ||
app.run() | ||
print("Enter your authtoken, which can be copied from https://dashboard.ngrok.com/get-started/your-authtoken") | ||
conf.get_default().auth_token = getpass.getpass() | ||
public_url = ngrok.connect(args.port).public_url | ||
print( | ||
f" * ngrok tunnel \"{public_url}\" -> \"http://127.0.0.1:{args.port}/\"") | ||
app.config['BASE_URL'] = public_url | ||
else: | ||
hostname = str.split(args.host, ':') | ||
if len(hostname) == 1: | ||
port = 4000 | ||
else: | ||
port = hostname[1] | ||
host = hostname[0] | ||
|
||
app.run(host=host, port=port, debug=args.debug, use_reloader=False, | ||
ssl_context='adhoc') | ||
app.config['BASE_URL'] = f"http://{args.host}:{args.port}" | ||
|
||
set_routes(app) | ||
|
||
for folder in [UPLOAD_FOLDER, DETECTION_FOLDER, SEGMENTATION_FOLDER, CSV_FOLDER, METADATA_FOLDER]: | ||
os.makedirs(folder, exist_ok=True) | ||
|
||
threading.Thread(target=app.run, kwargs={ | ||
"host": args.host, "port": args.port, "debug": args.debug, "use_reloader": False}).start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters