File size: 4,409 Bytes
db73aaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
from pathlib import Path

class Config:
    """Configuration class for Smart RAG API"""
    
    # Base directories
    BASE_DIR = Path(__file__).parent
    UPLOAD_DIR = BASE_DIR / "uploads"
    VECTOR_STORE_DIR = BASE_DIR / "vector_store"
    TEMP_DIR = BASE_DIR / "temp"
    
    # File processing
    MAX_FILE_SIZE = int(os.getenv("MAX_FILE_SIZE", 10 * 1024 * 1024))  # 10MB default
    ALLOWED_EXTENSIONS = {
        '.pdf', '.docx', '.txt', '.jpg', '.jpeg', '.png', '.csv', '.db'
    }
    
    # Text chunking
    CHUNK_SIZE = int(os.getenv("CHUNK_SIZE", 500))
    CHUNK_OVERLAP = int(os.getenv("CHUNK_OVERLAP", 50))
    
    # Hugging Face Models (Free)
    EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
    
    # LLM Model options (choose based on performance needs)
    LLM_MODEL = os.getenv("LLM_MODEL", "google/flan-t5-base")
    # Alternative models:
    # "microsoft/DialoGPT-medium" - for conversational responses
    # "google/flan-t5-small" - faster, smaller model
    # "facebook/bart-large-cnn" - good for summarization
    
    # Vector search
    VECTOR_SEARCH_K = int(os.getenv("VECTOR_SEARCH_K", 5))
    SIMILARITY_THRESHOLD = float(os.getenv("SIMILARITY_THRESHOLD", 0.1))
    
    # OCR settings
    TESSERACT_CMD = os.getenv("TESSERACT_CMD", "/usr/bin/tesseract")
    OCR_LANGUAGE = os.getenv("OCR_LANGUAGE", "eng")
    
    # API settings
    API_HOST = os.getenv("API_HOST", "0.0.0.0")
    API_PORT = int(os.getenv("API_PORT", 7860))
    
    # Gradio settings
    GRADIO_SHARE = os.getenv("GRADIO_SHARE", "true").lower() == "true"
    GRADIO_DEBUG = os.getenv("GRADIO_DEBUG", "false").lower() == "true"
    
    # Model cache directory (for Hugging Face models)
    HF_CACHE_DIR = os.getenv("HF_HOME", BASE_DIR / "model_cache")
    
    # Performance settings
    TORCH_THREADS = int(os.getenv("TORCH_THREADS", 4))
    USE_GPU = os.getenv("USE_GPU", "false").lower() == "true"
    
    # Logging
    LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
    
    @classmethod
    def setup_environment(cls):
        """Setup environment variables and directories"""
        
        # Set Hugging Face cache directory
        os.environ["HF_HOME"] = str(cls.HF_CACHE_DIR)
        os.environ["TRANSFORMERS_CACHE"] = str(cls.HF_CACHE_DIR)
        
        # Set PyTorch settings
        os.environ["OMP_NUM_THREADS"] = str(cls.TORCH_THREADS)
        os.environ["MKL_NUM_THREADS"] = str(cls.TORCH_THREADS)
        
        # Disable tokenizers parallelism warning
        os.environ["TOKENIZERS_PARALLELISM"] = "false"
        
        # Set Tesseract command if available
        if os.path.exists(cls.TESSERACT_CMD):
            import pytesseract
            pytesseract.pytesseract.tesseract_cmd = cls.TESSERACT_CMD

# File type configurations
FILE_TYPE_CONFIG = {
    '.pdf': {
        'icon': 'πŸ“„',
        'description': 'PDF Document',
        'processor': 'pdf'
    },
    '.docx': {
        'icon': 'πŸ“',
        'description': 'Word Document',
        'processor': 'docx'
    },
    '.txt': {
        'icon': 'πŸ“ƒ',
        'description': 'Text File',
        'processor': 'text'
    },
    '.jpg': {
        'icon': 'πŸ–ΌοΈ',
        'description': 'JPEG Image',
        'processor': 'image'
    },
    '.jpeg': {
        'icon': 'πŸ–ΌοΈ',
        'description': 'JPEG Image',
        'processor': 'image'
    },
    '.png': {
        'icon': 'πŸ–ΌοΈ',
        'description': 'PNG Image',
        'processor': 'image'
    },
    '.csv': {
        'icon': 'πŸ“Š',
        'description': 'CSV Data',
        'processor': 'csv'
    },
    '.db': {
        'icon': 'πŸ—„οΈ',
        'description': 'SQLite Database',
        'processor': 'database'
    }
}

# Model configurations for different use cases
MODEL_CONFIGS = {
    'fast': {
        'embedding': 'sentence-transformers/all-MiniLM-L6-v2',
        'llm': 'google/flan-t5-small',
        'description': 'Fast processing, lower accuracy'
    },
    'balanced': {
        'embedding': 'sentence-transformers/all-MiniLM-L6-v2',
        'llm': 'google/flan-t5-base',
        'description': 'Balanced speed and accuracy'
    },
    'accurate': {
        'embedding': 'sentence-transformers/all-mpnet-base-v2',
        'llm': 'google/flan-t5-large',
        'description': 'Higher accuracy, slower processing'
    }
}

# Initialize configuration
Config.setup_environment()