首页 > 编程语言 >Python——聊天机器人

Python——聊天机器人

时间:2024-05-30 22:47:13浏览次数:17  
标签:__ userText get Python 机器人 color 聊天 font margin

前端代码

 1 # 导入需要的包和库
 2 from chatterbot import ChatBot
 3 from chatterbot.trainers import ListTrainer, ChatterBotCorpusTrainer
 4 from flask import Flask, render_template, request
 5 
 6 # 创建Flask应用
 7 app = Flask(__name__)
 8 
 9 # 创建一个聊天机器人实例
10 bot = ChatBot("chatbot", read_only=False,
11               logic_adapters=[{
12                   "import_path": "chatterbot.logic.BestMatch",
13                   "default_response": "sorry,I don't know the answer",
14                   "maximum_similarity_threshold": 0.9
15               }]
16               )
17 
18 # 使用ChatterBotCorpusTrainer训练机器人
19 trainer = ChatterBotCorpusTrainer(bot)
20 trainer.train("chatterbot.corpus.english")
21 
22 # 首页路由
23 @app.route("/")
24 def main():
25     return render_template("index.html")  # 渲染主页模板
26 
27 # 处理与聊天机器人的交互
28 @app.route("/get")
29 def get_chatbot_response():
30     userText = request.args.get('userMessage')  # 获取用户输入的文本
31     return str(bot.get_response(userText))  # 返回机器人对用户文本的回复
32 
33 # 启动Flask应用
34 if __name__ == "__main__":
35     app.run(debug=True)

后端代码

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6     <title>Document</title>
  7     <script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
  8 
  9     <style>
 10 
 11 
 12           body{
 13             font-family: monospace;
 14           }
 15 
 16           #chatbox{
 17             margin-left: auto;
 18             margin-right: auto;
 19             width: 80%;
 20             margin-top: 50px;
 21           }
 22 
 23           .botText{
 24             font-family: monospace;
 25             font-size: 16px;
 26             text-align: left;
 27             line-height: 25px;
 28             color: green;
 29           }
 30 
 31           #userInput{
 32             margin-left: auto;
 33             margin-right: auto;
 34             width: 80%;
 35             text-align: center;
 36             margin-top: 50px;
 37           }
 38 
 39           #textInput{
 40             border: 3px solid white;
 41             border-bottom: 3px dotted #657889;
 42             font-family: monospace;
 43             font-size: 16px;
 44             width: 60%;
 45             padding: 20px;
 46             color: blue;
 47           }
 48 
 49           #buttonInput{
 50             padding: 5px;
 51             font-family: monospace;
 52             font-size: 16px;
 53             padding: 10px;
 54             border: 3px solid #58ff7c;
 55             background-color: green;
 56             color: white;
 57             border-radius: 10px;
 58           }
 59 
 60           h1{
 61             text-align: center;
 62             color: green;
 63           }
 64 
 65           .userText{
 66             text-align: right;
 67           }
 68 
 69 
 70     </style>
 71 
 72 </head>
 73 <body>
 74 
 75     <h1>Chatbot App</h1>
 76     <div>
 77         <div id="chatbox">
 78             <p class="botText"><span>Hi there!</span></p>
 79         </div>
 80         <div id="userInput">
 81             <input id="textInput" type="text" name="userMessage" placeholder="Type your message"/>
 82             <input id="buttonInput" type="submit" value="Send"/>
 83         </div>
 84 
 85     </div>
 86 
 87     <script>
 88 
 89       function getUserResponse(){
 90 
 91         var userText = $('#textInput').val();
 92         var userHTML = "<p class = 'userText'> User: <span>"+userText+"</span></p>";
 93         $('#textInput').val("");
 94         $('#chatbox').append(userHTML);
 95         document.getElementById("userInput").scrollIntoView({block:'start',behavior:"smooth"});
 96         $.get("/get",{userMessage: userText}).done(function(data){
 97           var botHTML = "<p class='botText'> Chatbot: <span>"+data+"</span></p>"
 98           $('#chatbox').append(botHTML);
 99 
100         });
101 
102 
103       }
104 
105       $("#textInput").keypress(function(e){
106         if(e.which == 13){
107           getUserResponse();
108         }
109       });
110 
111       $('#buttonInput').click(function(){
112         getUserResponse();
113       })
114 
115 
116 
117 
118     </script>
119 
120 
121 </body>
122 </html>

