You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
9.5 KiB

import pymysql
import time
import wave
import requests
from pyaudio import PyAudio, paInt16
from aip import AipSpeech
import pyttsx3
from tkinter import *
from tkinter import messagebox
import speech_recognition as sr
from tkinter import Text, Entry, Button
framer = 16000
num_samples = 2000
channels = 1
sampwidth = 2
APP_ID = '78542969'
API_KEY = 'B37LWVxLfoHixHx52OrNCPij'
SECRET_KEY = 'bkNnbC37K3Ac6gI8kQ8phG1h5RIivMwj'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
audio_file = '../pythonProject6/myvoices.wav'
# Class for handling audio recording and saving
class Speak():
def save_wave_file(self, filepath, data):
wf = wave.open(filepath, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(sampwidth)
wf.setframerate(framer)
wf.writeframes(b''.join(data))
wf.close()
def my_record(self):
pa = PyAudio()
stream = pa.open(format=paInt16, channels=channels, rate=framer, input=True, frames_per_buffer=num_samples)
my_buf = []
t_end = time.time() + 5
print('识别中...')
while time.time() < t_end:
string_audio_data = stream.read(num_samples)
my_buf.append(string_audio_data)
print('识别完成')
self.save_wave_file(audio_file, my_buf)
stream.close()
class RobotSay():
def __init__(self):
self.engine = pyttsx3.init()
def say(self, msg):
self.engine.say(msg)
self.engine.runAndWait()
class ReadWav():
def __init__(self):
self.APP_ID = '78542969'
self.API_KEY = 'B37LWVxLfoHixHx52OrNCPij'
self.SECRET_KEY = 'bkNnbC37K3Ac6gI8kQ8phG1h5RIivMwj'
self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY)
def get_file_content(self, filePath):
with open(filePath, 'rb') as fp:
return fp.read()
def predict(self):
return client.asr(self.get_file_content('../pythonProject6/myvoices.wav'), 'wav', 16000, {
'dev_pid': 1537,
})
# 定义登录窗口
class LoginWindow:
def __init__(self, master):
self.master = master
self.master.title("登录")
self.master.geometry("300x200")
# Center align the login window
self.screen_width = self.master.winfo_screenwidth()
self.screen_height = self.master.winfo_screenheight()
self.x_pos = (self.screen_width - 300) // 2
self.y_pos = (self.screen_height - 200) // 2
self.master.geometry(f"300x200+{self.x_pos}+{self.y_pos}")
self.label_username = Label(master, text="账号")
self.label_username.pack()
self.entry_username = Entry(master)
self.entry_username.pack()
self.label_password = Label(master, text="密码")
self.label_password.pack()
self.entry_password = Entry(master, show="*")
self.entry_password.pack()
self.button_login = Button(master, text="登录", command=self.login)
self.button_login.pack()
def login(self):
username = self.entry_username.get()
password = self.entry_password.get()
if username == "123" and password == "123":
messagebox.showinfo("登陆成功", "恭喜你登陆成功")
self.open_chat_window()
else:
messagebox.showerror("登录失败", "无效的用户名或密码")
def open_chat_window(self):
self.master.withdraw()
chat_root = Toplevel(self.master)
chat_window = ChatWindow(chat_root)
chat_root.protocol("WM_DELETE_WINDOW", self.master.quit)
class ChatWindow:
def __init__(self, master):
self.master = master
self.master.title("开始聊天")
self.master.geometry("700x500")
self.screen_width = self.master.winfo_screenwidth()
self.screen_height = self.master.winfo_screenheight()
self.x_pos = (self.screen_width - 300) // 2
self.y_pos = (self.screen_height - 200) // 2
self.master.geometry(f"700x500+{self.x_pos}+{self.y_pos}")
self.chat_text = Text(self.master)
self.chat_text.pack()
self.input_field = Entry(self.master)
self.input_field.pack()
self.send_button = Button(self.master, text="发送", command=self.send_message)
self.send_button.pack()
self.view_button = Button(self.master, text="查看聊天记录", command=self.view_chat_history)
self.view_button.pack()
self.delete_button = Button(self.master, text="删除聊天记录", command=self.delete_chat_history)
self.delete_button.pack()
self.recognizer = sr.Recognizer()
self.engine = pyttsx3.init()
self.connection = pymysql.connect(host="localhost",
user="root",
password="myroot",
database="ai_chatter",
port=3306)
self.cursor = self.connection.cursor()
self.chat_history = []
self.voice_button = Button(self.master, text="语音对话", command=self.start_voice_conversation)
self.voice_button.pack()
self.APP_ID = APP_ID
self.API_KEY = API_KEY
self.SECRET_KEY = SECRET_KEY
self.client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
'''
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
'''
def send_message(self):
user_input = self.input_field.get()
response = self.get_qingyunke_response(user_input)
self.update_chat("你: " + user_input + "\n")
self.update_chat("菲菲: " + response + "\n")
self.speak(response)
self.save_to_database(user_input, response)
def start_voice_conversation(self):
# Implement voice conversation functionality
self.speak("开始语音对话,请说话...")
speak_instance = Speak()
speak_instance.my_record()
text = self.transcribe_audio('../pythonProject6/myvoices.wav')
self.update_chat("你说: " + text + "\n")
response = self.get_qingyunke_response(text)
self.update_chat("菲菲: " + response + "\n")
self.speak(response)
self.save_to_database(text, response)
def get_qingyunke_response(self, message):
api_url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + message
response = requests.get(api_url)
if response.status_code == 200:
data = response.json()
if data['result'] == 0:
return data['content']
else:
return "对不起,我不能理解."
else:
return "我目前无法处理您的请求."
def update_chat(self, text):
self.chat_text.insert(END, text)
def speak(self, text):
self.engine.say(text)
self.engine.runAndWait()
def transcribe_audio(self, audio_file):
with open(audio_file, 'rb') as f:
audio_data = f.read()
result = self.client.asr(audio_data, 'wav', 16000, {
'dev_pid': 1537, # Use the model for Mandarin
})
if 'result' in result:
return result['result'][0]
else:
return "Baidu speech recognition failed"
#使用mysql数据库来保存
def save_to_database(self, user_message, bot_response):
sql = "INSERT INTO chat_history (user_message, bot_response) VALUES (%s, %s)"
text = (user_message, bot_response)
self.connection.cursor().execute(sql, text)
self.connection.commit()
print("聊天记录保存与数据库中.")
def view_chat_history(self):
self.chat_text.delete('1.0', END)
sql = "SELECT * FROM ai_chatter.chat_history"
self.cursor.execute(sql)
records = self.cursor.fetchall()
for record in records:
self.update_chat("你: " + record[1] + "\n")
self.update_chat("菲菲: " + record[2] + "\n")
if self.chat_history:
self.update_chat("\n--- 聊天记录 ---\n")
for message in self.chat_history:
self.update_chat(message + "\n")
else:
self.update_chat("没有找到聊天记录\n")
def delete_chat_history(self):
sql = "DELETE FROM chat_history"
self.cursor.execute(sql)
self.connection.commit()
self.chat_text.delete('1.0', END)
self.update_chat("聊天记录已删除。\n")
if self.chat_history:
self.chat_history.clear()
self.update_chat("聊天记录已删除\n")
else:
self.update_chat("没有找到聊天记录\n")
def close_connection(self):
self.cursor.close()
self.connection.close()
#自然语言处理
#知识图谱模块
def fetch_knowledge_graph_info(self, query):
# Assuming you are using Google Knowledge Graph API
api_key = 'YOUR_API_KEY'
base_url = 'https://kgsearch.googleapis.com/v1/entities:search'
params = {
'query': query,
'key': api_key,
'limit': 1 # You can adjust the limit based on response requirements
}
response = requests.get(base_url, params=params).json()
if 'itemListElement' in response:
item = response['itemListElement'][0]
if 'result' in item:
return item['result']['name'], item['result']['detailedDescription']['articleBody']
def main():
root = Tk()
login_window = LoginWindow(root)
root.mainloop()
if __name__ == "__main__":
main()