CVE-FactChecker / start_production.py
NLPGenius's picture
Stability: remove gunicorn --preload, increase timeout, disable tokenizers parallelism, cap BLAS threads, lighten /health, configurable embeddings
1dd0906
raw
history blame
3.74 kB
#!/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()