问题描述
我正在学习如何编写聊天机器人程序,并且正在在线学习如何使用Flask开发简单聊天机器人的教程。但是,尽管我根据自己的喜好(例如HTML等)设计了Flask网站,但似乎无法从Bot得到响应。如果我发送“ hello”消息,则不会返回任何内容。我正在使用带有Python的chatterbot软件包。我想训练我的机器人以能够谈论其他主题作为最终目标,而只是从默认的chatterbot响应开始。哦,顺便说一句,我没有收到来自PowerShell的错误消息。以下是我的Python和HTML代码。预先感谢您的帮助。
Python:
from flask import Flask,render_template,request
from chatterbot import chatbot
from chatterbot.trainers import chatterbotCorpusTrainer
from chatterbot.trainers import ListTrainer
app = Flask(__name__)
bot = chatbot('Bob')
trainer = chatterbotCorpusTrainer(bot)
trainer.train("chatterbot.corpus.english")
@app.route("/")
def home():
return render_template("index.html")
@app.route("/get")
def get_bot_response():
userText = request.args.get('msg')
return str(bot.get_response(userText))
if __name__ == "__main__":
app.run()
HTML:
<html lang="en" dir="ltr">
<head>
<Meta charset="utf-8">
<title>chatbot</title>
<link rel="stylesheet" type= "text/css" href="{{ url_for('static',filename='styles/style.css') }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
body {
font-family: monospace;
color: #fff;
}
h1 {
display: inline-block;
font-size: 3em;
margin: 0;
padding: 14px;
}
h3 {
color: black;
font-size: 20px;
margin-top: 3px;
text-align: center;
}
#chatBox {
margin-left: auto;
margin-right: auto;
width: 40%;
margin-top: 60px;
}
#userInput {
margin-left: auto;
margin-right: auto;
width: 40%;
margin-top: 70px;
}
#textInput {
width:40%;
border: none;
border-bottom: 5px solid #00cc99;
font-family: monospace;
font-size: 17px;
}
.userText {
color: white;
font-family: monospace;
font-size: 17px;
text-align: right;
line-height: 30px;
}
.userText span {
background-color: #4169e1;
padding: 10px;
border-radius: 2px;
}
.bottext {
color: white;
font-family: monospace;
font-size: 17px;
text-align: left;
line-height: 30px;
}
.bottext span {
background-color: #a4c639;
padding: 10px;
border-radius: 2px;
}
#tidbit {
position: absolute;
bottom: 0;
right: 0;
width: 300px;
}
.Boxed {
margin-left: auto;
margin-right: auto;
width: 78%;
margin-top: 60px;
border: 1px solid #00cc99;
}
.Box {
border: 2px solid #00cc99;
}
</style>
</head>
<body>
<center>
<h1 class="h1">Welcome to my chatbot</h1>
<h2>Hello! My name is Bob. Your personal chatbot!</h2>
<div class="Box"></div>
<div class="Boxed">
<div>
<div id="chatBox">
<img src="{{url_for('static',filename= 'robot.jpg')}}" alt="bob" width="60px" height= "60px"/>
<p class="bottext">
<span>Hi! Nice to meet you!</span>
</p>
</div>
<div class="userInput">
<input id="textInput" type="text" name="msg" placeholder="Message" />
</div>
</div>
<script>
function getBotResponse() {
var rawText = $("#textInput").val();
var userHtml = '<p class="userText"><span>' + rawText + "</span></p>";
$("#textInput").val("");
$("#chatBox").append(userHtml);
document
.getElementById("userInput")
.scrollIntoView({ block: "start",behavior: "smooth" });
$.get("/get",{ msg: rawText }).done(function (data) {
var botHtml = '<p class="bottext"><span>' + data + "</span></p>";
$("#chatBox").append(botHtml);
document
.getElementById("userInput")
.scrollIntoView({ block: "start",behavior: "smooth" });
});
}
$("#textInput").keypress(function (e) {
if (e.which == 13) {
getBotResponse();
}
});
</script>
</div>
</body>
</html>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)