Spaces:
Runtime error
Runtime error
| import os | |
| import sys | |
| import json | |
| from flask import Flask, render_template, render_template_string | |
| from flask_cors import CORS | |
| from flask_socketio import SocketIO | |
| from datetime import datetime, date | |
| from dotenv import load_dotenv | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # Ensure the current directory is in the Python path | |
| sys.path.append(os.path.abspath(os.path.dirname(__file__))) | |
| from database import setup_database | |
| socketio = SocketIO() | |
| def humanize_datetime(dt_str): | |
| """Converts a datetime string to a human-friendly format.""" | |
| if not dt_str: | |
| return "" | |
| try: | |
| # Split the string at the decimal point to handle microseconds | |
| dt = datetime.fromisoformat(dt_str.split('.')[0]) | |
| today = date.today() | |
| if dt.date() == today: | |
| return "Today" | |
| elif dt.date() == date.fromordinal(today.toordinal() - 1): | |
| return "Yesterday" | |
| else: | |
| return dt.strftime('%b %d, %Y') | |
| except (ValueError, TypeError): | |
| return dt_str # Return original string if parsing fails | |
| def create_app(): | |
| app = Flask( | |
| __name__, | |
| template_folder='templates', | |
| ) | |
| CORS(app) | |
| socketio.init_app(app, cors_allowed_origins="*") | |
| # Register custom Jinja2 filter | |
| app.jinja_env.filters['humanize'] = humanize_datetime | |
| app.jinja_env.filters['chr'] = chr | |
| app.jinja_env.filters['from_json'] = lambda x: json.loads(x) if x else [] | |
| # Configuration | |
| app.config['SECRET_KEY'] = os.urandom(24) | |
| app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 * 4096 | |
| app.config['UPLOAD_FOLDER'] = 'uploads' | |
| app.config['PROCESSED_FOLDER'] = 'processed' | |
| app.config['OUTPUT_FOLDER'] = 'output' | |
| app.config['TEMP_FOLDER'] = 'tmp' | |
| app.config['TEMPLATES_PREACT'] = os.path.join(app.root_path, 'templates_preact') | |
| # Ensure instance folders exist | |
| for folder in [app.config['UPLOAD_FOLDER'], app.config['PROCESSED_FOLDER'], app.config['OUTPUT_FOLDER'], app.config['TEMP_FOLDER']]: | |
| os.makedirs(folder, exist_ok=True) | |
| with app.app_context(): | |
| setup_database() | |
| # Setup Login Manager | |
| from user_auth import setup_login_manager | |
| setup_login_manager(app) | |
| # Register Blueprints | |
| from routes import main_bp | |
| from json_processor import json_bp | |
| from neetprep import neetprep_bp | |
| from classifier_routes import classifier_bp | |
| from dashboard import dashboard_bp | |
| from image_routes import image_bp | |
| from auth_routes import auth_bp | |
| from settings_routes import settings_bp | |
| from subjective_routes import subjective_bp | |
| from camera_routes import camera_bp | |
| from drive_routes import drive_bp | |
| from qtab_routes import qtab_bp | |
| from knowledge_graph_routes import knowledge_bp | |
| from graph_routes import graph_bp | |
| app.register_blueprint(main_bp) | |
| app.register_blueprint(json_bp) | |
| app.register_blueprint(neetprep_bp) | |
| app.register_blueprint(classifier_bp) | |
| app.register_blueprint(dashboard_bp) | |
| app.register_blueprint(image_bp) | |
| app.register_blueprint(auth_bp) | |
| app.register_blueprint(settings_bp) | |
| app.register_blueprint(subjective_bp) | |
| app.register_blueprint(camera_bp) | |
| app.register_blueprint(drive_bp) | |
| app.register_blueprint(qtab_bp) | |
| app.register_blueprint(knowledge_bp) | |
| app.register_blueprint(graph_bp) | |
| # Initialize socketio handlers for camera routes (after app creation to avoid circular import) | |
| from camera_routes import init_socketio_handlers | |
| init_socketio_handlers(socketio) | |
| return app | |
| app = create_app() |