parent
df68003ff7
commit
111dfaf20d
@ -0,0 +1,149 @@
|
||||
import tkinter as tk
|
||||
|
||||
import threading
|
||||
import speech_recognition as sr
|
||||
import requests
|
||||
import pyttsx3
|
||||
from AiChatterbot import talkWithRobot, robotSay
|
||||
|
||||
# Initialize speech synthesis object
|
||||
talker = pyttsx3.init()
|
||||
|
||||
# Initialize speech recognition object
|
||||
recognizer = sr.Recognizer()
|
||||
|
||||
userName = "admin"
|
||||
userPass = "123"
|
||||
# Create chat window
|
||||
def chat_window():
|
||||
chat_window = tk.Tk()
|
||||
chat_window.title("对话")
|
||||
chat_window.geometry("800x400")
|
||||
|
||||
def send_text_message():
|
||||
message = input_entry.get()
|
||||
chat_text.insert(tk.END, "你: " + message + "\n")
|
||||
response = get_response(message)
|
||||
chat_text.insert(tk.END, "机器人: " + response + "\n")
|
||||
input_entry.delete(0, tk.END)
|
||||
# 语音合成回复并播放
|
||||
talker.say(response)
|
||||
talker.runAndWait()
|
||||
|
||||
def start_voice_recognition(chat_text):
|
||||
with sr.Microphone() as source:
|
||||
print("请说话...")
|
||||
audio = recognizer.listen(source)
|
||||
|
||||
try:
|
||||
print("识别中...")
|
||||
text = recognizer.recognize_google(audio, language="zh-CN")
|
||||
print("你说: " + text)
|
||||
|
||||
# 调用自然语言处理功能获取回复
|
||||
response = get_response(text)
|
||||
|
||||
# 显示用户输入的消息和机器人的回复
|
||||
chat_text.insert(tk.END, "你: " + text + "\n")
|
||||
chat_text.insert(tk.END, "机器人: " + response + "\n")
|
||||
|
||||
# 语音合成回复并播放
|
||||
talker.say(response)
|
||||
talker.runAndWait()
|
||||
except sr.UnknownValueError:
|
||||
print("抱歉, 未能识别,请重试")
|
||||
|
||||
def send_voice_message():
|
||||
start_voice_recognition(chat_text)
|
||||
|
||||
chat_frame = tk.Frame(chat_window)
|
||||
chat_frame.pack(pady=10)
|
||||
|
||||
chat_text = tk.Text(chat_frame)
|
||||
chat_text.pack()
|
||||
|
||||
input_entry = tk.Entry(chat_frame)
|
||||
input_entry.pack(side=tk.LEFT, expand=True, fill=tk.BOTH)
|
||||
|
||||
send_text_button = tk.Button(chat_frame, text="发送", command=send_text_message)
|
||||
send_text_button.pack(side=tk.LEFT)
|
||||
|
||||
send_voice_button = tk.Button(chat_frame, text="语音输入", command=start_voice_recognition)
|
||||
send_voice_button.pack(side=tk.LEFT)
|
||||
|
||||
chat_window.mainloop()
|
||||
|
||||
# 启动聊天窗口
|
||||
# 创建登录窗口
|
||||
def login_window():
|
||||
login_window = tk.Tk()
|
||||
login_window.title("登录")
|
||||
|
||||
# 获取屏幕宽度和高度
|
||||
screen_width = login_window.winfo_screenwidth()
|
||||
screen_height = login_window.winfo_screenheight()
|
||||
|
||||
# 设置窗口居中
|
||||
window_width = 300
|
||||
window_height = 200
|
||||
x = (screen_width - window_width) // 2
|
||||
y = (screen_height - window_height) // 2
|
||||
login_window.geometry(f"{window_width}x{window_height}+{x}+{y}")
|
||||
|
||||
def login():
|
||||
inputName = name_entry.get()
|
||||
inputPass = pass_entry.get()
|
||||
|
||||
if inputName == userName and inputPass == userPass:
|
||||
talker.say("恭喜你登录成功!")
|
||||
talker.runAndWait()
|
||||
login_window.destroy()
|
||||
chat_window()
|
||||
else:
|
||||
talker.say("账号或密码错误,登录失败")
|
||||
talker.runAndWait()
|
||||
|
||||
name_label = tk.Label(login_window, text="用户名:")
|
||||
name_label.pack()
|
||||
name_entry = tk.Entry(login_window)
|
||||
name_entry.pack()
|
||||
|
||||
pass_label = tk.Label(login_window, text="密码:")
|
||||
pass_label.pack()
|
||||
pass_entry = tk.Entry(login_window, show="*")
|
||||
pass_entry.pack()
|
||||
|
||||
login_button = tk.Button(login_window, text="登录", command=login)
|
||||
login_button.pack()
|
||||
|
||||
login_window.mainloop()
|
||||
# 自然语言处理功能
|
||||
def get_response(message):
|
||||
if "你好" in message:
|
||||
return "你好,有什么可以帮助你的吗?"
|
||||
elif "再见" in message:
|
||||
return "再见,祝你一天愉快!"
|
||||
else:
|
||||
return "抱歉,我不明白你说的是什么。"
|
||||
|
||||
|
||||
def send_message():
|
||||
# 获取用户输入的消息
|
||||
chat_text = tk.Text(chat_window)
|
||||
chat_text.pack()
|
||||
|
||||
input_entry = tk.Entry(chat_window)
|
||||
input_entry.pack()
|
||||
message = input_entry.get()
|
||||
|
||||
# 显示用户输入的消息
|
||||
chat_text.insert(tk.END, "You: " + message + "\n")
|
||||
|
||||
# 获取模型生成的回复
|
||||
response = get_response(message)
|
||||
|
||||
# 显示模型生成的回复
|
||||
chat_text.insert(tk.END, "Bot: " + response + "\n")
|
||||
|
||||
# 启动登录窗口
|
||||
login_window()
|
Loading…
Reference in new issue