当前位置:   article > 正文

python聊天机器人接口+前端界面展示_如何将人和人工智能在终端的聊天显示在前端

如何将人和人工智能在终端的聊天显示在前端

效果展示

说明

本项目是在前端界面显示,基于医疗知识图谱+bert文本相似度+seq2seq attention的中文聊天机器人

代码、项目报告详见githubhttps://github.com/Changanyue/medical-chatbot

由于data和预训练参数过大,未上传至github

运行效果

视频:
国内bilibili:https://www.bilibili.com/video/BV1Re411W7LX/

国外YouTube:https://www.youtube.com/watch?v=Vjo8qHwGIAg&feature=youtu.be

说一下关键实现点

前端 script.js

    //发送
    addMessage: function() {
		let msg = this.$textarea.val()		
        let that = this 
        $.ajax({
            url: 'http://localhost:5000/api',
            method: "post",
            dataType: 'json',
            type:"post",
            data: {question:msg},
            headers:{
                "Access-Control-Allow-Origin":true,
            },
            success: function(res) {
                that.messageToSend = that.$textarea.val()
                that.render(res.answer);
            
            },
            error: function(e){
                console.log(e)
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

接口用flask实现,这里给一个简单的demo:



from flask import Flask, request
from flask_cors import  CORS
app = Flask(__name__)
CORS(app, supports_credentials=True)

@app.route('/api', methods=['GET', 'POST'])
def indextest():
    if request.method == 'GET':
        question = request.args
        question = question['question']
        answer = GetQuestionAns(question)    #这里是自己定义的根据问题给出回答的函数
        return {'answer': answer}

    elif request.method == 'POST':
        question = request.form["question"]
        answer = GetQuestionAns(question)
        return {'answer': answer}


@app.route('/', methods=['GET', 'POST'])
def a():
    return request.form['question']

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000,debug=True)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/343282
推荐阅读
相关标签
  

闽ICP备14008679号