|
|
|
@ -8,21 +8,17 @@ import pyttsx3
|
|
|
|
|
from tkinter import *
|
|
|
|
|
from tkinter import messagebox
|
|
|
|
|
import speech_recognition as sr
|
|
|
|
|
from tkinter import Text, Scrollbar, Entry, Button
|
|
|
|
|
import nltk
|
|
|
|
|
from nltk.tokenize import word_tokenize
|
|
|
|
|
from nltk.corpus import stopwords
|
|
|
|
|
from nltk.stem import WordNetLemmatizer
|
|
|
|
|
|
|
|
|
|
# Constants for audio recording
|
|
|
|
|
framer = 16000 # Sample rate
|
|
|
|
|
num_samples = 2000 # Sample points
|
|
|
|
|
from tkinter import Text, Entry, Button
|
|
|
|
|
|
|
|
|
|
framer = 16000
|
|
|
|
|
num_samples = 2000
|
|
|
|
|
channels = 1
|
|
|
|
|
sampwidth = 2
|
|
|
|
|
audio_file = 'myvoices.wav' # Specify the path to save the recorded audio file
|
|
|
|
|
APP_ID = '73927317'
|
|
|
|
|
API_KEY = '3jGcj5fLma64CtB3tTEuLcei'
|
|
|
|
|
SECRET_KEY = 'qm8gPCF7DSKpqatx5ZQ8e4OvNLmgdYcG' # Add your own API credentials here
|
|
|
|
|
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():
|
|
|
|
@ -39,16 +35,14 @@ class Speak():
|
|
|
|
|
stream = pa.open(format=paInt16, channels=channels, rate=framer, input=True, frames_per_buffer=num_samples)
|
|
|
|
|
my_buf = []
|
|
|
|
|
t_end = time.time() + 5
|
|
|
|
|
print('Speak now...')
|
|
|
|
|
print('识别中...')
|
|
|
|
|
while time.time() < t_end:
|
|
|
|
|
string_audio_data = stream.read(num_samples)
|
|
|
|
|
my_buf.append(string_audio_data)
|
|
|
|
|
print('Recording ended')
|
|
|
|
|
print('识别完成')
|
|
|
|
|
self.save_wave_file(audio_file, my_buf)
|
|
|
|
|
stream.close()
|
|
|
|
|
|
|
|
|
|
client = AipSpeech('73927317', '3jGcj5fLma64CtB3tTEuLcei', 'qm8gPCF7DSKpqatx5ZQ8e4OvNLmgdYcG')
|
|
|
|
|
|
|
|
|
|
class RobotSay():
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.engine = pyttsx3.init()
|
|
|
|
@ -58,12 +52,17 @@ class RobotSay():
|
|
|
|
|
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('myvoices.wav'), 'wav', 16000, {
|
|
|
|
|
return client.asr(self.get_file_content('../pythonProject6/myvoices.wav'), 'wav', 16000, {
|
|
|
|
|
'dev_pid': 1537,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
@ -144,16 +143,38 @@ class ChatWindow:
|
|
|
|
|
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()
|
|
|
|
|
self.update_chat("你: " + user_input + "\n")
|
|
|
|
|
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)
|
|
|
|
@ -172,10 +193,15 @@ class ChatWindow:
|
|
|
|
|
self.engine.runAndWait()
|
|
|
|
|
|
|
|
|
|
def transcribe_audio(self, audio_file):
|
|
|
|
|
with sr.AudioFile(audio_file) as source:
|
|
|
|
|
audio_data = self.recognizer.record(source)
|
|
|
|
|
text = self.recognizer.recognize_sphinx(audio_data)
|
|
|
|
|
return text
|
|
|
|
|
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)"
|
|
|
|
@ -214,20 +240,25 @@ class ChatWindow:
|
|
|
|
|
self.cursor.close()
|
|
|
|
|
self.connection.close()
|
|
|
|
|
#自然语言处理
|
|
|
|
|
def process_user_input(self, user_input):
|
|
|
|
|
# 对用户输入进行分词
|
|
|
|
|
tokens = word_tokenize(user_input)
|
|
|
|
|
# 去除停用词
|
|
|
|
|
stop_words = set(stopwords.words('english'))
|
|
|
|
|
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
|
|
|
|
|
# 词形还原
|
|
|
|
|
lemmatizer = WordNetLemmatizer()
|
|
|
|
|
lemmatized_tokens = [lemmatizer.lemmatize(word) for word in filtered_tokens]
|
|
|
|
|
# 将处理后的文本重新组合为句子
|
|
|
|
|
processed_input = ' '.join(lemmatized_tokens)
|
|
|
|
|
# 返回处理后的文本
|
|
|
|
|
return processed_input
|
|
|
|
|
user_input = "Can you tell me about machine learning?"
|
|
|
|
|
|
|
|
|
|
#知识图谱模块
|
|
|
|
|
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)
|
|
|
|
|