最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】把NLP部分的代码合并到当前Flask的app中

app crifan 2111浏览 0评论

之前已经写好了自己的Flask的app的代码:

提供了对外的REST接口。

现在需要把别人的NLP部分的代码:

合并进来,实现真正的AI的对话功能。

此处至少会涉及到:

  • 导入的路径的配置和优化

  • 如何调用对方的Class去实现真正的AI

  • 其他一些可能的问题

先把代码放进来再说

然后再去一点点整合。

然后出错:

【已解决】gunicorn运行Flask的app出错:gunicorn.errors.HaltServer HaltServer Worker failed to boot 3

然后继续去测试接口,看看是否出错。

结果传入:

失败:

log:

结果找到

logs/gunicorn_error.log

中的原因:

<code>  File "ai/nlp/dialog/intent.py", line 12, in __init__
    self.parse(query)
  File "ai/nlp/dialog/intent.py", line 37, in parse
    config.readfp(open("config/config.ini"))
FileNotFoundError: [Errno 2] No such file or directory: 'config/config.ini'
</code>

然后继续去修改代码

<code>config = configparser.ConfigParser()
# config.readfp(open("config/config.ini"))
configIniFilePath = os.path.join(self.projectRootPath, "ai/nlp/dialog/config/config.ini")
# print("configIniFilePath=%s" % (configIniFilePath))
configIniFp = open(configIniFilePath)
# print("configIniFp=%s" % (configIniFp))
</code>

就可以了。

后续又遇到其他的类似的导入的问题。

继续去修改代码。

相关部分修改为:

ai/nlp/dialog/intent.py

<code>import os

class DialogInput(object):

    def __init__(self, query, domain=None, slots=None, score=None):

        self.dialogRootPath = os.path.join(os.getcwd(), "ai/nlp/dialog")

    def parse(self, query):

        config = configparser.ConfigParser()
        # config.readfp(open("config/config.ini"))
        configIniFilePath = os.path.join(self.dialogRootPath, "config/config.ini")
        # print("configIniFilePath=%s" % (configIniFilePath))
        configIniFp = open(configIniFilePath)
        # print("configIniFp=%s" % (configIniFp))
        config.readfp(configIniFp)
        maxDomain = ""
        maxSlots = ""
        maxScore = 0
        for domain in config["regexfile"]:
            filepath = config.get("regexfile", domain)
            # print(filepath)
            # for line in open(filepath):
            for line in open(os.path.join(self.dialogRootPath, filepath)):
</code>

然后又出现其他错误:

【已解决】pymysql连接出错:pymysql.err.OperationalError 2003 Can’t connect to MySQL server on mysql.rds.aliyuncs.com Errno 61 Connection refused

至此,终于把NLP部分的代码,合并到当前Flaks的app了。

后面就是自己优化接口的事情了。

目前,改为:

<code>from flask import Flask
from flask import jsonify
from flask_restful import Resource, Api, reqparse
import logging
from logging.handlers import RotatingFileHandler
# from flask_pymongo import PyMongo
from gridfs import GridFS
from pymongo import MongoClient
from bson.objectid import ObjectId
from flask import send_file
import io
import re

import json

import sys
sys.path.append("ai")
...
sys.path.append("ai/util")

from DialogueManager import Context
from intent import DialogInput
from AccessData import AccessData
from GenerateResponse import GenerateResponse

################################################################################
# Global Function
################################################################################

# NLP AI related
def QueryAnalyse(query, context):
    result = {'mediaId': None, 'response': None, 'control' : None}
    semantic = DialogInput(query)
    if semantic.domain == "control":
        result['control'] = semantic.slots["command"]
    mediaresult = AccessData(semantic,context)
    if mediaresult and len(mediaresult) &gt; 0:
        result['mediaId'] = mediaresult[0][3]
    result['response'] = GenerateResponse(mediaresult,semantic)
    context.AddTurn(semantic, mediaresult)
    return result


################################################################################
# Global Init App
################################################################################
app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')
# app.config.from_object('config.ProductionConfig')

logFormatterStr = app.config["LOG_FORMAT"]
logFormatter = logging.Formatter(logFormatterStr)

fileHandler = RotatingFileHandler(
    app.config['LOG_FILE_FILENAME'],
    maxBytes=app.config["LOF_FILE_MAX_BYTES"],
    backupCount=app.config["LOF_FILE_BACKUP_COUNT"],
    encoding="UTF-8")
fileHandler.setLevel(logging.DEBUG)
fileHandler.setFormatter(logFormatter)
app.logger.addHandler(fileHandler)

streamHandler = logging.StreamHandler()
streamHandler.setFormatter(logFormatter)
streamHandler.setLevel(logging.INFO)
app.logger.addHandler(streamHandler)

app.logger.setLevel(logging.DEBUG) # set root log level

log = app.logger
log.debug("app=%s", app)
log.info("app.config=%s", app.config)

