File size: 3,489 Bytes
1099afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
522f7a0
1099afe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# DEPENDENCIES
from pathlib import Path
from pydantic import Field
from typing import Optional
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    """
    Application-wide settings: primary configuration source
    """
    # Application Info
    APP_NAME               : str           = "AI Contract Risk Analyzer"
    APP_VERSION            : str           = "1.0.0"
    API_PREFIX             : str           = "/api/v1/"
    
    # Server Configuration
    HOST                   : str           = "0.0.0.0"
    PORT                   : int           = 8000
    RELOAD                 : bool          = True
    WORKERS                : int           = 1
    
    # CORS Settings
    CORS_ORIGINS           : list          = ["http://localhost:3000", "http://localhost:8000", "http://127.0.0.1:8000"]
    CORS_ALLOW_CREDENTIALS : bool          = True
    CORS_ALLOW_METHODS     : list          = ["*"]
    CORS_ALLOW_HEADERS     : list          = ["*"]
    
    # File Upload Settings
    MAX_UPLOAD_SIZE        : int           = 10 * 1024 * 1024  # 10 MB
    ALLOWED_EXTENSIONS     : list          = [".pdf", ".docx", ".txt"]
    UPLOAD_DIR             : Path          = Path("uploads")
    
    # Model Management Settings
    MODEL_CACHE_SIZE       : int           = 3     # Number of models to keep in memory
    MODEL_DOWNLOAD_TIMEOUT : int           = 1800  # 30 minutes
    USE_GPU                : bool          = True  # Automatically detect and use GPU if available
    
    # External API Settings
    OLLAMA_BASE_URL        : str           = "http://localhost:11434"
    OLLAMA_MODEL           : str           = "llama3:8b"
    OLLAMA_TIMEOUT         : int           = 300
    OLLAMA_TEMPERATURE     : float         = 0.1
    
    # External API Keys
    OPENAI_API_KEY         : Optional[str] = None
    ANTHROPIC_API_KEY      : Optional[str] = None
    
    # Analysis Limits
    MIN_CONTRACT_LENGTH    : int           = 300    # Minimum characters for valid contract
    MAX_CONTRACT_LENGTH    : int           = 500000 # Maximum characters (500KB text)
    MAX_CLAUSES_TO_ANALYZE : int           = 15
    
    # Logging Settings
    LOG_LEVEL              : str           = "INFO"
    LOG_FORMAT             : str           = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    LOG_FILE               : Optional[Path] = Path("logs/app.log")
    
    # Cache Settings
    ENABLE_CACHE           : bool          = True
    CACHE_TTL              : int           = 3600 # 1 hour
    CACHE_DIR              : Path          = Path("cache")
    
    # Rate Limiting Settings
    RATE_LIMIT_ENABLED     : bool          = True
    RATE_LIMIT_REQUESTS    : int           = 10
    RATE_LIMIT_PERIOD      : int           = 60  # seconds
    
    # PDF Report Settings
    PDF_FONT_SIZE          : int           = 10
    PDF_MARGIN             : float         = 0.5 # inches
    PDF_PAGE_SIZE          : str           = "letter"
    

    class Config:
        env_file          = ".env"
        env_file_encoding = "utf-8"
        case_sensitive    = True
    

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # Ensure directories exist
        self.UPLOAD_DIR.mkdir(parents = True, exist_ok = True)
        self.CACHE_DIR.mkdir(parents = True, exist_ok = True)
        
        if self.LOG_FILE:
            self.LOG_FILE.parent.mkdir(parents = True, exist_ok = True)


# Global settings instance
settings = Settings()