Skip to content

Commit eca295b

Browse files
authored
Merge pull request #6 from TatraDev/venv_logic
refactor structure project
2 parents da2d677 + d26000e commit eca295b

File tree

13 files changed

+169
-116
lines changed

13 files changed

+169
-116
lines changed

.github/workflows/test.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Python package
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python-version: ["3.9"]
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up Python ${{ matrix.python-version }}
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: ${{ matrix.python-version }}
19+
- name: Install dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
pip install flake8 pytest
23+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
24+
- name: Lint with flake8
25+
run: |
26+
# stop the build if there are Python syntax errors or undefined names
27+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
28+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
29+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
30+
- name: Test with pytest
31+
run: |
32+
pytest --verbose tests/running_piper_test.py::TestVenv

main.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
from piper.services import TestMessageAdder, StringValue, TesseractRecognizer, SpacyNER
2-
from piper.envs import CurrentEnv, DockerEnv, VirtualEnv
3-
from piper.configurations import get_configuration
4-
import time
51
import asyncio
62
import sys
7-
from piper.utils import tesrct_utils as tu
3+
import time
84

95
from loguru import logger
6+
7+
from piper.configurations import get_configuration
8+
from piper.envs import CurrentEnv, DockerEnv, VirtualEnv
9+
# from piper.services import (SpacyNER, StringValue, TesseractRecognizer,
10+
# TestMessageAdder)
11+
# from piper.utils import tesrct_utils as tu
12+
1013
logger.add("file.log", level="INFO", backtrace=True, diagnose=True, rotation='5 MB')
1114

1215
if __name__ == '__main__':
@@ -24,15 +27,15 @@
2427
# print(result)
2528
# adder.rm_container()
2629

27-
logger.info(f'main here {time.time()}')
28-
cfg = get_configuration()
29-
loop = asyncio.get_event_loop()
30-
with DockerEnv() as env:
31-
# object created
32-
recognizer = TesseractRecognizer(port=cfg.docker_app_port)
33-
34-
result = loop.run_until_complete(recognizer())
35-
logger.info(f'result of recognition is {result}')
30+
# logger.info(f'main here {time.time()}')
31+
# cfg = get_configuration()
32+
# loop = asyncio.get_event_loop()
33+
# with DockerEnv() as env:
34+
# # object created
35+
# recognizer = TesseractRecognizer(port=cfg.docker_app_port)
36+
#
37+
# result = loop.run_until_complete(recognizer())
38+
# logger.info(f'result of recognition is {result}')
3639

3740
with VirtualEnv() as env:
3841
env.copy_struct_project()

piper/base/executors.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
from abc import abstractmethod, ABC
2-
from distutils.command.config import config
1+
import inspect
32
import os
3+
import sys
44
import time
5+
from abc import ABC, abstractmethod
6+
from distutils.command.config import config
57
from typing import Dict
6-
import inspect
78

89
import aiohttp
9-
from loguru import logger
1010
import docker
11-
from pydantic import BaseModel #, BytesObject, ListOfStringsObject
11+
import requests
12+
from loguru import logger
13+
from pydantic import BaseModel # , BytesObject, ListOfStringsObject
1214

13-
from piper.base.docker import PythonImage
1415
# from piper.base.docker import PythonTesseractImage
15-
from piper.base.backend.utils import render_fast_api_backend, render_fast_api_tsrct_backend
16-
from piper.envs import is_docker_env, is_current_env, get_env
16+
from piper.base.backend.utils import (render_fast_api_backend,
17+
render_fast_api_tsrct_backend)
18+
from piper.base.docker import PythonImage
1719
from piper.configurations import get_configuration
20+
from piper.envs import get_env, is_current_env, is_docker_env
1821
from piper.utils import docker_utils as du
1922

20-
import requests
21-
import sys
2223

2324
class BaseExecutor:
2425
pass

piper/configurations.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Configuration:
2-
path = "/Users/olegsokolov/PycharmProjects/piper/applications"
3-
path = "/home/pavel/repo/piper_new_out/"
4-
test_path = "/home/pavel/repo/piper_test_out/"
2+
# path = "/Users/olegsokolov/PycharmProjects/piper/applications"
3+
path = "./piper_new_out/"
4+
test_path = "./piper_test_out/"
55
piper_path = "piper"
66
default_env = "docker"
77
docker_app_port = 8788
@@ -17,7 +17,7 @@ class Configuration:
1717

1818
# docker start time and counter
1919
docker_wait_on_iter = 0.5
20-
docker_n_iters = 20
20+
docker_n_iters = 20
2121

2222
n_gunicorn_workers = 1
2323

@@ -58,8 +58,6 @@ class Configuration:
5858
ts_config_row = r'--oem 1 --psm 11'
5959
ts_config = {'ts_lang': 'eng', 'ts_config_row': r'--oem 1 --psm 11'}
6060

