1
1
from abc import abstractmethod , ABC
2
+ from distutils .command .config import config
2
3
import os
3
4
import time
4
5
from typing import Dict
7
8
import aiohttp
8
9
from loguru import logger
9
10
import docker
10
- from pydantic import BaseModel
11
+ from pydantic import BaseModel #, BytesObject, ListOfStringsObject
11
12
12
- from piper .base .docker import PythonImage
13
- from piper .base .backend .utils import render_fast_api_backend
13
+ # from piper.base.docker import PythonImage
14
+ from piper .base .docker import PythonTsrImage
15
+ from piper .base .backend .utils import render_fast_api_backend , render_fast_api_tsrct_backend
14
16
from piper .envs import is_docker_env , is_current_env , get_env
15
17
from piper .configurations import get_configuration
16
18
from piper .utils import docker_utils as du
@@ -157,7 +159,7 @@ def __init__(self, port: int = 8080, **service_kwargs):
157
159
158
160
copy_piper (project_output_path )
159
161
copy_scripts (project_output_path , self .scripts ())
160
-
162
+ # build_image(project_output_path, docker_image)
161
163
self .create_fast_api_files (project_output_path , ** service_kwargs )
162
164
163
165
# create and run docker container
@@ -170,15 +172,11 @@ def __init__(self, port: int = 8080, **service_kwargs):
170
172
port
171
173
)
172
174
173
- wait_for_fast_api_app_start ('localhost' , 8788 , 0.5 , 10 )
175
+ wait_for_fast_api_app_start ('localhost' , cfg . docker_app_port , cfg . wait_on_iter , cfg . n_iters )
174
176
else :
175
177
# TODO: Local ENVIRONMENT checks
176
178
pass
177
179
178
- # a = super().__init__('localhost', port, 'hl')
179
- # a.__call__()
180
- # print('hl', a)
181
-
182
180
super ().__init__ ('localhost' , port , self .base_handler )
183
181
184
182
def rm_container (self ):
@@ -189,6 +187,8 @@ def scripts(self):
189
187
return {"service" : inspect .getfile (self .__class__ )}
190
188
191
189
def create_fast_api_files (self , path : str , ** service_kwargs ):
190
+ cfg = get_configuration ()
191
+
192
192
backend = render_fast_api_backend (service_class = self .__class__ .__name__ ,
193
193
service_kwargs = dict (service_kwargs ),
194
194
scripts = self .scripts (),
@@ -202,6 +202,75 @@ def create_fast_api_files(self, path: str, **service_kwargs):
202
202
write_requirements (path , self .requirements )
203
203
204
204
gunicorn = "#!/bin/bash \n " \
205
- "gunicorn -b 0.0.0.0:8080 --workers 4 main:app --worker-class uvicorn.workers.UvicornWorker --preload --timeout 120"
205
+ f"gunicorn -b 0.0.0.0:8080 --workers { cfg .n_gunicorn_workers } main:app --worker-class uvicorn.workers.UvicornWorker --preload --timeout 120 --reload=True"
206
+ with open (f"{ path } /run.sh" , "w" ) as output :
207
+ output .write (gunicorn )
208
+
209
+
210
+ class FastAPITesseractExecutor (HTTPExecutor ):
211
+ requirements = ["gunicorn" , "fastapi" , "uvicorn" , "aiohttp" , "docker" , "Jinja2" , "pydantic" , "loguru" , "numpy" , "opencv-python" , "Pillow" , "pytesseract" , "python-multipart" ]
212
+ base_handler = "recognize"
213
+
214
+ def __init__ (self , port : int = 8080 , ** service_kwargs ):
215
+ self .container = None
216
+ self .image_tag = 'piper:latest'
217
+ self .container_name = "piper_FastAPITsrct"
218
+
219
+ if is_docker_env ():
220
+ docker_client = docker .DockerClient (base_url = 'unix://var/run/docker.sock' )
221
+ cfg = get_configuration ()
222
+ project_output_path = cfg .path
223
+
224
+ copy_piper (project_output_path )
225
+ copy_scripts (project_output_path , self .scripts ())
226
+
227
+ docker_image = PythonTsrImage (self .image_tag , "3.9" , cmd = f"./run.sh" )
228
+ build_image (project_output_path , docker_image )
229
+
230
+ self .create_fast_api_files (project_output_path , ** service_kwargs )
231
+
232
+ # create and run docker container
233
+ # if container exits it will be recreated!
234
+ du .create_image_and_container_by_dockerfile (
235
+ docker_client ,
236
+ project_output_path ,
237
+ self .image_tag ,
238
+ self .container_name ,
239
+ port
240
+ )
241
+
242
+ wait_for_fast_api_app_start ('localhost' , cfg .docker_app_port , cfg .wait_on_iter , cfg .n_iters )
243
+ else :
244
+ # TODO: Local ENVIRONMENT checks
245
+ pass
246
+
247
+ super ().__init__ ('localhost' , port , self .base_handler )
248
+
249
+ def rm_container (self ):
250
+ if self .container :
251
+ self .container .remove (force = True )
252
+
253
+ def scripts (self ):
254
+ return {"service" : inspect .getfile (self .__class__ )}
255
+
256
+ def create_fast_api_files (self , path : str , ** service_kwargs ):
257
+ cfg = get_configuration ()
258
+
259
+ backend = render_fast_api_tsrct_backend (
260
+ service_class = self .__class__ .__name__ ,
261
+ service_kwargs = dict (service_kwargs ),
262
+ scripts = self .scripts (),
263
+ function_name = self .base_handler ,
264
+ # request_model="BytesObject",
265
+ # response_model="ListOfStringsObject"
266
+ )
267
+
268
+ with open (f"{ path } /main.py" , "w" ) as output :
269
+ output .write (backend )
270
+
271
+ write_requirements (path , self .requirements )
272
+
273
+ gunicorn = "#!/bin/bash \n " \
274
+ f"gunicorn -b 0.0.0.0:8080 --workers { cfg .n_gunicorn_workers } main:app --worker-class uvicorn.workers.UvicornWorker --preload --timeout 120"
206
275
with open (f"{ path } /run.sh" , "w" ) as output :
207
276
output .write (gunicorn )
0 commit comments