#!/usr/bin/env python3 """ Development runner script for Arabic Travel Agency Chatbot. Handles environment setup and graceful startup. """ import os import sys import logging from pathlib import Path def setup_environment(): """Load environment variables from .env file.""" try: from dotenv import load_dotenv load_dotenv() print("✓ Environment variables loaded from .env") except ImportError: print("⚠ python-dotenv not installed, using system environment variables") except Exception as e: print(f"⚠ Could not load .env file: {e}") def check_requirements(): """Check if required environment variables are set.""" required_vars = ['GEMINI_API_KEY'] missing_vars = [] for var in required_vars: if not os.getenv(var): missing_vars.append(var) if missing_vars: print("❌ Missing required environment variables:") for var in missing_vars: print(f" - {var}") print("\nPlease copy .env.example to .env and fill in the required values.") return False print("✓ All required environment variables are set") return True def create_directories(): """Create necessary directories if they don't exist.""" dirs_to_create = ['data', 'storage', 'static/img', 'static/css', 'static/js', 'templates'] for dir_path in dirs_to_create: Path(dir_path).mkdir(parents=True, exist_ok=True) print("✓ Directory structure verified") def check_data_files(): """Check if sample data files exist.""" data_dir = Path('data') txt_files = list(data_dir.glob('*.txt')) if not txt_files: print("⚠ No .txt files found in data/ directory") print(" Sample files should be created automatically") else: print(f"✓ Found {len(txt_files)} data files: {[f.name for f in txt_files]}") def main(): """Main runner function.""" print("🚀 Starting Arabic Travel Agency Chatbot...") print("=" * 50) # Setup setup_environment() if not check_requirements(): sys.exit(1) create_directories() check_data_files() print("=" * 50) print("🌟 All checks passed! Starting Flask application...") print("📱 Open http://localhost:5000 in your browser") print("🔄 Press Ctrl+C to stop the server") print("=" * 50) # Import and run the Flask app try: from app import app, initialize_services # Ensure services are initialized initialize_services() # Run the Flask app debug_mode = os.getenv('FLASK_ENV') == 'development' port = int(os.getenv('PORT', 5000)) app.run( host='0.0.0.0', port=port, debug=debug_mode ) except KeyboardInterrupt: print("\n👋 Chatbot stopped gracefully") except Exception as e: print(f"❌ Error starting application: {e}") sys.exit(1) if __name__ == '__main__': main()