qwerlol123? 6 months ago
parent e221cea7ba
commit 64588d63d4

@ -20,7 +20,7 @@ SECRET_KEY = 'bkNnbC37K3Ac6gI8kQ8phG1h5RIivMwj'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
audio_file = '../pythonProject6/myvoices.wav' audio_file = '../pythonProject6/myvoices.wav'
# Class for handling audio recording and saving # 用于处理音频录制和保存
class Speak(): class Speak():
def save_wave_file(self, filepath, data): def save_wave_file(self, filepath, data):
wf = wave.open(filepath, 'wb') wf = wave.open(filepath, 'wb')
@ -165,7 +165,6 @@ class ChatWindow:
self.save_to_database(user_input, response) self.save_to_database(user_input, response)
def start_voice_conversation(self): def start_voice_conversation(self):
# Implement voice conversation functionality
self.speak("开始语音对话,请说话...") self.speak("开始语音对话,请说话...")
speak_instance = Speak() speak_instance = Speak()
speak_instance.my_record() speak_instance.my_record()
@ -201,14 +200,14 @@ class ChatWindow:
if 'result' in result: if 'result' in result:
return result['result'][0] return result['result'][0]
else: else:
return "Baidu speech recognition failed" return "百度语音识别失败"
#使用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)"
text = (user_message, bot_response) text = (user_message, bot_response)
self.connection.cursor().execute(sql, text) self.connection.cursor().execute(sql, text)
self.connection.commit() self.connection.commit()
print("聊天记录保存与数据库中.") self.update_chat("聊天记录保存与数据库中\n")
def view_chat_history(self): def view_chat_history(self):
self.chat_text.delete('1.0', END) self.chat_text.delete('1.0', END)
sql = "SELECT * FROM ai_chatter.chat_history" sql = "SELECT * FROM ai_chatter.chat_history"
@ -222,7 +221,7 @@ class ChatWindow:
for message in self.chat_history: for message in self.chat_history:
self.update_chat(message + "\n") self.update_chat(message + "\n")
else: else:
self.update_chat("没有找到聊天记录\n") self.update_chat("\n")
def delete_chat_history(self): def delete_chat_history(self):
sql = "DELETE FROM chat_history" sql = "DELETE FROM chat_history"
@ -243,18 +242,16 @@ class ChatWindow:
#知识图谱模块 #知识图谱模块
def fetch_knowledge_graph_info(self, query): def fetch_knowledge_graph_info(self, query):
# Assuming you are using Google Knowledge Graph API # 使用Google知识图谱API
api_key = 'YOUR_API_KEY' api_key = ''
base_url = 'https://kgsearch.googleapis.com/v1/entities:search' base_url = 'https://kgsearch.googleapis.com/v1/entities:search'
params = { params = {
'query': query, 'query': query,
'key': api_key, 'key': api_key,
'limit': 1 # You can adjust the limit based on response requirements 'limit': 1
} }
response = requests.get(base_url, params=params).json() response = requests.get(base_url, params=params).json()
if 'itemListElement' in response: if 'itemListElement' in response:
item = response['itemListElement'][0] item = response['itemListElement'][0]
if 'result' in item: if 'result' in item:

Loading…
Cancel
Save