File size: 3,739 Bytes
e06a21d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dd0906
e06a21d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Production-ready CVE Fact Checker with English language filtering.
This script sets up the environment and starts the application.
"""

import os
import sys
import subprocess

def setup_production_environment():
    """Setup production environment variables."""
    
    # Core configuration
    env_vars = {
        'OPENROUTER_API_KEY': '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',
        'LANGUAGE_FILTER': 'English',
        'AUTO_INGEST': 'true',
        'PORT': '7860',
        'VECTOR_PERSIST_DIR': '/tmp/vector_db',
        'SENTENCE_TRANSFORMERS_HOME': '/tmp/sentence_transformers',
    }
    
    # Apply environment variables
    for key, value in env_vars.items():
        os.environ[key] = value
        print(f"βœ… Set {key}")
    
    print(f"\nπŸ”§ Environment configured for English articles only")

def start_production_server():
    """Start the production server."""
    print("\nπŸš€ Starting CVE Fact Checker (Production)")
    print("=" * 50)
    
    try:
        # Use gunicorn for production
        cmd = [
            "gunicorn",
            "-w", "1",  # Single worker to avoid race conditions
            "-k", "gthread",
            "--threads", "4",
            "-b", f"0.0.0.0:{os.environ.get('PORT', '7860')}",
            "--timeout", "180",
            "--access-logfile", "-",
            "--error-logfile", "-",
            "cve_factchecker.wsgi:application"
        ]
        
        print(f"πŸ“‹ Command: {' '.join(cmd)}")
        print(f"🌐 Server will start on port {os.environ.get('PORT', '7860')}")
        print(f"🌍 Language filter: {os.environ.get('LANGUAGE_FILTER', 'English')}")
        print("πŸ”— Access at: http://localhost:7860")
        print("\n" + "="*50)
        
        # Start the server
        subprocess.run(cmd, check=True)
        
    except FileNotFoundError:
        print("❌ Gunicorn not found. Installing...")
        subprocess.run([sys.executable, "-m", "pip", "install", "gunicorn"], check=True)
        print("βœ… Gunicorn installed. Retrying...")
        subprocess.run(cmd, check=True)
        
    except KeyboardInterrupt:
        print("\nπŸ›‘ Server stopped by user")
        
    except subprocess.CalledProcessError as e:
        print(f"❌ Server failed: {e}")
        sys.exit(1)

def start_development_server():
    """Start development server with Flask."""
    print("\nπŸ”§ Starting CVE Fact Checker (Development)")
    print("=" * 50)
    
    try:
        from cve_factchecker.app import app
        
        print(f"🌐 Server will start on port {os.environ.get('PORT', '7860')}")
        print(f"🌍 Language filter: {os.environ.get('LANGUAGE_FILTER', 'English')}")
        print("πŸ”— Access at: http://localhost:7860")
        print("\n" + "="*50)
        
        app.run(
            host='0.0.0.0',
            port=int(os.environ.get('PORT', '7860')),
            debug=False  # Set to False for stability
        )
        
    except KeyboardInterrupt:
        print("\nπŸ›‘ Server stopped by user")

def main():
    """Main entry point."""
    import argparse
    
    parser = argparse.ArgumentParser(description="CVE Fact Checker with Language Filtering")
    parser.add_argument("--mode", choices=["dev", "prod"], default="prod", 
                       help="Run in development or production mode")
    
    args = parser.parse_args()
    
    print("πŸ€– CVE Fact Checker - English Articles Only")
    print("=" * 60)
    
    # Setup environment
    setup_production_environment()
    
    # Start appropriate server
    if args.mode == "dev":
        start_development_server()
    else:
        start_production_server()

if __name__ == "__main__":
    main()