# coding:utf-8 # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License" # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from paddlenlp.server import BaseTaskflowHandler from paddlenlp.utils.log import logger
@classmethod defprocess(cls, predictor, data, parameters): if data isNone: return {} text = None iflen(data) > 0: text = data else: return {} if parameters isnotNone : schema = parameters predictor.set_schema(schema) try: result = predictor(text) format_result = format_response(result) return format_result except Exception as e: return {"result": RESP_FAILURE, "response": e, "history": []}
defformat_response(result,schema): rw={} for r in result: iflen(r) == 0: continue for w in r.values(): for t in w: if t['text'] == '': continue rw[t['text']]=t return {"result": RESP_SUCCESS, "response": list(rw.keys()), "history":[]}
web服务类 TrsUieServer.py 代码如下
1 2 3 4 5 6 7 8 9 10 11
from paddlenlp import Taskflow from trs_server import TrsSimpleServer from trs_taskflow_handler import TrsTaskflowHandler # The schema changed to your defined schema schema = ["人物"] # The task path changed to your best model path uie = Taskflow("information_extraction", schema=schema, task_path="../../checkpoint/model_best/") # If you want to define the finetuned uie service trsApp = TrsSimpleServer() trsHandler = TrsTaskflowHandler() trsApp.register_taskflow("chat", uie,trsHandler)
启动就只需要 执行 如下命令
1
paddle server TrsUieServer:trsApp --host 0.0.0.0 --port 9090