liangzhongquan 2 anni fa
parent
commit
05d318fb03
2 ha cambiato i file con 27 aggiunte e 4 eliminazioni
  1. 2 2
      environment.yml
  2. 25 2
      server.py

+ 2 - 2
environment.yml

@@ -2,7 +2,7 @@ name: py38
 channels:
   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ # Anocanda清华镜像
   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
-  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
+  # - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
   - bioconda
   - fastai
   - defaults
@@ -14,7 +14,7 @@ dependencies:
   - ipython
   - ipykernel
   - pip
-  - pytorch
+  # - pytorch
   - torchvision
   - torchaudio
   - cudatoolkit=11.0

+ 25 - 2
server.py

@@ -9,13 +9,36 @@ import numpy as np
 import torch
 import base64
 import random
+import sys
+import logging
 
 YOLO_DIR = '/workspace/yolov5'
 # WEIGHTS = '/data/yolov5/runs/train/yolov5x_layout_reuslt37/weights/best.pt'
 WEIGHTS = '/workspace/best.pt'
 
+logger = logging.getLogger('log')
+logger.setLevel(logging.DEBUG)
+
+# 调用模块时,如果错误引用,比如多次调用,每次会添加Handler,造成重复日志,这边每次都移除掉所有的handler,后面在重新添加,可以解决这类问题
+while logger.hasHandlers():
+    for i in logger.handlers:
+        logger.removeHandler(i)
+
+# file log 写入文件配置
+formatter = logging.Formatter('%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s')  # 日志的格式
+fh = logging.FileHandler(r'/var/log/be.log', encoding='utf-8')  # 日志文件路径文件名称,编码格式
+fh.setLevel(logging.DEBUG)  # 日志打印级别
+fh.setFormatter(formatter)
+logger.addHandler(fh)
+
+# console log 控制台输出控制
+ch = logging.StreamHandler(sys.stdout)
+ch.setLevel(logging.DEBUG)
+ch.setFormatter(formatter)
+logger.addHandler(ch)
+
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
-print('====',torch.cuda.is_available())
+logger.info('====',torch.cuda.is_available())
 
 app = FastAPI()
 templates = Jinja2Templates(directory = 'templates')
@@ -28,7 +51,7 @@ colors = [tuple([random.randint(0, 255) for _ in range(3)]) for _ in range(100)]
 
 if model_dict['ocr-layout'] is None:
           model_dict['ocr-layout'] = model = torch.hub.load(YOLO_DIR, 'custom', path=WEIGHTS, source='local').to(device)
-          print("========>加载成功")
+          logger.info("========>加载成功")
 
 
 ##############################################