Spaces:
Running
Running
File size: 1,751 Bytes
d66ab65 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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 |