我已经编写了使用HTML和python创建ChatBot的代码。我使用了python 3.8,但在尝试执行表单时失败。我不知道它怎么会失败,因为我使用了最新版本的python。我还尝试了一些已发布的解决方案,但它们对我也不起作用。为我提供一些解决方案。
这是代码:
app.py
#import files
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("Python-BOT")
trainer = ListTrainer(bot)
trainer.train(['what is your name?', 'My name is Python-BOT'])
trainer.train(['who are you?', 'I am a BOT'])
trainer = ChatterBotCorpusTrainer(bot)
trainer.train("chatterbot.corpus.english")
@app.route("/")
def index():
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()
index.html
<!DOCTYPE html>
<html>
<head>
<title>Python-BOT</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body,
html {
height: 100%;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
#chatbox {
margin-left: auto;
margin-right: auto;
width: 40%;
margin-top: 60px;
}
#userInput {
margin-left: auto;
margin-right: auto;
width: 40%;
margin-top: 60px;
}
#textInput {
width: 90%;
border: none;
border-bottom: 3px solid black;
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: #808080;
padding: 10px;
border-radius: 2px;
}
.botText {
color: white;
font-family: monospace;
font-size: 17px;
text-align: left;
line-height: 30px;
}
.botText span {
background-color: #4169e1;
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 green;
}
</style>
</head>
<body>
<div>
<h1 align="center">
<b>Welcome to Python-BOT</b>
</h1>
</div>
</body>
</html>
这是我尝试在Python 3.8版本中执行结果时遇到的错误
F:\Projects\python_projects\ChatBot-sample-3>"F:/Installed Software/python/phython 3.8/python.exe" f:/Projects/python_projects/ChatBot-sample-3/app.py
Traceback (most recent call last):
File "f:/Projects/python_projects/ChatBot-sample-3/app.py", line 9, in <module>
bot = ChatBot("Python-BOT")
File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\chatterbot\chatterbot.py", line 34, in __init__
self.storage = utils.initialize_class(storage_adapter, **kwargs)
File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\chatterbot\utils.py", line 54, in initialize_class
return Class(*args, **kwargs)
File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\chatterbot\storage\sql_storage.py", line 22, in __init__
from sqlalchemy import create_engine
File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\__init__.py", line 8, in <module>
from . import util as _util # noqa
File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\__init__.py", line 14, in <module>
from ._collections import coerce_generator_arg # noqa
File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\_collections.py", line 16, in <module>
from .compat import binary_types
File "C:\Users\AMASHI SANDUNIKA\AppData\Roaming\Python\Python38\site-packages\sqlalchemy\util\compat.py", line 264, in <module>
time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'
答案 0 :(得分:0)
发生错误是因为在python 2中有time.clock()
,但是在python 3中它已被time.perf_counter()
取代。
只需将所有time.clock
替换为time.perf_counter
,就可以了。有关更多信息:https://www.webucator.com/blog/2015/08/python-clocks-explained/
答案 1 :(得分:-1)
试试这个对我有用:
if win32 or jython:
try: # Python 3.4+
preferred_clock = time.perf_counter
except AttributeError: # Earlier than Python 3.
preferred_clock = time.clock
else:
time_func = time.time