| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2024-01-19 19:51:57 +08:00
										 |  |  | #  Copyright 2024 The InfiniFlow Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  | # | 
					
						
							|  |  |  | #  Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | #  you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | #  You may obtain a copy of the License at | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #      http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | #  distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | #  See the License for the specific language governing permissions and | 
					
						
							|  |  |  | #  limitations under the License. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import logging | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import signal | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import traceback | 
					
						
							|  |  |  | from werkzeug.serving import run_simple | 
					
						
							| 
									
										
										
										
											2024-01-17 20:20:42 +08:00
										 |  |  | from api.apps import app | 
					
						
							|  |  |  | from api.db.runtime_config import RuntimeConfig | 
					
						
							|  |  |  | from api.settings import ( | 
					
						
							| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  |     HOST, HTTP_PORT, access_logger, database_logger, stat_logger, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-01-17 20:20:42 +08:00
										 |  |  | from api import utils | 
					
						
							| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-17 20:20:42 +08:00
										 |  |  | from api.db.db_models import init_database_tables as init_web_db | 
					
						
							|  |  |  | from api.db.init_data import init_web_data | 
					
						
							|  |  |  | from api.versions import get_versions | 
					
						
							| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2024-02-28 15:01:12 +08:00
										 |  |  |     print("""
 | 
					
						
							|  |  |  |     ____                 ______ __                | 
					
						
							|  |  |  |    / __ \ ____ _ ____ _ / ____// /____  _      __ | 
					
						
							|  |  |  |   / /_/ // __ `// __ `// /_   / // __ \| | /| / / | 
					
						
							|  |  |  |  / _, _// /_/ // /_/ // __/  / // /_/ /| |/ |/ /  | 
					
						
							|  |  |  | /_/ |_| \__,_/ \__, //_/    /_/ \____/ |__/|__/   | 
					
						
							|  |  |  |               /____/                              | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 16:19:03 +08:00
										 |  |  |     """, flush=True)
 | 
					
						
							| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  |     stat_logger.info( | 
					
						
							|  |  |  |         f'project base: {utils.file_utils.get_project_base_directory()}' | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # init db | 
					
						
							|  |  |  |     init_web_db() | 
					
						
							|  |  |  |     init_web_data() | 
					
						
							|  |  |  |     # init runtime config | 
					
						
							|  |  |  |     import argparse | 
					
						
							|  |  |  |     parser = argparse.ArgumentParser() | 
					
						
							| 
									
										
										
										
											2024-01-17 09:43:27 +08:00
										 |  |  |     parser.add_argument('--version', default=False, help="rag flow version", action='store_true') | 
					
						
							| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  |     parser.add_argument('--debug', default=False, help="debug mode", action='store_true') | 
					
						
							|  |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  |     if args.version: | 
					
						
							|  |  |  |         print(get_versions()) | 
					
						
							|  |  |  |         sys.exit(0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     RuntimeConfig.DEBUG = args.debug | 
					
						
							|  |  |  |     if RuntimeConfig.DEBUG: | 
					
						
							|  |  |  |         stat_logger.info("run on debug mode") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     RuntimeConfig.init_env() | 
					
						
							|  |  |  |     RuntimeConfig.init_config(JOB_SERVER_HOST=HOST, HTTP_PORT=HTTP_PORT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     peewee_logger = logging.getLogger('peewee') | 
					
						
							|  |  |  |     peewee_logger.propagate = False | 
					
						
							| 
									
										
										
										
											2024-01-17 09:43:27 +08:00
										 |  |  |     # rag_arch.common.log.ROpenHandler | 
					
						
							| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  |     peewee_logger.addHandler(database_logger.handlers[0]) | 
					
						
							|  |  |  |     peewee_logger.setLevel(database_logger.level) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # start http server | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2024-01-17 09:43:27 +08:00
										 |  |  |         stat_logger.info("RAG Flow http server start...") | 
					
						
							| 
									
										
										
										
											2024-01-15 08:46:22 +08:00
										 |  |  |         werkzeug_logger = logging.getLogger("werkzeug") | 
					
						
							|  |  |  |         for h in access_logger.handlers: | 
					
						
							|  |  |  |             werkzeug_logger.addHandler(h) | 
					
						
							|  |  |  |         run_simple(hostname=HOST, port=HTTP_PORT, application=app, threaded=True, use_reloader=RuntimeConfig.DEBUG, use_debugger=RuntimeConfig.DEBUG) | 
					
						
							|  |  |  |     except Exception: | 
					
						
							|  |  |  |         traceback.print_exc() | 
					
						
							|  |  |  |         os.kill(os.getpid(), signal.SIGKILL) |