Spaces:
Sleeping
Sleeping
Update app/__init__.py
Browse files- app/__init__.py +77 -77
app/__init__.py
CHANGED
|
@@ -1,77 +1,77 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from flask import Flask, request
|
| 3 |
-
from config import Config
|
| 4 |
-
from ml.loader import load_model
|
| 5 |
-
from app.utils.helper import resource_path
|
| 6 |
-
|
| 7 |
-
import logging
|
| 8 |
-
from logging.handlers import RotatingFileHandler
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def create_app():
|
| 12 |
-
"""Application factory - Create and configures the flask app"""
|
| 13 |
-
|
| 14 |
-
# 1. Creates flask app with correct paths
|
| 15 |
-
app = Flask(__name__,
|
| 16 |
-
template_folder=resource_path('app/templates'),
|
| 17 |
-
static_folder=resource_path('app/static')
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
# Simple Logging Middleware
|
| 21 |
-
if not os.path.exists("logs"):
|
| 22 |
-
os.mkdir("logs")
|
| 23 |
-
|
| 24 |
-
handler = RotatingFileHandler('logs/app.log', maxBytes=100000, backupCount=3)
|
| 25 |
-
handler.setLevel(logging.INFO)
|
| 26 |
-
|
| 27 |
-
formatter = logging.Formatter(
|
| 28 |
-
"%(asctime)s - %(levelname)s - %(message)s"
|
| 29 |
-
)
|
| 30 |
-
handler.setFormatter(formatter)
|
| 31 |
-
|
| 32 |
-
app.logger.setLevel(logging.INFO)
|
| 33 |
-
app.logger.addHandler(handler)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
@app.before_request
|
| 37 |
-
def log_request():
|
| 38 |
-
app.logger.info(
|
| 39 |
-
f"REQUEST: {request.method} {request.path} | IP={request.remote_addr}"
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
@app.after_request
|
| 43 |
-
def log_response(response):
|
| 44 |
-
app.logger.info(
|
| 45 |
-
f"RESPONSE: {response.status} for {request.method} {request.path}"
|
| 46 |
-
)
|
| 47 |
-
return response
|
| 48 |
-
|
| 49 |
-
app.secret_key = os.urandom(24) # Secure key for sessions and flash messages
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# 2. Load configuration
|
| 53 |
-
config = Config()
|
| 54 |
-
app.config["APP_CONFIG"] = config
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
# 3. Load ML Model ONCE
|
| 58 |
-
app.model = load_model(config)
|
| 59 |
-
|
| 60 |
-
if app.model is None:
|
| 61 |
-
print("Error: Model could not be loaded.")
|
| 62 |
-
|
| 63 |
-
# 4. Register Blueprints
|
| 64 |
-
|
| 65 |
-
from app.routes.page_routes import static_bp
|
| 66 |
-
from app.routes.coordinate_routes import coordinate_bp
|
| 67 |
-
from app.routes.predictions_routes import prediction_bp
|
| 68 |
-
|
| 69 |
-
app.register_blueprint(static_bp)
|
| 70 |
-
app.register_blueprint(coordinate_bp)
|
| 71 |
-
app.register_blueprint(prediction_bp)
|
| 72 |
-
|
| 73 |
-
# 5. Return app
|
| 74 |
-
return app
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request
|
| 3 |
+
from app.config import Config
|
| 4 |
+
from ml.loader import load_model
|
| 5 |
+
from app.utils.helper import resource_path
|
| 6 |
+
|
| 7 |
+
import logging
|
| 8 |
+
from logging.handlers import RotatingFileHandler
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def create_app():
|
| 12 |
+
"""Application factory - Create and configures the flask app"""
|
| 13 |
+
|
| 14 |
+
# 1. Creates flask app with correct paths
|
| 15 |
+
app = Flask(__name__,
|
| 16 |
+
template_folder=resource_path('app/templates'),
|
| 17 |
+
static_folder=resource_path('app/static')
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Simple Logging Middleware
|
| 21 |
+
if not os.path.exists("logs"):
|
| 22 |
+
os.mkdir("logs")
|
| 23 |
+
|
| 24 |
+
handler = RotatingFileHandler('logs/app.log', maxBytes=100000, backupCount=3)
|
| 25 |
+
handler.setLevel(logging.INFO)
|
| 26 |
+
|
| 27 |
+
formatter = logging.Formatter(
|
| 28 |
+
"%(asctime)s - %(levelname)s - %(message)s"
|
| 29 |
+
)
|
| 30 |
+
handler.setFormatter(formatter)
|
| 31 |
+
|
| 32 |
+
app.logger.setLevel(logging.INFO)
|
| 33 |
+
app.logger.addHandler(handler)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@app.before_request
|
| 37 |
+
def log_request():
|
| 38 |
+
app.logger.info(
|
| 39 |
+
f"REQUEST: {request.method} {request.path} | IP={request.remote_addr}"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
@app.after_request
|
| 43 |
+
def log_response(response):
|
| 44 |
+
app.logger.info(
|
| 45 |
+
f"RESPONSE: {response.status} for {request.method} {request.path}"
|
| 46 |
+
)
|
| 47 |
+
return response
|
| 48 |
+
|
| 49 |
+
app.secret_key = os.urandom(24) # Secure key for sessions and flash messages
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# 2. Load configuration
|
| 53 |
+
config = Config()
|
| 54 |
+
app.config["APP_CONFIG"] = config
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# 3. Load ML Model ONCE
|
| 58 |
+
app.model = load_model(config)
|
| 59 |
+
|
| 60 |
+
if app.model is None:
|
| 61 |
+
print("Error: Model could not be loaded.")
|
| 62 |
+
|
| 63 |
+
# 4. Register Blueprints
|
| 64 |
+
|
| 65 |
+
from app.routes.page_routes import static_bp
|
| 66 |
+
from app.routes.coordinate_routes import coordinate_bp
|
| 67 |
+
from app.routes.predictions_routes import prediction_bp
|
| 68 |
+
|
| 69 |
+
app.register_blueprint(static_bp)
|
| 70 |
+
app.register_blueprint(coordinate_bp)
|
| 71 |
+
app.register_blueprint(prediction_bp)
|
| 72 |
+
|
| 73 |
+
# 5. Return app
|
| 74 |
+
return app
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|