基于Python制作AI聊天软件的示例代码
作者:晋升阁 发布时间:2023-09-27 14:58:25
标签:Python,AI,聊天
效果图
先看一下效果图
就当是女友无聊的时候自己抽不出时间的小分身吧!
需要用到的库
tkinter、time、urllib、requests
tkinter负责窗体、time显示时间、urllib和requests负责请求
窗体设计
from tkinter import *
win1 = Tk()
win1.geometry('400x644+100+100')
win1.title('xxx男神的AI分身')
Label11 = Label(win1, text='男神白', font=('黑体', 12), anchor='center').place(y=13, x=15, width=380, height=20)
Entry11 = Entry(win1, font=('等线', 11), width = 70)
Entry11.place(y=600, x=15, width=310, height=26)
Button11 = Button(win1, text='发送', font=('等线', 11), command = mecha).place(y=598, x=328, width=65, height=30)
console = Text(win1, font=('等线', 11))
console.place(y=35, x=15, width=368, height=550)
console.insert(1.0,' 欢迎来到你与男神的小天地!\n你可以把你想说的内容输入到下面的输入框哦\n')
console.mark_set('markOne', 1.0)
console.mark_set('markTwo', 3.0)
console.tag_add('tag1', 'markOne', 'markTwo')
console.tag_config('tag1', foreground='red')
win1.bind("<Return>", test_fun)
win1.mainloop()
函数
在txet部件上显示发送时间及颜色处理,使用requests和urllib库来调用接口处理回复你女朋友的信息。
def liaotian():
global b
import time
b = 3
def mecha():
global b
b+=2
console.insert('end',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
console.insert('end',str(Entry11.get())+'\n')
console.mark_set(str(b-1), str(b-1)+'.0')
console.mark_set(str(b), str(b)+'.0')
console.tag_add(str(b), str(b-1), str(b))
console.tag_config(str(b), foreground='blue')
console.see(END)
console.update()
console.insert('end',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
console.insert('end',aicha()+'\n')
console.mark_set(str(b-1), str(b-1)+'.0')
console.mark_set(str(b), str(b)+'.0')
console.tag_add(str(b), str(b-1), str(b))
console.tag_config(str(b), foreground='red')
console.see(END)
Entry11.delete(0,END)
console.update()
def test_fun(self):
mecha()
def aicha():
global b
b+=2
msg = str(Entry11.get())
else:
import urllib
import requests
def qingyunke(msg):
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
html = requests.get(url)
return html.json()["content"]
print("原话>>", msg)
res = qingyunke(msg)
res = res.replace('菲菲', '你男神')
res = res.replace('我', '你男神')
print("智能回复>>", res)
return res
为粉丝们额外添加的功能
根据女友输入的内容自定义回复
我自己添加的内容太肉麻的,不太适合分享给你们哈。于是我就给你们做多了一个自定义回复的功能。嘿嘿!那就是独一无二的代码了
坚持着你们直接能使用代码不做任何修改的原则,我就不让你们在代码里面添加了,当你第一次运行此代码的时候会自动创建一个txt文件(甚至还不用让你创建文件),你就可以在txt文件中自定义回复内容了。
使用异常处理模块try来尝试open读取名为“自定义回复.txt”的文件,若不存在except就创建,若已存在直接读取即可。操作读取的字符串逐个添加到zidingyi字典中,判断输入的语句是否在zidingyi.keys()中即可做出相应回复。
添加后的效果图:
zidingyi = {}
try:
with open("自定义回复.txt", "r", encoding='utf8') as f:
asd = f.readlines()
print(asd)
for line in asd:
line = line.strip('\n')
wen, da = line.split(':', 1)
zidingyi[wen] = da
except:
with open("自定义回复.txt", "w+", encoding='utf8') as f:
f.write('提示——>采用“输入:回复”格式 如——>你吃饭了吗?:我吃饭啦 回车以继续下一自定义回复(注意使用英文的冒号)')
with open("自定义回复.txt", "r", encoding='utf8') as f:
asd = f.readlines()
print(asd)
for line in asd[1:]:
line = line.strip('\n')
wen, da = line.split(':', 1)
zidingyi[wen] = da
print(line)
print(zidingyi)
完整代码
from tkinter import *
def liaotian():
global b
import time
b = 3
def mecha():
global b
b += 2
console.insert('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
console.insert('end', str(Entry11.get()) + '\n')
console.mark_set(str(b - 1), str(b - 1) + '.0')
console.mark_set(str(b), str(b) + '.0')
console.tag_add(str(b), str(b - 1), str(b))
console.tag_config(str(b), foreground='blue')
console.see(END)
console.update()
console.insert('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + '\n')
console.insert('end', aicha() + '\n')
console.mark_set(str(b - 1), str(b - 1) + '.0')
console.mark_set(str(b), str(b) + '.0')
console.tag_add(str(b), str(b - 1), str(b))
console.tag_config(str(b), foreground='red')
console.see(END)
Entry11.delete(0, END)
console.update()
def test_fun(self):
mecha()
def aicha():
global b
b += 2
msg = str(Entry11.get())
if msg in zidingyi.keys():
res = zidingyi[msg]
return res
else:
import urllib
import requests
def qingyunke(msg):
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
html = requests.get(url)
return html.json()["content"]
print("原话>>", msg)
res = qingyunke(msg)
res = res.replace('菲菲', '你男神')
res = res.replace('我', '你男神')
print("智能回复>>", res)
return res
zidingyi = {}
try:
with open("自定义回复.txt", "r", encoding='utf8') as f:
asd = f.readlines()
print(asd)
for line in asd:
line = line.strip('\n')
wen, da = line.split(':', 1)
zidingyi[wen] = da
except:
with open("自定义回复.txt", "w+", encoding='utf8') as f:
f.write('提示——>采用“输入:回复”格式 如——>你吃饭了吗?:我吃饭啦 回车以继续下一自定义回复(注意使用英文的冒号)')
with open("自定义回复.txt", "r", encoding='utf8') as f:
asd = f.readlines()
print(asd)
for line in asd[1:]:
line = line.strip('\n')
wen, da = line.split(':', 1)
zidingyi[wen] = da
print(line)
print(zidingyi)
win1 = Tk()
win1.geometry('400x644+100+100')
win1.title('男神的AI分身')
Label11 = Label(win1, text='你男神', font=('黑体', 12), anchor='center').place(y=13, x=15, width=380, height=20)
Entry11 = Entry(win1, font=('等线', 11), width=70)
Entry11.place(y=600, x=15, width=310, height=26)
Button11 = Button(win1, text='发送', font=('等线', 11), command=mecha).place(y=598, x=328, width=65, height=30)
console = Text(win1, font=('等线', 11))
console.place(y=35, x=15, width=368, height=550)
console.insert(1.0, ' 欢迎来到你与男神的小天地!\n 你可以把你想说的内容输入到下面的输入框哦\n')
console.mark_set('markOne', 1.0)
console.mark_set('markTwo', 3.0)
console.tag_add('tag1', 'markOne', 'markTwo')
console.tag_config('tag1', foreground='red')
win1.bind("<Return>", test_fun)
win1.mainloop()
liaotian()
怎么样,是不是特别简单~快复制去送给你心中的那个女神吧~
来源:https://blog.csdn.net/m0_62814033/article/details/125563506


猜你喜欢
- 操作系统:WINDOWS-XP 系统数据库版本:mysql 5.x提示:access denied for user 'root
- 如下所示:b.reset_index(drop=True)reset_index代表重新设置索引,drop=True为删除原索引。来源:ht
- 背景最近在搞爬虫,很多小组件里面都使用了 Python 的 requests 库,很好用,很强大。但最近发现很多任务总是莫名其妙的卡住,不报
- 问题查询了很多网上的文章,连接远程服务器调试基本上都是本地复制一个代码文件夹调试好后再部署到服务器上,这就很麻烦,(作为一个懒人)我想直接打
- 一、 背景由于公司业务需要动态配置一些存储过程来生成数据,之前尝试过使用jpa来完成,或多或少都存在一些问题,最后使用了spring的Jdb
- 其实网上已经有很多ASP生成htm的文章了,有一种方法是ASP+XML的生成方法,虽然有一种好处就是不用程序写模版就可以直接引用原来的要生成
- 散点图什么是散点图?散点图是指在数理统计回归分析中,数据点在直角坐标系平面上的分布图, 散点图表示因变量随自变量而变化的大致趋势,
- 从Request对象中获取数据我们在第三章讲述View的函数时已经介绍过HttpRequest对象了,但当时并没有讲太多。 让我们回忆下:每
- 我们在网页开发过程中经常会有打印页面的需求,通过JS来实现的方法有很多,这里我做了一个整理,供大家参考。方式一:window.print()
- 1 引言各位朋友大家好,欢迎来到月来客栈。今天要和大家介绍的内容是如何在Pytorch框架中对模型进行保存和载入、以及模型的迁移和再训练。一
- 本文主要展示了使用matplotlib设计logo的示例及完整代码,首先看下其演示结果:Python代码如下:import numpy as
- function gaga(obj){ // 值允许输入一个小数点和数字 obj.value = obj.value.replace(/[^
- 1、get方式:如何为爬虫添加ip代理,设置Request header(请求头)import urllib import urllib.r
- 假设有表tb_sku,其表结构如下:表中大约有200w条记录,执行如下的sql 语句大约 4.36s 返回数据select count(*)
- 前言pymssql模块是用于sql server数据库(一种数据库通用接口标准)的连接。另外pyodbc不仅限于SQL server,还包括
- 痛点在传统的工作中,发送会议纪要是一个比较繁琐的任务,需要手动输入邮件内容、收件人、抄送人等信息,每次发送都需要重复操作,不仅费时费力,而且
- 一、Monkey测试简介Monkey测试是Android平台自动化测试的一种手段,通过Monkey程序模拟用户触摸屏幕、滑动Trackbal
- 周五下午,作为小白太痛苦了,这两天一直在做一件事,如下:使flask接口中的函数执行的同时,向指定的url传递数据(我甚至不知道怎么描述这个
- 本文实例讲述了C语言实现访问及查询MySQL数据库的方法。分享给大家供大家参考,具体如下:1、添加头文件路径(MySQL安装路径中的incl
- 如题,今天兜兜转转找了很多网站帖子,一个个环节击破,最后装好费了不少时间。希望这个帖子能帮助有需要的人,教你一篇帖子搞定python+num