#!/usr/bin/env python
# encoding: utf-8
"""
@author: simin. Gong
@license: (C) Copyright 2013-2023 Spacemit Corporation Limited.
@contact: simin.gong@spacemit.com
@file: app.py.py
@date: 2024/11/6 16:58 
@desc:
"""

from flask import Flask
from flask_cors import CORS
from flask_socketio import SocketIO
import os
import sys
from ai_tool import aiLib
lib_path = os.path.dirname(aiLib.__file__)

sys.path.append(lib_path)



home_path = os.path.expanduser("~")

save_path = os.path.join(home_path, '.ai-tmp')

try:
    import Classifi as cls
    import yolov8 as yolo
    import ObjectSuperpoint as Superpoint
    import PPOCRRec as ocr
    import ObjectMask as mask
    import cv2

    onnx_path = os.path.join(lib_path, 'mobilenet_v2.q.onnx')

    yolo_path = os.path.join(lib_path, 'yolo11n.onnx')

    super_point_path = os.path.join(lib_path, 'superpoint_512_480x640.onnx')

    ocr_path = os.path.join(lib_path, 'ppocrv4_rec_new.onnx')

    mask_path = os.path.join(lib_path, 'model_627_new.onnx')

    print('加载class 模型')
    cls_infer = cls.create_infer(onnx_path, 'onnx')

    print('加载yolo 模型')
    yolo_infer = yolo.create_infer(yolo_path, 'onnx')

    print('加载superpoint 模型')
    super_point_infer = Superpoint.create_infer(super_point_path)

    print('加载ocr模型')
    ocr_infer = ocr.create_infer(ocr_path)

    print('加载seaformer模型')
    mask_infer = mask.create_infer(mask_path)
except Exception:
    print("\033[31m\***************** not riscv env! ******************* /\033[0m")

from ai_tool.io_namespace.AiDomainNamespace import AiDomainNamespace
from ai_tool.io_namespace.OllamaNamespace import OllamaNamespace

app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, async_mode='threading', cors_allowed_origins="*")
app.config['SECRET_KEY'] = 'secret!'
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024


socketio.on_namespace(OllamaNamespace('/llm-chat'))
socketio.on_namespace(AiDomainNamespace('/ai-domain'))

def io_run():
    socketio.run(app, host='0.0.0.0', port=5001,  allow_unsafe_werkzeug=True)

if __name__ == '__main__':
    io_run()