效果展示

 

标签:__,userText,get,Python,机器人,color,聊天,font,margin
From: https://www.cnblogs.com/Lyh3012648079/p/18223407

相关文章

  • 【Python快速上手(三十四)】- Python math 模块
    目录Python快速上手(三十四)-Pythonmath模块Pythonmath模块详解1.导入math模块2.基本数学运算3.三角函数4.双曲函数5.特殊函数6.浮点运算辅助函数7.常量8.实际应用案例9.小结Python快速上手(三十四)-Pythonmath模块Pythonmath模块详解math模块......
  • 数据分享|python分类预测职员离职:逻辑回归、梯度提升、随机森林、XGB、CatBoost、LGB
    全文链接:https://tecdat.cn/?p=34434原文出处:拓端数据部落公众号分析师:ShilinChen离职率是企业保留人才能力的体现。分析预测职员是否有离职趋向有利于企业的人才管理,提升组织职员的心理健康,从而更有利于企业未来的发展。解决方案任务/目标采用分类这一方法构建6种模型对职......
  • Python实训的心路历程——第4天
    时间过得好快,转眼就第四天了,今天还是接着做界面,并往里面添加内容。========================================一天又过去了,但是每天都感觉自己成长了好多呢,哈哈来汇总一下今天的工作和收获今天把界面和一些基础的功能写完了,程序能正常跑了,可以处理实时摄像头、视频、图片三种......
  • Python信贷风控模型:梯度提升Adaboost,XGBoost,SGD, GBOOST, SVC,随机森林, KNN预测金
    原文链接:http://tecdat.cn/?p=26184 原文出处:拓端数据部落公众号最近我们被客户要求撰写关于信贷风控模型的研究报告,包括一些图形和统计输出。在此数据集中,我们必须预测信贷的违约支付,并找出哪些变量是违约支付的最强预测因子?以及不同人口统计学变量的类别,拖欠还款的概率如何......
  • Python selenium webdriver
    元素定位第一步,导入selenium模块的webdrivier包importtimefromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy窗口最大化driver=maximize_window()第二步,调用webdriver包的Chrome类,返回chrome浏览器对象driver=webdriver.Chrome()第三步,如使用......
  • python连接数据库
    一#删除数据库importpymysqlmydb=pymysql.connect(host="localhost",user="root",password="000000",database="my_db")mycursor=mydb.cursor()sql="DROPDATABASEIFEXISTSmy_db"mycursor.execute(sql)m......
  • 「Python魔法药剂:列表推导式的秘密配方大公开!」(一)
    嗨,我是阿佑,最近给大家打开了python操作数据库的大门,今天阿佑将为你揭开列表推导式这个秘密配方的神秘面纱。通过一系列生动有趣的案例和深入浅出的解释,阿佑将带你领略列表推导式的无穷魅力。准备好了吗?让我们一起调配出最强大的编程药剂,让你的代码魔法无人能敌!文章目录......
  • 「Python魔法药剂:列表推导式的秘密配方大公开!」(二)
    嗨,我是阿佑,上一篇给大家讲了《Python魔法药剂——列表推导式(一)》,今天给大家解锁剩下的全部内容!在Python编程的世界中,列表推导式就是那个能让你脱颖而出的秘籍。但如何掌握它,如何运用它解决实际问题?今天阿佑将为你揭晓答案。阿佑将带你一步步深入了解列表推导式的高级......
  • 「异步魔法:Python数据库交互的革命」(二)
    哈喽,我是阿佑,上篇文章带领了大家跨入的异步魔法的大门——Python数据库交互,一场魔法与技术的奇幻之旅!从基础概念到DB-API,再到ORM的高级魔法,我们一步步揭开了数据库操作的神秘面纱。SQLAlchemy和DjangoORM让我们的数据库操作变得强大而灵活。现在,让我们迈向异步编程的新世......
  • 「异步魔法:Python数据库交互的革命」(一)
    Hi,我是阿佑,今天将和大家一块打开异步魔法的大门,进入Python异步编程的神秘领域,学习如何同时施展多个咒语而不需等待。了解asyncio的魔力,掌握AsyncSQLAlchemy和Tortoise-ORM的秘密,让你的数据库操作快如闪电!文章目录Python进阶之数据库交互详解1.引言数据库在现代应用......