Main function to initialize and start the server
()
| 1080 | print(f"[{self.date_time_string()}] {format % args}") |
| 1081 | |
| 1082 | def main(): |
| 1083 | """Main function to initialize and start the server""" |
| 1084 | PORT = 8000 # ๐ Define port |
| 1085 | try: |
| 1086 | # Initialize the database |
| 1087 | print("โ Database initialized successfully") |
| 1088 | |
| 1089 | # Initialize the PDF processor |
| 1090 | try: |
| 1091 | pdf_module.initialize_simple_pdf_processor() |
| 1092 | print("๐ Initializing simple PDF processing...") |
| 1093 | if pdf_module.simple_pdf_processor: |
| 1094 | print("โ Simple PDF processor initialized") |
| 1095 | else: |
| 1096 | print("โ ๏ธ PDF processing could not be initialized.") |
| 1097 | except Exception as e: |
| 1098 | print(f"โ Error initializing PDF processor: {e}") |
| 1099 | print("โ ๏ธ PDF processing disabled - server will run without RAG functionality") |
| 1100 | |
| 1101 | # Set a global reference to the initialized processor if needed elsewhere |
| 1102 | global pdf_processor |
| 1103 | pdf_processor = pdf_module.simple_pdf_processor |
| 1104 | if pdf_processor: |
| 1105 | print("โ Global PDF processor initialized") |
| 1106 | else: |
| 1107 | print("โ ๏ธ PDF processing disabled - server will run without RAG functionality") |
| 1108 | |
| 1109 | # Cleanup empty sessions on startup |
| 1110 | print("๐งน Cleaning up empty sessions...") |
| 1111 | cleanup_count = db.cleanup_empty_sessions() |
| 1112 | if cleanup_count > 0: |
| 1113 | print(f"โจ Cleaned up {cleanup_count} empty sessions") |
| 1114 | else: |
| 1115 | print("โจ No empty sessions to clean up") |
| 1116 | |
| 1117 | # Start the server |
| 1118 | with ReusableTCPServer(("", PORT), ChatHandler) as httpd: |
| 1119 | print(f"๐ Starting localGPT backend server on port {PORT}") |
| 1120 | print(f"๐ Chat endpoint: http://localhost:{PORT}/chat") |
| 1121 | print(f"๐ Health check: http://localhost:{PORT}/health") |
| 1122 | |
| 1123 | # Test Ollama connection |
| 1124 | client = OllamaClient() |
| 1125 | if client.is_ollama_running(): |
| 1126 | models = client.list_models() |
| 1127 | print(f"โ Ollama is running with {len(models)} models") |
| 1128 | print(f"๐ Available models: {', '.join(models[:3])}{'...' if len(models) > 3 else ''}") |
| 1129 | else: |
| 1130 | print("โ ๏ธ Ollama is not running. Please start Ollama:") |
| 1131 | print(" Install: https://ollama.ai") |
| 1132 | print(" Run: ollama serve") |
| 1133 | |
| 1134 | print(f"\n๐ Frontend should connect to: http://localhost:{PORT}") |
| 1135 | print("๐ฌ Ready to chat!\n") |
| 1136 | |
| 1137 | httpd.serve_forever() |
| 1138 | except KeyboardInterrupt: |
| 1139 | print("\n๐ Server stopped") |
no test coverage detected