61-
62-
6361
# models and where to find them
6462
spacy_models = set(
6563
[
@@ -70,5 +68,6 @@ class Configuration:
7068
)
7169
model_path = '/app/models'
7270

71+
7372
def get_configuration():
7473
return Configuration

piper/services/__init__.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
from piper.base.executors import FastAPIExecutor, FastAPITesseractExecutor, VirtualEnvExecutor
2-
from fastapi.responses import JSONResponse
3-
4-
from pydantic import BaseModel
5-
from loguru import logger
61
import json
7-
import spacy
82
import sys
3+
4+
import spacy
5+
from fastapi.responses import JSONResponse
6+
from loguru import logger
7+
from pydantic import BaseModel
8+
9+
from piper.base.executors import FastAPIExecutor, FastAPITesseractExecutor
910
from piper.configurations import get_configuration
1011
from piper.utils import tesrct_utils as tu
1112

12-
1313
logger.add("file.log", level="INFO", backtrace=True, diagnose=True, rotation='5 MB')
1414

1515

1616
class StringValue(BaseModel):
1717
value: str
1818

19+
1920
class BytesObject(BaseModel):
2021
value: bytes
2122

23+
2224
class ListOfStringsObject(BaseModel):
2325
value: list
2426

27+
2528
class TestMessageAdder(FastAPIExecutor):
2629

2730
def __init__(self, appender="TEST", **kwargs):
@@ -30,18 +33,19 @@ def __init__(self, appender="TEST", **kwargs):
3033

3134
async def run(self, message: StringValue) -> StringValue:
3235
return StringValue(value=(message.value + self.appender))
33-
36+
3437

3538
class TesseractRecognizer(FastAPITesseractExecutor):
3639
'''
3740
Tesseract OCR implementation service
3841
'''
42+
3943
def __init__(self, **kwargs):
4044
super().__init__(**kwargs)
4145
cfg = get_configuration()
4246
self.ts_config = cfg.ts_config
4347

44-
def set_config_(self, config_):
48+
def set_config_(self, config_):
4549
if 'ts_lang' not in config_.keys():
4650
logger.error(f'tesseract config keys must contains ts_lang, keys {config_.keys()}')
4751
logger.error(f'tesseract config did not set')
@@ -59,23 +63,24 @@ async def sconfig(self, conf) -> ListOfStringsObject:
5963
# conf = '12'
6064
logger.info(f'request to set config to {conf}')
6165
self.set_config_(conf)
62-
return JSONResponse(content={'text':'OK'})
63-
64-
async def recognize(self, file_content : BytesObject, suf: str) -> ListOfStringsObject:
66+
return JSONResponse(content={'text': 'OK'})
67+
68+
async def recognize(self, file_content: BytesObject, suf: str) -> ListOfStringsObject:
6569
logger.info(f'file_content {type(file_content)}, file suffix is {suf}')
6670

6771
logger.info(f'current tesseract config is {self.ts_config}')
6872
text_dict = tu.bytes_handler(file_content, suf, self.ts_config)
6973
logger.info(f'img_bytes_handler return {type(text_dict)} object')
7074
return JSONResponse(content=text_dict)
7175

72-
async def ner(self, txt: str):
76+
async def ner(self, txt: str):
7377
sn = SpacyNER()
7478
if sn.available_models and len(sn.available_models) > 0:
7579
dummy_model = sn.available_models[0]
7680
sn.set_model(dummy_model)
7781
return JSONResponse(content=sn.extract_named_ents(txt))
7882

83+
7984
# class ModelNameNotInList(BaseException):
8085
# def __init__(self, msg):
8186
# # pass
@@ -86,6 +91,7 @@ class SpacyNER():
8691
'''
8792
Spacy NER service
8893
'''
94+
8995
def __init__(self):
9096
cfg = get_configuration()
9197
self.available_models = set()
@@ -102,34 +108,32 @@ def __init__(self):
102108
logger.error(f'catch exception {e}')
103109
sys.exit()
104110

105-
106111
def set_model(self, cur_model):
107112
if cur_model not in self.available_models:
108113
logger.error(f'there is not {cur_model} in available_models set: {self.available_models}')
109114
self.nlp = None
110115
raise ValueError(f'there is not {cur_model} in available_models set: {self.available_models}')
111116

112-
try:
117+
try:
113118
nlp = spacy.load(cur_model)
114119
# nlp = spacy.load('en_default')
115120
logger.info('spacy nlp object created with model {cur_model}')
116121
except Exception as e:
117122
logger.error(f'catch exception {e}')
118-
if isinstance(e, OSError):
123+
if isinstance(e, OSError):
119124
logger.error(f'you must download spacy model {cur_model}')
120125
nlp = None
121126
logger.info('spacy nlp object DID NOT create')
122-
123-
self.nlp = nlp
124127

128+
self.nlp = nlp
125129

126130
def extract_named_ents(self, txt: str):
127131
logger.debug(f'got data type {type(txt)} and data <<{txt}>> for NER')
128132
if self.nlp:
129133
res = []
130134
doc = self.nlp(txt)
131135
for ent in doc.ents:
132-
res.append((ent.text, ent.label_))
136+
res.append((ent.text, ent.label_))
133137
return JSONResponse(content=res)
134138
else:
135139
logger.error(f'nlp object didn`t create. you should use set_model(model_name)')

piper/utils/docker_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import docker
2-
import time
31
import sys
2+
import time
3+
4+
import docker
45
from loguru import logger
56

67
from piper.configurations import get_configuration

piper/utils/tesrct_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import cv2
2-
import pytesseract
32
import numpy as np
4-
from loguru import logger
53
import pdf2image
4+
import pytesseract
65
import requests
6+
from loguru import logger
7+
78
from piper.configurations import get_configuration
89

910
cfg = get_configuration()

0 commit comments

Comments
 (0)