-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIMars.py
391 lines (324 loc) · 13.9 KB
/
AIMars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import requests
import json
import uuid
import threading
import pyaudio
import time
import nls
import base64
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from PIL import Image, ImageTk
import io
from urllib.request import urlopen
from coze_env_setting import MY_BOT_ID, MY_COZE_TOKEN
from huoshan_env_setting import MY_HUOSHAN_APPID, MY_HUOSHAN_TOKEN
from text2speech_tool import TextToSpeech
from ali_env_setting import MY_ALI_APPKEY, MY_ALI_TOKEN
YOUR_BOT_ID = MY_BOT_ID
YOUR_COZE_TOKEN = MY_COZE_TOKEN
# 文本转语音
huoshan_appid = MY_HUOSHAN_APPID
huoshan_token = MY_HUOSHAN_TOKEN
huoshan_uid = str(uuid.uuid4())
ALI_TOKEN = MY_ALI_TOKEN
ALI_APPKEY = MY_ALI_APPKEY
URL = "wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1"
TOKEN = ALI_TOKEN
APPKEY = ALI_APPKEY
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 1024
class MyChatBot:
def __init__(self, display_callback):
self.messages = []
self.input_value = ''
self.conversation_id = None
self.bot_id = YOUR_BOT_ID
self.user_id = self.generate_uuid()
self.current_message = ''
self.tts = TextToSpeech(huoshan_appid, huoshan_token, huoshan_uid)
self.is_processing = False
self.display_callback = display_callback
self.user_message = ''
# 初始化锁
self.lock = threading.Lock()
# 最后生成的图片URL
self.pic_url = ''
def generate_uuid(self):
return str(uuid.uuid4())
def send_message(self):
if self.is_processing:
self.display_callback("AI正在处理,跳过发送")
return
self.is_processing = True
message = self.input_value
if not message:
self.is_processing = False
return
if not self.conversation_id:
self.create_empty_conversation()
if not self.conversation_id:
self.is_processing = False
self.display_callback("无法创建对话!!")
return
self.messages.append({'role': 'user', 'content': message})
self.input_value = ''
#提供一个上下文
msg_to_coze = '用户当前输入: ' + message + '\n' + '用户之前让AI生成的图片:' + self.pic_url
self.display_user_message(message)
access_token = YOUR_COZE_TOKEN
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
data = {
'conversation_id': self.conversation_id,
'bot_id': self.bot_id,
'user_id': self.user_id,
'stream': True,
'auto_save_history': True,
'additional_messages': [
{
'role': 'user',
'content': msg_to_coze,
'content_type': 'text'
}
]
}
response = requests.post('https://api.coze.cn/v3/chat', headers=headers, json=data, stream=True)
try:
response.raise_for_status()
for line in response.iter_lines():
if line:
decoded_line = line.decode('utf-8')
if decoded_line.startswith('event:'):
event_type = decoded_line.split(':', 1)[1].strip()
elif decoded_line.startswith('data:'):
event_data = decoded_line.split(':', 1)[1].strip()
self.handle_event(event_type, event_data)
except requests.exceptions.HTTPError as http_err:
self.display_callback(f'HTTP error occurred: {http_err}')
except json.JSONDecodeError:
self.display_callback('JSON decode error: 响应不是有效的 JSON 格式')
self.display_callback('响应内容:', response.text)
except Exception as err:
self.display_callback(f'Other error occurred: {err}')
finally:
self.is_processing = False
def display_user_message(self, message):
self.display_callback(f'用户: {message}', user=True)
def handle_event(self, event_type, event_data):
print(f'----->Event type: {event_type}')
print(f'----->Event data: {event_data}')
if event_type == 'conversation.chat.created':
data = json.loads(event_data)
#self.display_callback(f'对话已创建: {data}')
elif event_type == 'conversation.chat.in_progress':
data = json.loads(event_data)
#self.display_callback(f'对话进行中: {data}')
elif event_type == 'conversation.message.delta':
data = json.loads(event_data)
if data.get('role') == 'assistant':
new_content = data.get('content', '')
# 不处理流式内容
elif event_type == 'conversation.message.completed':
try:
data = json.loads(event_data)
if data.get('type') == 'tool_response':
tool_res_data = data.get('content')
try:
tool_res_data = json.loads(tool_res_data)
self.pic_url = tool_res_data['output']
self.display_callback(f'工具抽取: {self.pic_url}')
except Exception as e:
pass
# self.display_callback(f'工具响应: {data.get("content")}')
elif data.get('type') == 'function_call':
raw_data = data.get('content')
tool_msg = json.loads(raw_data)
tool_use = f'工具调用: {tool_msg["plugin_name"]}'
self.display_callback(tool_use)
self.tts.synthesize_and_play(tool_use)
elif data.get('type') == 'answer':
ai_message = data.get('content')
self.display_callback(f'AI 消息: {data.get("content")}')
if not self.current_message.startswith("http"):
print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 应该播放: {}'.format(ai_message))
self.tts.synthesize_and_play(ai_message)
except Exception as e:
#self.display_callback(f'JSON decode error: {e}')
pass
elif event_type == 'conversation.chat.completed':
if self.current_message:
self.messages.append({'role': 'assistant', 'content': self.current_message})
self.current_message = ''
data = json.loads(event_data)
#self.display_callback(f'对话已完成: {data}')
elif event_type == 'conversation.chat.failed':
data = json.loads(event_data)
self.display_callback(f'对话失败: {data}')
else:
#self.display_callback(f'未知事件类型: {event_type}')
pass
def create_empty_conversation(self):
access_token = YOUR_COZE_TOKEN
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
data = {
'bot_id': self.bot_id,
'user_id': self.user_id
}
response = requests.post('https://api.coze.cn/v1/conversation/create', headers=headers, json=data)
try:
response.raise_for_status()
res_data = response.json()
if res_data.get('code') == 0:
self.conversation_id = res_data['data']['id']
self.messages.append({'role': 'system', 'content': f'空会话已创建,ID: {self.conversation_id}'})
else:
self.display_callback(f'创建空会话失败: {res_data.get("msg")}')
except requests.exceptions.HTTPError as http_err:
self.display_callback(f'HTTP error occurred: {http_err}')
except json.JSONDecodeError:
self.display_callback('JSON decode error: 响应不是有效的 JSON 格式')
self.display_callback('响应内容:', response.text)
except Exception as err:
self.display_callback(f'Other error occurred: {err}')
class RealTimeSt:
def __init__(self, display_callback):
self.audio = pyaudio.PyAudio()
self.stream = None
self.sr = None
self.is_recording = False
self.chat_bot = MyChatBot(display_callback)
def start_recording(self):
self.is_recording = True
self.stream = self.audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
self.sr = nls.NlsSpeechTranscriber(
url=URL,
token=TOKEN,
appkey=APPKEY,
on_sentence_begin=self.on_sentence_begin,
on_sentence_end=self.on_sentence_end,
on_start=self.on_start,
on_result_changed=self.on_result_changed,
on_completed=self.on_completed,
on_error=self.on_error,
on_close=self.on_close
)
self.sr.start(aformat="pcm",
enable_intermediate_result=True,
enable_punctuation_prediction=True,
enable_inverse_text_normalization=True)
self.recording_thread = threading.Thread(target=self.process_stream)
self.recording_thread.start()
def process_stream(self):
while self.is_recording:
data = self.stream.read(CHUNK)
self.sr.send_audio(data)
time.sleep(0.01)
def stop_recording(self):
self.is_recording = False
self.recording_thread.join()
self.stream.stop_stream()
self.stream.close()
self.audio.terminate()
self.sr.stop()
def on_sentence_begin(self, message, *args):
pass
def on_sentence_end(self, message, *args):
data = json.loads(message)
result_content = data['payload']['result']
threading.Thread(target=self.process_ai_response, args=(result_content,)).start()
def process_ai_response(self, result_content):
self.chat_bot.input_value = result_content
self.chat_bot.send_message()
def on_start(self, message, *args):
pass
def on_result_changed(self, message, *args):
pass
def on_completed(self, message, *args):
pass
def on_error(self, message, *args):
pass
def on_close(self, *args):
pass
class App:
def __init__(self, root):
self.root = root
self.root.title("AI Chat Application")
self.root.geometry("800x600") # 固定窗口大小
self.left_frame = ttk.Frame(self.root)
self.left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.right_frame = ttk.Frame(self.root)
self.right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
self.text_area = scrolledtext.ScrolledText(self.left_frame, wrap=tk.WORD, font=("Helvetica", 12))
self.text_area.pack(fill=tk.BOTH, expand=True)
self.display_area = tk.Label(self.right_frame)
self.display_area.pack(fill=tk.BOTH, expand=True)
self.realtime_st = RealTimeSt(self.display_message)
self.recording = False
self.toggle_button = ttk.Button(self.root, text="Start Recording", command=self.toggle_recording)
self.toggle_button.pack(side=tk.BOTTOM)
# 初始化锁
self.lock = threading.Lock()
# 用于存储消息的列表
self.messages = []
self.setup_styles()
def toggle_recording(self):
if self.recording:
self.realtime_st.stop_recording()
self.toggle_button.config(text="Start Recording")
else:
self.realtime_st.start_recording()
self.toggle_button.config(text="Stop Recording")
self.recording = not self.recording
def display_message(self, message, user=False, stream=False, update=False):
with self.lock: # 使用锁来同步访问
if user:
# self.messages.append(f'用户: {message}')
self.text_area.insert(tk.END, f'用户: {message}\n', 'user')
elif stream:
if update and self.messages and self.messages[-1].startswith('助手:'):
# 更新最后一个助手消息
self.messages[-1] = f'助手: {message}'
# 获取最后一行的起始索引
last_line_index = self.text_area.index('end-2l')
# 删除最后一行
self.text_area.delete(last_line_index, 'end-1l')
self.text_area.insert(tk.END, f'助手: {message}\n', 'assistant')
else:
self.messages.append(f'助手: {message}')
self.text_area.insert(tk.END, f'助手: {message}\n', 'assistant')
else:
self.messages.append(message)
self.text_area.insert(tk.END, f'{message}\n')
self.text_area.yview(tk.END)
if message.startswith("工具抽取:"):
content = message.split("工具抽取:")[1].strip()
if content.startswith("https"):
self.display_image(content)
def display_image(self, url):
try:
image_bytes = urlopen(url).read()
data_stream = io.BytesIO(image_bytes)
pil_image = Image.open(data_stream)
tk_image = ImageTk.PhotoImage(pil_image)
self.display_area.config(image=tk_image)
self.display_area.image = tk_image
except Exception as e:
self.display_message(f"无法加载图片: {e}")
def setup_styles(self):
self.text_area.tag_configure('user', foreground='black', background='lightblue')
self.text_area.tag_configure('assistant', foreground='black', background='white')
if __name__ == '__main__':
root = tk.Tk()
app = App(root)
root.mainloop()