Spaces:
Running
Running
import os | |
import logging | |
from logging.handlers import RotatingFileHandler | |
from flask import Flask | |
from config import Config | |
def create_app(config_class=Config): | |
"""Create and configure Flask application.""" | |
app = Flask(__name__) | |
app.config.from_object(config_class) | |
# Ensure upload folder exists | |
if not os.path.exists(app.config['UPLOAD_FOLDER']): | |
os.makedirs(app.config['UPLOAD_FOLDER']) | |
# Ensure HuggingFace cache directory exists | |
if not os.path.exists(app.config['HF_HOME']): | |
os.makedirs(app.config['HF_HOME']) | |
# Set HuggingFace environment variables | |
os.environ['HF_HOME'] = app.config['HF_HOME'] | |
if 'HF_CACHE_DIR' in app.config: | |
os.environ['HF_CACHE_DIR'] = app.config['HF_CACHE_DIR'] | |
# Register Blueprints | |
from app.routes import main_bp | |
app.register_blueprint(main_bp) | |
# Setup logging | |
if not app.debug and not app.testing: | |
if not os.path.exists('logs'): | |
os.mkdir('logs') | |
file_handler = RotatingFileHandler( | |
f'logs/{app.config.get("LOG_FILE", "tokenizer_pro.log")}', | |
maxBytes=app.config.get("LOG_MAX_BYTES", 10 * 1024 * 1024), | |
backupCount=app.config.get("LOG_BACKUP_COUNT", 3) | |
) | |
file_handler.setFormatter(logging.Formatter( | |
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]' | |
)) | |
log_level = getattr(logging, app.config.get("LOG_LEVEL", "INFO").upper()) | |
file_handler.setLevel(log_level) | |
app.logger.addHandler(file_handler) | |
app.logger.setLevel(log_level) | |
app.logger.info('Tokenizer Pro startup') | |
return app |