qwerlol123? 9 months ago
parent 5ebafa2b19
commit e221cea7ba

@ -8,21 +8,17 @@ import pyttsx3
from tkinter import * from tkinter import *
from tkinter import messagebox from tkinter import messagebox
import speech_recognition as sr import speech_recognition as sr
from tkinter import Text, Scrollbar, Entry, Button from tkinter import Text, Entry, Button
import nltk
from nltk.tokenize import word_tokenize framer = 16000
from nltk.corpus import stopwords num_samples = 2000
from nltk.stem import WordNetLemmatizer
# Constants for audio recording
framer = 16000 # Sample rate
num_samples = 2000 # Sample points
channels = 1 channels = 1
sampwidth = 2 sampwidth = 2
audio_file = 'myvoices.wav' # Specify the path to save the recorded audio file APP_ID = '78542969'
APP_ID = '73927317' API_KEY = 'B37LWVxLfoHixHx52OrNCPij'
API_KEY = '3jGcj5fLma64CtB3tTEuLcei' SECRET_KEY = 'bkNnbC37K3Ac6gI8kQ8phG1h5RIivMwj'
SECRET_KEY = 'qm8gPCF7DSKpqatx5ZQ8e4OvNLmgdYcG' # Add your own API credentials here client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
audio_file = '../pythonProject6/myvoices.wav'
# Class for handling audio recording and saving # Class for handling audio recording and saving
class Speak(): class Speak():
@ -39,16 +35,14 @@ class Speak():
stream = pa.open(format=paInt16, channels=channels, rate=framer, input=True, frames_per_buffer=num_samples) stream = pa.open(format=paInt16, channels=channels, rate=framer, input=True, frames_per_buffer=num_samples)
my_buf = [] my_buf = []
t_end = time.time() + 5 t_end = time.time() + 5
print('Speak now...') print('识别中...')
while time.time() < t_end: while time.time() < t_end:
string_audio_data = stream.read(num_samples) string_audio_data = stream.read(num_samples)
my_buf.append(string_audio_data) my_buf.append(string_audio_data)
print('Recording ended') print('识别完成')
self.save_wave_file(audio_file, my_buf) self.save_wave_file(audio_file, my_buf)
stream.close() stream.close()
client = AipSpeech('73927317', '3jGcj5fLma64CtB3tTEuLcei', 'qm8gPCF7DSKpqatx5ZQ8e4OvNLmgdYcG')
class RobotSay(): class RobotSay():
def __init__(self): def __init__(self):
self.engine = pyttsx3.init() self.engine = pyttsx3.init()
@ -58,12 +52,17 @@ class RobotSay():
self.engine.runAndWait() self.engine.runAndWait()
class ReadWav(): 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): def get_file_content(self, filePath):
with open(filePath, 'rb') as fp: with open(filePath, 'rb') as fp:
return fp.read() return fp.read()
def predict(self): 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, 'dev_pid': 1537,
}) })
@ -144,16 +143,38 @@ class ChatWindow:
port=3306) port=3306)
self.cursor = self.connection.cursor() self.cursor = self.connection.cursor()
self.chat_history = [] 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('punkt')
nltk.download('stopwords') nltk.download('stopwords')
nltk.download('wordnet') nltk.download('wordnet')
'''
def send_message(self): def send_message(self):
user_input = self.input_field.get() user_input = self.input_field.get()
self.update_chat("你: " + user_input + "\n")
response = self.get_qingyunke_response(user_input) response = self.get_qingyunke_response(user_input)
self.update_chat("你: " + user_input + "\n")
self.update_chat("菲菲: " + response + "\n") self.update_chat("菲菲: " + response + "\n")
self.speak(response) 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): def get_qingyunke_response(self, message):
api_url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + message api_url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + message
response = requests.get(api_url) response = requests.get(api_url)
@ -172,10 +193,15 @@ class ChatWindow:
self.engine.runAndWait() self.engine.runAndWait()
def transcribe_audio(self, audio_file): def transcribe_audio(self, audio_file):
with sr.AudioFile(audio_file) as source: with open(audio_file, 'rb') as f:
audio_data = self.recognizer.record(source) audio_data = f.read()
text = self.recognizer.recognize_sphinx(audio_data) result = self.client.asr(audio_data, 'wav', 16000, {
return text 'dev_pid': 1537, # Use the model for Mandarin
})
if 'result' in result:
return result['result'][0]
else:
return "Baidu speech recognition failed"
#使用mysql数据库来保存 #使用mysql数据库来保存
def save_to_database(self, user_message, bot_response): def save_to_database(self, user_message, bot_response):
sql = "INSERT INTO chat_history (user_message, bot_response) VALUES (%s, %s)" sql = "INSERT INTO chat_history (user_message, bot_response) VALUES (%s, %s)"
@ -214,20 +240,25 @@ class ChatWindow:
self.cursor.close() self.cursor.close()
self.connection.close() self.connection.close()
#自然语言处理 #自然语言处理
def process_user_input(self, user_input):
# 对用户输入进行分词 #知识图谱模块
tokens = word_tokenize(user_input) def fetch_knowledge_graph_info(self, query):
# 去除停用词 # Assuming you are using Google Knowledge Graph API
stop_words = set(stopwords.words('english')) api_key = 'YOUR_API_KEY'
filtered_tokens = [word for word in tokens if word.lower() not in stop_words] base_url = 'https://kgsearch.googleapis.com/v1/entities:search'
# 词形还原
lemmatizer = WordNetLemmatizer() params = {
lemmatized_tokens = [lemmatizer.lemmatize(word) for word in filtered_tokens] 'query': query,
# 将处理后的文本重新组合为句子 'key': api_key,
processed_input = ' '.join(lemmatized_tokens) 'limit': 1 # You can adjust the limit based on response requirements
# 返回处理后的文本 }
return processed_input
user_input = "Can you tell me about machine learning?" 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(): def main():
root = Tk() root = Tk()
login_window = LoginWindow(root) login_window = LoginWindow(root)

Binary file not shown.
Loading…
Cancel
Save