File size: 4,122 Bytes
af25178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
=============================================================================
Mozhii RAG Data Platform - Flask Application Factory
=============================================================================
This module contains the Flask application factory function.
The factory pattern allows us to create multiple instances of the app,
which is useful for testing and running different configurations.

Architecture:
    - Flask as the web framework
    - Blueprint-based routing for modular code organization
    - Service layer for business logic separation
    - Local storage with HuggingFace sync capability
=============================================================================
"""

from flask import Flask
from flask_cors import CORS
import os

def create_app(config_name=None):
    """
    Application factory function.
    
    This function creates and configures the Flask application.
    Using a factory pattern allows for:
    - Multiple app instances with different configs
    - Better testing capabilities
    - Delayed initialization of extensions
    
    Args:
        config_name: Optional configuration name ('development', 'production', 'testing')
    
    Returns:
        Flask: Configured Flask application instance
    """
    
    # -------------------------------------------------------------------------
    # Create Flask App Instance
    # -------------------------------------------------------------------------
    # template_folder: Where HTML templates are stored
    # static_folder: Where CSS, JS, and assets are stored
    app = Flask(
        __name__,
        template_folder='../templates',
        static_folder='../static'
    )
    
    # -------------------------------------------------------------------------
    # Load Configuration
    # -------------------------------------------------------------------------
    # Import and apply configuration settings
    from .config import Config
    app.config.from_object(Config)
    
    # -------------------------------------------------------------------------
    # Initialize Extensions
    # -------------------------------------------------------------------------
    # Enable CORS for API access from different origins
    # This is important if the frontend is served from a different domain
    CORS(app)
    
    # -------------------------------------------------------------------------
    # Ensure Data Directories Exist
    # -------------------------------------------------------------------------
    # Create the directory structure for storing files locally
    # before they are pushed to HuggingFace
    data_dirs = [
        'data/pending/raw',
        'data/pending/cleaned', 
        'data/pending/chunked',
        'data/approved/raw',
        'data/approved/cleaned',
        'data/approved/chunked'
    ]
    
    for dir_path in data_dirs:
        full_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), dir_path)
        os.makedirs(full_path, exist_ok=True)
    
    # -------------------------------------------------------------------------
    # Register Blueprints (Route Modules)
    # -------------------------------------------------------------------------
    # Blueprints allow us to organize routes into separate modules
    # Each tab in the UI has its own blueprint
    
    from .routes import main_bp, raw_data_bp, cleaning_bp, chunking_bp, admin_bp
    
    # Main routes (index page, etc.)
    app.register_blueprint(main_bp)
    
    # Raw Data Tab API endpoints
    app.register_blueprint(raw_data_bp, url_prefix='/api/raw')
    
    # Cleaning Tab API endpoints
    app.register_blueprint(cleaning_bp, url_prefix='/api/cleaning')
    
    # Chunking Tab API endpoints
    app.register_blueprint(chunking_bp, url_prefix='/api/chunking')
    
    # Admin approval endpoints
    app.register_blueprint(admin_bp, url_prefix='/api/admin')
    
    # -------------------------------------------------------------------------
    # Return Configured App
    # -------------------------------------------------------------------------
    return app