evgeniy778 commited on
Commit
5aedb0f
·
verified ·
1 Parent(s): fffccb6

Update Compute Adapter to Version 1.1

Browse files
Files changed (1) hide show
  1. compute_adapter.py +82 -65
compute_adapter.py CHANGED
@@ -1,65 +1,99 @@
1
  # =====================================================
2
  # Apckeyl Framework
3
- # Version 1.0
4
  # compute_adapter.py
5
  # =====================================================
6
 
7
  """
8
- Apckeyl Compute Adapter.
9
 
10
- Version 1.0
11
 
12
- Adapter является прослойкой между
13
- Control Plane / Dispatcher и внешним
14
- Compute Module.
15
 
16
- На этом этапе реальное сетевое
17
- подключение НЕ выполняется.
18
  """
19
 
 
 
20
 
21
  # =====================================================
22
- # Base Compute Adapter
23
  # =====================================================
24
 
25
  class ComputeAdapter:
26
 
27
  def __init__(
28
  self,
29
- module_id,
30
- module_name,
31
- endpoint=None,
32
  ):
33
 
34
- self.module_id = module_id
35
 
36
- self.module_name = module_name
 
 
37
 
38
- self.endpoint = endpoint
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
 
41
  # =================================================
42
  # Module Information
43
  # =================================================
44
 
45
- def get_info(self):
46
 
47
- return {
 
 
48
 
49
- "module_id": self.module_id,
50
 
51
- "module_name": self.module_name,
 
 
52
 
53
- "endpoint": self.endpoint,
54
 
55
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
 
58
  # =================================================
59
- # Dispatch
60
  # =================================================
61
 
62
- def dispatch(
63
  self,
64
  task,
65
  ):
@@ -70,7 +104,6 @@ class ComputeAdapter:
70
  "Task cannot be None"
71
  )
72
 
73
-
74
  if not isinstance(
75
  task,
76
  dict,
@@ -80,7 +113,6 @@ class ComputeAdapter:
80
  "Task must be a dictionary"
81
  )
82
 
83
-
84
  task_id = task.get(
85
  "task_id"
86
  )
@@ -91,56 +123,41 @@ class ComputeAdapter:
91
  "Task must contain task_id"
92
  )
93
 
 
 
 
94
 
95
- return {
96
-
97
- "status": "prepared",
98
-
99
- "module_id": self.module_id,
100
 
101
- "module_name": self.module_name,
 
 
102
 
103
- "task_id": task_id,
 
 
104
 
105
- "task_type": task.get(
106
- "task_type"
107
- ),
108
 
109
- "payload": task.get(
110
- "payload"
111
- ),
112
 
113
- }
 
 
 
 
114
 
115
 
116
  # =====================================================
117
- # RealESRGAN Adapter
118
  # =====================================================
119
 
120
- class RealESRGANAdapter(
121
- ComputeAdapter
122
  ):
123
 
124
- def __init__(
125
- self,
126
- endpoint=None,
127
- ):
128
-
129
- super().__init__(
130
-
131
- module_id="realesrgan",
132
-
133
- module_name="Apckeyl_RealESRGAN",
134
-
135
- endpoint=endpoint,
136
-
137
- )
138
-
139
-
140
- # =====================================================
141
- # Default Adapter
142
- # =====================================================
143
-
144
- realesrgan_adapter = (
145
- RealESRGANAdapter()
146
- )
 
1
  # =====================================================
2
  # Apckeyl Framework
3
+ # Version 1.1
4
  # compute_adapter.py
5
  # =====================================================
6
 
7
  """
8
+ Compute Adapter.
9
 
10
+ Version 1.1
11
 
12
+ Адаптер между Apckeyl Framework
13
+ и конкретным Compute Module.
 
14
 
15
+ На этом этапе реальный внешний модуль
16
+ Apckeyl_RealESRGAN ещё НЕ подключается.
17
  """
18
 
19
+ from compute_module import ComputeModule
20
+
21
 
22
  # =====================================================
23
+ # Compute Adapter
24
  # =====================================================
25
 
26
  class ComputeAdapter:
27
 
28
  def __init__(
29
  self,
30
+ compute_module,
 
 
31
  ):
32
 
33
+ if compute_module is None:
34
 
35
+ raise ValueError(
36
+ "compute_module is required"
37
+ )
38
 
39
+ if not isinstance(
40
+ compute_module,
41
+ ComputeModule,
42
+ ):
43
+
44
+ raise TypeError(
45
+ "compute_module must be "
46
+ "an instance of ComputeModule"
47
+ )
48
+
49
+ self.compute_module = (
50
+ compute_module
51
+ )
52
 
53
 
54
  # =================================================
55
  # Module Information
56
  # =================================================
57
 
58
+ def get_module_info(self):
59
 
60
+ return (
61
+ self.compute_module.get_info()
62
+ )
63
 
 
64
 
65
+ # =================================================
66
+ # Health Check
67
+ # =================================================
68
 
69
+ def health_check(self):
70
 
71
+ return (
72
+ self.compute_module.health_check()
73
+ )
74
+
75
+
76
+ # =================================================
77
+ # Task Support
78
+ # =================================================
79
+
80
+ def supports_task(
81
+ self,
82
+ task_type,
83
+ ):
84
+
85
+ return (
86
+ self.compute_module.supports_task(
87
+ task_type
88
+ )
89
+ )
90
 
91
 
92
  # =================================================
93
+ # Execute Task
94
  # =================================================
95
 
96
+ def execute(
97
  self,
98
  task,
99
  ):
 
104
  "Task cannot be None"
105
  )
106
 
 
107
  if not isinstance(
108
  task,
109
  dict,
 
113
  "Task must be a dictionary"
114
  )
115
 
 
116
  task_id = task.get(
117
  "task_id"
118
  )
 
123
  "Task must contain task_id"
124
  )
125
 
126
+ task_type = task.get(
127
+ "task_type"
128
+ )
129
 
130
+ if not task_type:
 
 
 
 
131
 
132
+ raise ValueError(
133
+ "Task must contain task_type"
134
+ )
135
 
136
+ if not self.supports_task(
137
+ task_type
138
+ ):
139
 
140
+ raise ValueError(
 
 
141
 
142
+ f"Unsupported task type: "
143
+ f"{task_type}"
144
+ )
145
 
146
+ return (
147
+ self.compute_module.execute(
148
+ task
149
+ )
150
+ )
151
 
152
 
153
  # =====================================================
154
+ # Adapter Factory
155
  # =====================================================
156
 
157
+ def create_compute_adapter(
158
+ compute_module,
159
  ):
160
 
161
+ return ComputeAdapter(
162
+ compute_module
163
+ )