api = Api(app)
log.info("api=%s", api)

aiContext = Context()
log.info("aiContext=%s", aiContext)

purePymongo = MongoClient(
    host=app.config["MONGODB_HOST"],
    port=app.config["MONGODB_PORT"],
    username=app.config["MONGODB_USERNAME"],
    password=app.config["MONGODB_PASSWORD"],
    authSource=app.config["MONGODB_AUTH_SOURCE"]
)
log.info("purePymongo=%s", purePymongo)
# mongoServerInfo = purePymongo.server_info()
# log.debug("mongoServerInfo=%s", mongoServerInfo)

# Pure PyMongo
gridfsDb = purePymongo.gridfs
log.info("gridfsDb=%s", gridfsDb)
fsCollection = GridFS(gridfsDb)
log.info("fsCollection=%s", fsCollection)


class RobotQaAPI(Resource):

    def get(self):
        respDict = {
            "code": 200,
            "message": "generate response ok",
            "data": {
                "input": "",
                "response": "",
                "control": "",
                "audio": {}
            }
        }

        parser = reqparse.RequestParser()
        # i want to hear the story of Baby Sister Says No
        parser.add_argument('input', type=str, help="input words")
        log.info("parser=%s", parser)

        parsedArgs = parser.parse_args()  #
        log.info("parsedArgs=%s", parsedArgs)
        if not parsedArgs:
            respDict["data"]["response"] = "Can not recognize input"
            return jsonify(respDict)

        inputStr = parsedArgs["input"]
        log.info("inputStr=%s", inputStr)

        if not inputStr:
            respDict["data"]["response"] = "Can not recognize parameter input"
            return jsonify(respDict)

        respDict["data"]["input"] = inputStr

        aiResult = QueryAnalyse(inputStr, aiContext)
        log.info("aiResult=%s", aiResult)

        respDict["data"]["response"] = aiResult["response"]
        respDict["data"]["control"] = aiResult["control"]
        log.info('respDict["data"]=%s', respDict["data"])

        audioFileIdStr = aiResult["mediaId"]
        log.info("audioFileIdStr=%s", audioFileIdStr)

        audioFileObjectId = ObjectId(audioFileIdStr)
        log.info("audioFileObjectId=%s", audioFileObjectId)
        # audioFileObj = fsCollection.find_one({"_id": audioFileObjectId})
        # audioFileObj = fsCollection.find({"_id": audioFileObjectId})
        # audioFileObj = fsCollection.find_one({"_id": audioFileIdStr})
        # audioFileObj = fsCollection.get(audioFileObjectId)
        # audioFileObj = fsCollection.get(audioFileIdStr)
        # audioFileObj = fsCollection.find_one({"_id": audioFileIdStr})

        # findFileCursor = fsCollection.find({"_id": audioFileObjectId})
        # log.info("audioFileObj=%s", findFileCursor)
        # for curIdx, eachFile in enumerate(findFileCursor):
        #     curNum = curIdx + 1
        #     log.info("[%2d] _id=%s, contentType=%s, filename=%s", curNum, eachFile._id, eachFile.contentType, eachFile.filename)

        respDict["data"]["audio"] = {
            "url": "http://%s:%d/files/%s/%s" %
                   (app.config["FILE_URL_HOST"],
                    app.config["FLASK_PORT"],
                    audioFileIdStr,
                    "fake_filename.mp3")
        }
        log.info("respDict=%s", respDict)
        return jsonify(respDict)


        # # if fsCollection.exists(audioFileObjectId):
        # if fsCollection.exists(audioFileIdStr):
        #     # audioFileObj = fsCollection.get(audioFileObjectId)
        #     audioFileObj = fsCollection.get(audioFileIdStr)
        #     log.info("audioFileObj=%s", audioFileObj)
        #
        #     respDict["data"]["audio"] = {
        #         "contentType": audioFileObj.contentType,
        #         "name": audioFileObj.filename,
        #         "size": audioFileObj.length,
        #         "url": "http://%s:%d/files/%s/%s" %
        #                (app.config["FILE_URL_HOST"],
        #                 app.config["FLASK_PORT"],
        #                 audioFileObj._id,
        #                 audioFileObj.filename)
        #     }
        #     log.info("respDict=%s", respDict)
        #     return jsonify(respDict)
        # else:
        #     log.warning("Can not find file from id %s", audioFileIdStr)
        #     respDict["data"]["audio"] = {}
        #     return jsonify(respDict)

api.add_resource(RobotQaAPI, '/qa', endpoint='qa')

if __name__ == "__main__":
    app.run(
        host=app.config["FLASK_HOST"],
        port=app.config["FLASK_PORT"],
        debug=app.config["DEBUG"]
    )
</code>

去返回响应。

其他的nlp的代码,就不贴了。

转载请注明:在路上 » 【已解决】把NLP部分的代码合并到当前Flask的app中

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.225 seconds, using 22.12MB memory