fengmiguoji commited on
Commit
27454f8
·
verified ·
1 Parent(s): 12adb21

Upload api\app_factory.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. api//app_factory.py +101 -0
api//app_factory.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import time
3
+
4
+ from configs import dify_config
5
+ from dify_app import DifyApp
6
+
7
+
8
+ # ----------------------------
9
+ # Application Factory Function
10
+ # ----------------------------
11
+ def create_flask_app_with_configs() -> DifyApp:
12
+ """
13
+ create a raw flask app
14
+ with configs loaded from .env file
15
+ """
16
+ dify_app = DifyApp(__name__)
17
+ dify_app.config.from_mapping(dify_config.model_dump())
18
+
19
+ return dify_app
20
+
21
+
22
+ def create_app() -> DifyApp:
23
+ start_time = time.perf_counter()
24
+ app = create_flask_app_with_configs()
25
+ initialize_extensions(app)
26
+ end_time = time.perf_counter()
27
+ if dify_config.DEBUG:
28
+ logging.info(f"Finished create_app ({round((end_time - start_time) * 1000, 2)} ms)")
29
+ return app
30
+
31
+
32
+ def initialize_extensions(app: DifyApp):
33
+ from extensions import (
34
+ ext_app_metrics,
35
+ ext_blueprints,
36
+ ext_celery,
37
+ ext_code_based_extension,
38
+ ext_commands,
39
+ ext_compress,
40
+ ext_database,
41
+ ext_hosting_provider,
42
+ ext_import_modules,
43
+ ext_logging,
44
+ ext_login,
45
+ ext_mail,
46
+ ext_migrate,
47
+ ext_proxy_fix,
48
+ ext_redis,
49
+ ext_sentry,
50
+ ext_set_secretkey,
51
+ ext_storage,
52
+ ext_timezone,
53
+ ext_warnings,
54
+ )
55
+
56
+ extensions = [
57
+ ext_timezone,
58
+ ext_logging,
59
+ ext_warnings,
60
+ ext_import_modules,
61
+ ext_set_secretkey,
62
+ ext_compress,
63
+ ext_code_based_extension,
64
+ ext_database,
65
+ ext_app_metrics,
66
+ ext_migrate,
67
+ ext_redis,
68
+ ext_storage,
69
+ ext_celery,
70
+ ext_login,
71
+ ext_mail,
72
+ ext_hosting_provider,
73
+ ext_sentry,
74
+ ext_proxy_fix,
75
+ ext_blueprints,
76
+ ext_commands,
77
+ ]
78
+ for ext in extensions:
79
+ short_name = ext.__name__.split(".")[-1]
80
+ is_enabled = ext.is_enabled() if hasattr(ext, "is_enabled") else True
81
+ if not is_enabled:
82
+ if dify_config.DEBUG:
83
+ logging.info(f"Skipped {short_name}")
84
+ continue
85
+
86
+ start_time = time.perf_counter()
87
+ ext.init_app(app)
88
+ end_time = time.perf_counter()
89
+ if dify_config.DEBUG:
90
+ logging.info(f"Loaded {short_name} ({round((end_time - start_time) * 1000, 2)} ms)")
91
+
92
+
93
+ def create_migrations_app():
94
+ app = create_flask_app_with_configs()
95
+ from extensions import ext_database, ext_migrate
96
+
97
+ # Initialize only required extensions
98
+ ext_database.init_app(app)
99
+ ext_migrate.init_app(app)
100
+
101
+ return app