File size: 7,657 Bytes
fd50325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""

Report Generation Module Setup Script



Run this script to:

1. Check/install dependencies

2. Download the LLM model

3. Create necessary directories

4. Verify the installation

"""

import os
import sys
import subprocess


def check_python_version():
    """Check Python version."""
    print("πŸ” Checking Python version...")
    version = sys.version_info
    if version.major < 3 or (version.major == 3 and version.minor < 9):
        print(f"❌ Python 3.9+ required. Found: {version.major}.{version.minor}")
        return False
    print(f"βœ… Python {version.major}.{version.minor}.{version.micro}")
    return True


def install_dependencies():
    """Install required Python packages."""
    print("\nπŸ“¦ Installing Python dependencies...")
    
    packages = [
        "llama-cpp-python",
        "huggingface_hub",
        "jinja2",
        "markdown",
        "Pillow",
        "reportlab",
    ]
    
    # Optional packages (may fail on some systems)
    optional_packages = [
        "weasyprint",  # Requires GTK3 on Windows
    ]
    
    # Install required packages
    for package in packages:
        print(f"  Installing {package}...")
        try:
            subprocess.check_call(
                [sys.executable, "-m", "pip", "install", package, "-q"],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL
            )
            print(f"  βœ… {package}")
        except subprocess.CalledProcessError:
            print(f"  ❌ Failed to install {package}")
            return False
    
    # Try optional packages
    for package in optional_packages:
        print(f"  Installing {package} (optional)...")
        try:
            subprocess.check_call(
                [sys.executable, "-m", "pip", "install", package, "-q"],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL
            )
            print(f"  βœ… {package}")
        except subprocess.CalledProcessError:
            print(f"  ⚠️ {package} not installed (PDF export may not work)")
    
    return True


def download_model():
    """Download the LLM model."""
    print("\nπŸ€– Downloading LLM model...")
    
    try:
        from huggingface_hub import hf_hub_download
        
        model_dir = os.path.join(os.path.dirname(__file__), 'models')
        os.makedirs(model_dir, exist_ok=True)
        
        # Download Qwen2.5-3B-Instruct Q4 quantized
        print("  Downloading Qwen2.5-3B-Instruct (Q4_K_M, ~2GB)...")
        print("  This may take several minutes depending on your connection...")
        
        downloaded_path = hf_hub_download(
            repo_id="Qwen/Qwen2.5-3B-Instruct-GGUF",
            filename="qwen2.5-3b-instruct-q4_k_m.gguf",
            local_dir=model_dir,
            local_dir_use_symlinks=False
        )
        
        print(f"  βœ… Model downloaded to: {downloaded_path}")
        return True
        
    except Exception as e:
        print(f"  ❌ Failed to download model: {e}")
        print("  You can manually download from:")
        print("  https://huggingface.co/Qwen/Qwen2.5-3B-Instruct-GGUF")
        return False


def create_directories():
    """Create necessary directories."""
    print("\nπŸ“ Creating directories...")
    
    base_dir = os.path.dirname(__file__)
    directories = [
        os.path.join(base_dir, 'models'),
        os.path.join(base_dir, 'templates'),
        os.path.join(base_dir, 'prompts'),
        os.path.join(os.path.dirname(base_dir), 'video_processing_outputs', 'reports'),
    ]
    
    for directory in directories:
        os.makedirs(directory, exist_ok=True)
        print(f"  βœ… {directory}")
    
    return True


def verify_installation():
    """Verify the installation."""
    print("\nπŸ”¬ Verifying installation...")
    
    # Check imports
    modules = [
        ('llama_cpp', 'llama-cpp-python'),
        ('huggingface_hub', 'huggingface_hub'),
        ('jinja2', 'Jinja2'),
        ('markdown', 'markdown'),
        ('PIL', 'Pillow'),
        ('reportlab', 'reportlab'),
    ]
    
    all_ok = True
    for module, package in modules:
        try:
            __import__(module)
            print(f"  βœ… {package}")
        except ImportError:
            print(f"  ❌ {package} not found")
            all_ok = False
    
    # Check weasyprint (optional)
    try:
        import weasyprint
        print("  βœ… weasyprint (PDF export available)")
    except (ImportError, OSError):
        print("  ⚠️ weasyprint not available (PDF export disabled, use HTML)")
    
    # Check model file
    model_path = os.path.join(
        os.path.dirname(__file__), 
        'models', 
        'qwen2.5-3b-instruct-q4_k_m.gguf'
    )
    if os.path.exists(model_path):
        size_mb = os.path.getsize(model_path) / (1024 * 1024)
        print(f"  βœ… LLM model found ({size_mb:.0f} MB)")
    else:
        print("  ⚠️ LLM model not found - will download on first use")
    
    return all_ok


def test_generation():
    """Test the report generation system."""
    print("\nπŸ§ͺ Testing report generation...")
    
    try:
        from report_generation import ReportGenerator
        
        generator = ReportGenerator()
        print("  βœ… ReportGenerator initialized")
        
        # Test without actual data
        print("  βœ… Module imports successful")
        print("\n  Note: Full test requires a processed video in the database")
        
        return True
        
    except Exception as e:
        print(f"  ❌ Test failed: {e}")
        return False


def main():
    """Run the setup process."""
    print("=" * 60)
    print("πŸ›‘οΈ DetectifAI Report Generation Module Setup")
    print("=" * 60)
    
    steps = [
        ("Check Python version", check_python_version),
        ("Create directories", create_directories),
        ("Install dependencies", install_dependencies),
        ("Verify installation", verify_installation),
    ]
    
    all_passed = True
    for step_name, step_func in steps:
        if not step_func():
            all_passed = False
            print(f"\n⚠️ Step '{step_name}' had issues. Continuing...")
    
    # Ask about model download
    print("\n" + "=" * 60)
    print("πŸ“₯ Model Download")
    print("=" * 60)
    print("\nThe LLM model (~2GB) is required for AI-generated report content.")
    print("It will be automatically downloaded on first use, or you can download now.")
    
    response = input("\nDownload model now? [y/N]: ").strip().lower()
    if response == 'y':
        download_model()
    else:
        print("Skipping model download. Will download on first use.")
    
    # Final summary
    print("\n" + "=" * 60)
    print("πŸ“‹ Setup Summary")
    print("=" * 60)
    
    if all_passed:
        print("\nβœ… Setup completed successfully!")
        print("\nNext steps:")
        print("1. Process a video through DetectifAI pipeline")
        print("2. Generate a report:")
        print("   from report_generation import ReportGenerator")
        print("   generator = ReportGenerator()")
        print("   report = generator.generate_report('video_id_here')")
        print("   generator.export_html(report)")
    else:
        print("\n⚠️ Setup completed with some warnings.")
        print("Check the messages above for details.")
    
    print("\n" + "=" * 60)


if __name__ == "__main__":
    main()