File size: 5,017 Bytes
11d9dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
"""
Setup script for Professional RAG Assistant.
"""

import os
import sys
import subprocess
import urllib.request
from pathlib import Path


def run_command(command, description=""):
    """Run a shell command with error handling."""
    print(f"Running: {command}")
    if description:
        print(f"Description: {description}")
    
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    
    if result.returncode != 0:
        print(f"Error running command: {command}")
        print(f"Error output: {result.stderr}")
        return False
    
    if result.stdout:
        print(result.stdout)
    
    return True


def check_python_version():
    """Check if Python version is compatible."""
    if sys.version_info < (3, 8):
        print("Error: Python 3.8 or higher is required")
        return False
    
    print(f"Python version: {sys.version}")
    return True


def install_requirements():
    """Install Python requirements."""
    print("Installing Python dependencies...")
    
    if not run_command("pip install -r requirements.txt", "Installing main dependencies"):
        return False
    
    # Install dev dependencies if requested
    if "--dev" in sys.argv:
        if not run_command("pip install -r requirements-dev.txt", "Installing dev dependencies"):
            return False
    
    return True


def create_directories():
    """Create necessary directories."""
    directories = [
        "logs",
        "cache",
        "static/uploads",
        "data",
        "models"
    ]
    
    for directory in directories:
        Path(directory).mkdir(parents=True, exist_ok=True)
        print(f"Created directory: {directory}")


def download_sample_documents():
    """Download sample documents for testing."""
    if "--skip-samples" in sys.argv:
        return True
    
    print("Downloading sample documents...")
    
    sample_dir = Path("examples/sample_docs")
    sample_dir.mkdir(parents=True, exist_ok=True)
    
    # Create a sample text document
    sample_text = """
Professional RAG Assistant - Sample Document

This is a sample document to demonstrate the capabilities of the Professional RAG Assistant.

Key Features:
- Multi-format document processing (PDF, DOCX, TXT)
- Advanced hybrid search combining vector similarity and keyword search
- Cross-encoder re-ranking for improved relevance
- Comprehensive caching system for performance
- Professional Gradio interface with analytics

The system uses sentence-transformers for creating document embeddings and BM25 for keyword search.
Results are re-ranked using cross-encoder models to provide the most relevant answers to user queries.

This sample document can be used to test the upload and search functionality of the system.
"""
    
    with open(sample_dir / "sample_document.txt", "w") as f:
        f.write(sample_text)
    
    print(f"Created sample document: {sample_dir / 'sample_document.txt'}")
    return True


def setup_configuration():
    """Setup configuration files."""
    print("Setting up configuration...")
    
    # Check if config files exist
    if not Path("config.yaml").exists():
        print("Warning: config.yaml not found. Using default configuration.")
    
    if not Path("config-local.yaml").exists():
        print("Info: config-local.yaml not found. This is optional for local development.")
    
    return True


def run_tests():
    """Run test suite if requested."""
    if "--test" not in sys.argv:
        return True
    
    print("Running test suite...")
    
    if not run_command("python -m pytest tests/ -v", "Running tests"):
        print("Warning: Some tests failed. The application should still work.")
        return True  # Don't fail setup on test failures
    
    return True


def main():
    """Main setup function."""
    print("Setting up Professional RAG Assistant...")
    print("=" * 50)
    
    # Check Python version
    if not check_python_version():
        sys.exit(1)
    
    # Create directories
    create_directories()
    
    # Install requirements
    if not install_requirements():
        print("Failed to install requirements")
        sys.exit(1)
    
    # Setup configuration
    if not setup_configuration():
        print("Failed to setup configuration")
        sys.exit(1)
    
    # Download sample documents
    if not download_sample_documents():
        print("Failed to download sample documents")
        sys.exit(1)
    
    # Run tests if requested
    if not run_tests():
        print("Tests failed")
        sys.exit(1)
    
    print("\n" + "=" * 50)
    print("Setup completed successfully!")
    print("\nTo start the application:")
    print("  python app.py")
    print("\nTo start in debug mode:")
    print("  python app.py --debug")
    print("\nTo use local configuration:")
    print("  python app.py --config config-local.yaml")
    print("\nFor more options:")
    print("  python app.py --help")


if __name__ == "__main__":
    main()