Spaces:
Sleeping
Sleeping
| #!/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() | |