liangzhongquan 2 éve
szülő
commit
f1fbdf77a5
3 módosított fájl, 46 hozzáadás és 2 törlés
  1. 1 1
      docker-compose.yml
  2. 2 1
      server.py
  3. 43 0
      utils/web-util.py

+ 1 - 1
docker-compose.yml

@@ -4,7 +4,7 @@ services:
     hostname: layoutv2
     container_name: layoutv2
     restart: always
-    image: layout:v2
+    image: registry.cn-hangzhou.aliyuncs.com/sxtest/layout:gpu:latest
     privileged: true
     ipc: host
     tty: true

+ 2 - 1
server.py

@@ -2,7 +2,7 @@ from fastapi import FastAPI, Request, Form, File, UploadFile
 from fastapi.templating import Jinja2Templates
 from pydantic import BaseModel
 from typing import List, Optional
-
+from utils.web-util import web_try
 import cv2
 import numpy as np
 
@@ -101,6 +101,7 @@ def detect_via_web_form(request: Request,
 
 
 @app.post("/detect")
+@web_try()
 def detect_via_api(request: Request,
                 file_list: List[UploadFile] = File(...),
                 model_name: str = Form(...),

+ 43 - 0
utils/web-util.py

@@ -0,0 +1,43 @@
+import traceback
+import numpy as np
+from decorator import decorator
+from fastapi.responses import JSONResponse
+
+
+def json_compatible(data):
+    if isinstance(data,dict):
+        return {k:json_compatible(v) for k,v in data.items()}
+    if isinstance(data,bytes):
+        return str(data)
+    if isinstance(data,np.ndarray):
+        return data.tolist()
+    return data
+
+def web_try(exception_ret=None):
+    @decorator
+    def f(func, *args, **kwargs):
+        error_code = "000"
+        ret = None
+        msg = ''
+        try:
+            ret = func(*args, **kwargs)
+        except Exception as e:
+            msg = traceback.format_exc()
+            if len(e.args) > 0 and isinstance(e.args[0], int):
+                error_code = e.args[0]
+            else:
+                error_code = "101"
+            print('--------------------------------')
+            print ('Get Exception in web try :( \n{}\n'.format(msg))
+            print('--------------------------------')
+            if callable(exception_ret):
+                ret = exception_ret()
+            else:
+                ret = exception_ret
+        finally:
+            if ret is not None and isinstance(ret, JSONResponse):
+                return ret
+            return json_compatible({"status": error_code,
+                                    "result": ret,
+                                    "msg": msg.split('\n')[-2] if msg is not '' else msg})
+    return f