alschameri/helping-source
0
1#!/usr/bin/env python32"""3Development runner script for Arabic Travel Agency Chatbot.4Handles environment setup and graceful startup.5"""6 7import os8import sys9import logging10from pathlib import Path11 12def setup_environment():13 """Load environment variables from .env file."""14 try:15 from dotenv import load_dotenv16 load_dotenv()17 print("✓ Environment variables loaded from .env")18 except ImportError:19 print("⚠ python-dotenv not installed, using system environment variables")20 except Exception as e:21 print(f"⚠ Could not load .env file: {e}")22 23def check_requirements():24 """Check if required environment variables are set."""25 required_vars = ['GEMINI_API_KEY']26 missing_vars = []27 28 for var in required_vars:29 if not os.getenv(var):30 missing_vars.append(var)31 32 if missing_vars:33 print("❌ Missing required environment variables:")34 for var in missing_vars:35 print(f" - {var}")36 print("\nPlease copy .env.example to .env and fill in the required values.")37 return False38 39 print("✓ All required environment variables are set")40 return True41 42def create_directories():43 """Create necessary directories if they don't exist."""44 dirs_to_create = ['data', 'storage', 'static/img', 'static/css', 'static/js', 'templates']45 46 for dir_path in dirs_to_create:47 Path(dir_path).mkdir(parents=True, exist_ok=True)48 49 print("✓ Directory structure verified")50 51def check_data_files():52 """Check if sample data files exist."""53 data_dir = Path('data')54 txt_files = list(data_dir.glob('*.txt'))55 56 if not txt_files:57 print("⚠ No .txt files found in data/ directory")58 print(" Sample files should be created automatically")59 else:60 print(f"✓ Found {len(txt_files)} data files: {[f.name for f in txt_files]}")61 62def main():63 """Main runner function."""64 print("🚀 Starting Arabic Travel Agency Chatbot...")65 print("=" * 50)66 67 # Setup68 setup_environment()69 70 if not check_requirements():71 sys.exit(1)72 73 create_directories()74 check_data_files()75 76 print("=" * 50)77 print("🌟 All checks passed! Starting Flask application...")78 print("📱 Open http://localhost:5000 in your browser")79 print("🔄 Press Ctrl+C to stop the server")80 print("=" * 50)81 82 # Import and run the Flask app83 try:84 from app import app, initialize_services85 86 # Ensure services are initialized87 initialize_services()88 89 # Run the Flask app90 debug_mode = os.getenv('FLASK_ENV') == 'development'91 port = int(os.getenv('PORT', 5000))92 93 app.run(94 host='0.0.0.0',95 port=port,96 debug=debug_mode97 )98 99 except KeyboardInterrupt:100 print("\n👋 Chatbot stopped gracefully")101 except Exception as e:102 print(f"❌ Error starting application: {e}")103 sys.exit(1)104 105if __name__ == '__main__':106 main()