-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmytest6_2.py
More file actions
899 lines (746 loc) · 32.2 KB
/
mytest6_2.py
File metadata and controls
899 lines (746 loc) · 32.2 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
import os
import sys
import threading
import random
import time
import numpy as np
import torch
import pygame
import requests
from PyQt5.QtWidgets import (QApplication, QSystemTrayIcon, QMenu, QWidget, QTextEdit,
QPushButton, QLineEdit, QGraphicsDropShadowEffect,
QHBoxLayout, QLabel, QFileDialog, QStyle, QVBoxLayout,
QDialog, QProgressBar)
from PyQt5.QtGui import QIcon, QPixmap, QMovie
from PyQt5.QtCore import (Qt, QPropertyAnimation, QEasingCurve, QTimer, QPoint,
pyqtSignal, QThread, QMetaObject, Q_ARG)
from transformers import AutoModelForCausalLM
from janus.models import MultiModalityCausalLM, VLChatProcessor
from janus.utils.io import load_pil_images
from concurrent.futures import ThreadPoolExecutor
from queue import Queue
import PIL.Image
# 初始化环境变量
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
sound_flag = 1
# 全局线程池
executor = ThreadPoolExecutor(max_workers=4)
task_queue = Queue()
class SpeechBubble(QDialog):
def __init__(self, text, parent=None):
super().__init__(parent)
self.setWindowFlags(
Qt.FramelessWindowHint |
Qt.WindowStaysOnTopHint |
Qt.Tool
)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setAttribute(Qt.WA_ShowWithoutActivating)
self.label = QLabel(text)
self.label.setWordWrap(True)
self.font_size = 24
self.max_width = 400
self.update_style()
shadow = QGraphicsDropShadowEffect(self)
shadow.setBlurRadius(10)
shadow.setXOffset(0)
shadow.setYOffset(2)
shadow.setColor(Qt.gray)
self.label.setGraphicsEffect(shadow)
layout = QVBoxLayout()
layout.addWidget(self.label)
self.setLayout(layout)
self.label.mousePressEvent = lambda e: self.close_bubble()
self.animation = QPropertyAnimation(self, b"windowOpacity")
self.animation.setDuration(300)
self.animation.setStartValue(0)
self.animation.setEndValue(1)
self.animation.setEasingCurve(QEasingCurve.InOutQuad)
self.animation.start()
def close_bubble(self):
self.animation = QPropertyAnimation(self, b"windowOpacity")
self.animation.setDuration(300)
self.animation.setStartValue(1)
self.animation.setEndValue(0)
self.animation.finished.connect(self.close)
self.animation.start()
def update_style(self):
self.label.setStyleSheet(f"""
QLabel {{
background-color: #fff;
color: #333;
border-radius: 10px;
padding: 8px;
font-size: {self.font_size}px;
min-width: 100px;
max-width: {self.max_width}px;
}}
""")
self.adjustSize()
def set_bubble_size(self, size_level):
if size_level == "small":
self.font_size = 10
self.max_width = 150
elif size_level == "medium":
self.font_size = 12
self.max_width = 200
elif size_level == "large":
self.font_size = 14
self.max_width = 250
self.update_style()
# class WorkerThread(QThread):
# """通用工作线程"""
# finished = pyqtSignal(object)
# error = pyqtSignal(str)
# progress = pyqtSignal(int)
#
# def __init__(self, task_func, *args, **kwargs):
# super().__init__()
# self.task_func = task_func
# self.args = args
# self.kwargs = kwargs
# self._is_running = True
#
# def run(self):
# try:
# result = self.task_func(*self.args, **self.kwargs)
# if self._is_running:
# self.finished.emit(result)
# except Exception as e:
# if self._is_running:
# self.error.emit(str(e))
#
# def stop(self):
# self._is_running = False
# self.quit()
class ImageGenerationDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
self.init_ui()
self.setMouseTracking(True)
self.is_generating = False
self.cancel_flag = False
def init_ui(self):
self.setWindowTitle("文本生成图片")
self.setWindowFlags(Qt.Window | Qt.WindowStaysOnTopHint | Qt.WindowCloseButtonHint)
self.setGeometry(300, 300, 400, 300)
layout = QVBoxLayout()
# 文本输入区域
self.text_edit = QTextEdit()
self.text_edit.setPlaceholderText("输入图片描述...")
layout.addWidget(self.text_edit)
# 按钮布局
button_layout = QHBoxLayout()
self.generate_btn = QPushButton("生成图片")
self.generate_btn.clicked.connect(self.generate_image)
button_layout.addWidget(self.generate_btn)
self.cancel_btn = QPushButton("取消")
self.cancel_btn.clicked.connect(self.cancel_generation)
self.cancel_btn.setEnabled(False)
button_layout.addWidget(self.cancel_btn)
layout.addLayout(button_layout)
# 状态显示
self.status_label = QLabel("准备就绪")
layout.addWidget(self.status_label)
# 进度条
self.progress_bar = QProgressBar()
self.progress_bar.setRange(0, 100)
self.progress_bar.setValue(0)
self.progress_bar.setHidden(True)
layout.addWidget(self.progress_bar)
self.setLayout(layout)
def generate_image(self):
if self.is_generating:
return
text = self.text_edit.toPlainText().strip()
if not text:
self._update_status("请输入描述文字", True)
return
self._update_status("正在生成图片...", False)
self.progress_bar.setHidden(False)
self.progress_bar.setValue(0)
self.is_generating = True
self.cancel_flag = False
# 使用线程池执行生成任务
future = executor.submit(self._run_generation, text)
future.add_done_callback(self._generation_done)
def _run_generation(self, text):
"""在后台线程中运行生成任务"""
try:
#torch.cuda.empty_cache() # 生成前清理
if not torch.cuda.is_available():
raise RuntimeError("CUDA不可用,需要NVIDIA GPU支持")
total_mem = torch.cuda.get_device_properties(0).total_memory / (1024 ** 3)
if total_mem < 8:
print(f"警告: 显存较低(仅有{total_mem:.1f}GB),建议使用8GB以上显存")
# 准备模型输入
conversation = [
{"role": "<|User|>", "content": text},
{"role": "<|Assistant|>", "content": ""},
]
sft_format = self.parent.vl_chat_processor.apply_sft_template_for_multi_turn_prompts(
conversations=conversation,
sft_format=self.parent.vl_chat_processor.sft_format,
system_prompt="",
)
prompt = sft_format + self.parent.vl_chat_processor.image_start_tag
# 生成图片
self._generate_images(prompt)
return "图片生成完成!"
except Exception as e:
return f"生成出错: {str(e)}"
finally:
torch.cuda.empty_cache()
def _generation_done(self, future):
"""生成任务完成后的回调"""
try:
result = future.result()
self._update_status(result, True)
self.progress_bar.setValue(100)
QTimer.singleShot(1000, lambda: self.progress_bar.setHidden(True))
except Exception as e:
self._update_status(f"生成出错: {str(e)}", True)
self._log_error(str(e))
finally:
self.is_generating = False
def cancel_generation(self):
self.cancel_flag = True
self.cancel_btn.setEnabled(False)
def _update_status(self, message, enable_btn):
"""线程安全的状态更新"""
QMetaObject.invokeMethod(self.status_label, 'setText',
Qt.QueuedConnection,
Q_ARG(str, message))
QMetaObject.invokeMethod(self.generate_btn, 'setEnabled',
Qt.QueuedConnection,
Q_ARG(bool, enable_btn))
QMetaObject.invokeMethod(self.cancel_btn, 'setEnabled',
Qt.QueuedConnection,
Q_ARG(bool, not enable_btn))
def _log_error(self, error_msg):
"""记录错误日志"""
with open("image_gen_errors.log", "a") as f:
f.write(f"{time.ctime()}\n{error_msg}\n\n")
@torch.inference_mode()
def _generate_images(self, prompt,
parallel_size: int = 1,
cfg_weight: float = 5,
temperature: float = 1,
img_size: int = 128,
patch_size: int = 8):
try:
# 计算正确的token数量
grid_size = img_size // patch_size
image_token_num_per_image = 24 * grid_size * grid_size # 8通道 × 高度网格 × 宽度网格
# 编码文本输入
input_ids = self.parent.vl_chat_processor.tokenizer.encode(prompt)
input_ids = torch.LongTensor(input_ids).cuda()
tokens = torch.zeros((parallel_size * 2, len(input_ids)), dtype=torch.int).cuda()
for i in range(parallel_size * 2):
tokens[i, :] = input_ids
if i % 2 != 0:
tokens[i, 1:-1] = self.parent.vl_chat_processor.pad_id
inputs_embeds = self.parent.vl_gpt.language_model.get_input_embeddings()(tokens)
generated_tokens = torch.zeros((parallel_size, image_token_num_per_image), dtype=torch.int).cuda()
# 生成过程
for i in range(image_token_num_per_image):
if self.cancel_flag:
raise RuntimeError("用户取消了生成")
outputs = self.parent.vl_gpt.language_model.model(
inputs_embeds=inputs_embeds,
use_cache=True,
past_key_values=outputs.past_key_values if i != 0 else None
)
hidden_states = outputs.last_hidden_state
logits = self.parent.vl_gpt.gen_head(hidden_states[:, -1, :])
logit_cond = logits[0::2, :]
logit_uncond = logits[1::2, :]
logits = logit_uncond + cfg_weight * (logit_cond - logit_uncond)
probs = torch.softmax(logits / temperature, dim=-1)
next_token = torch.multinomial(probs, num_samples=1)
generated_tokens[:, i] = next_token.squeeze(dim=-1)
next_token = torch.cat([next_token.unsqueeze(dim=1), next_token.unsqueeze(dim=1)], dim=1).view(-1)
img_embeds = self.parent.vl_gpt.prepare_gen_img_embeds(next_token)
inputs_embeds = img_embeds.unsqueeze(dim=1)
# 更新进度
progress = int((i + 1) / image_token_num_per_image * 100)
self.progress_bar.setValue(progress)
# 解码时确保形状正确
expected_shape = [parallel_size, 24, grid_size, grid_size]
print("expected_shape=",expected_shape)
dec = self.parent.vl_gpt.gen_vision_model.decode_code(
generated_tokens.to(dtype=torch.int),
shape=expected_shape
)
# 后处理
dec = dec.to(torch.float32).cpu().numpy()
dec = dec.transpose(0, 2, 3, 1) # 转换为NHWC格式
dec = dec[..., :3]
dec = np.clip((dec + 1) / 2 * 255, 0, 255).astype(np.uint8)
# 保存图片
os.makedirs('generated_samples', exist_ok=True)
for i in range(parallel_size):
img = PIL.Image.fromarray(dec[i])
save_path = os.path.join('generated_samples', f"img_{int(time.time())}_{i}.jpg")
img.save(save_path)
except Exception as e:
raise RuntimeError(f"生成过程中出错: {str(e)}")
class ChatWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
self.image_path = ""
self.init_ui()
self.setMouseTracking(True)
def mousePressEvent(self, event):
event.accept()
def init_ui(self):
self.setWindowTitle("AI对话")
self.setWindowFlags(Qt.Window | Qt.WindowStaysOnTopHint | Qt.WindowCloseButtonHint)
self.setGeometry(300, 300, 500, 400)
layout = QVBoxLayout()
# 聊天显示区域
self.chat_display = QTextEdit()
self.chat_display.setReadOnly(True)
layout.addWidget(self.chat_display)
# 输入区域
input_layout = QHBoxLayout()
self.input_field = QLineEdit()
self.input_field.setPlaceholderText("输入您的问题...")
self.input_field.returnPressed.connect(self.send_message)
input_layout.addWidget(self.input_field)
# 按钮区域
button_layout = QHBoxLayout()
self.send_button = QPushButton("发送")
self.send_button.clicked.connect(self.send_message)
button_layout.addWidget(self.send_button)
self.image_button = QPushButton("添加图片")
self.image_button.clicked.connect(self.add_image)
button_layout.addWidget(self.image_button)
layout.addLayout(input_layout)
layout.addLayout(button_layout)
self.setLayout(layout)
def send_message(self):
"""处理发送消息的逻辑"""
question = self.input_field.text().strip()
if not question:
return
self.chat_display.append(f"你: {question}")
self.input_field.clear()
# 在后台线程中生成回答
self.generate_response_threaded(question)
def generate_response_threaded(self, question):
"""在后台线程中生成回答"""
def task():
try:
answer = self.parent.generate_response(question)
return answer
except Exception as e:
return f"生成回答出错: {str(e)}"
def callback(future):
try:
answer = future.result()
self.chat_display.append(f"AI: {answer}\n")
self.chat_display.ensureCursorVisible()
# 只在非静音状态下播放语音
global sound_flag
if sound_flag == 1:
self.parent.play_audio_from_text(answer)
except Exception as e:
self.chat_display.append(f"AI: 处理回答时出错: {str(e)}\n")
future = executor.submit(task)
future.add_done_callback(callback)
def add_image(self):
"""添加图片功能"""
file_path, _ = QFileDialog.getOpenFileName(
self, "选择图片", "", "图片文件 (*.png *.jpg *.jpeg)"
)
if file_path:
self.image_path = file_path
self.chat_display.append(f"[已添加图片: {os.path.basename(file_path)}]")
class DesktopPet(QWidget):
close_speech_bubble_signal = pyqtSignal()
def __init__(self, pic_tuopan, pifu, model_path, API_URL):
super().__init__()
self.pifu = pifu
self.pic_tuopan = pic_tuopan
self.model_path = model_path
self.API_URL = API_URL
self.init_ui()
self.init_model()
self.drag_pos = QPoint()
self.chat_window = None
self.image_gen_window = None
self.close_speech_bubble_signal.connect(self.close_speech_bubble)
self.bubble_size = "medium"
self.current_bubble = None
# 初始化音频系统
self.init_audio()
# 自动发言定时器
self.auto_speak_timer = QTimer(self)
self.auto_speak_timer.timeout.connect(self.auto_speak)
self.auto_speak_timer.start(random.randint(20000, 60000))
def contextMenuEvent(self, event):
"""重写右键菜单事件,显示与托盘菜单相同的菜单"""
# 创建与托盘菜单相同的菜单
menu = QMenu(self)
# 添加设置菜单项
settings_action = menu.addAction("设置")
settings_menu = QMenu()
settings_action.setMenu(settings_menu)
# 添加桌宠大小设置子菜单
size_menu = settings_menu.addMenu("设置桌宠大小")
sizes = [("小 (90x90)", 90), ("中 (150x150)", 150), ("大 (200x200)", 200)]
for text, size in sizes:
action = size_menu.addAction(text)
action.triggered.connect(lambda checked, s=size: self.set_pet_size(s))
# 气泡大小设置子菜单
bubble_size_menu = settings_menu.addMenu("设置气泡大小")
bubble_sizes = [("小号气泡", "small"), ("中号气泡", "medium"), ("大号气泡", "large")]
for text, size in bubble_sizes:
action = bubble_size_menu.addAction(text)
action.triggered.connect(lambda checked, s=size: self.set_bubble_size(s))
# 添加切换显示/隐藏选项
toggle_text = "隐藏" if self.is_window_visible else "显示"
self.toggle_action = menu.addAction(toggle_text)
self.toggle_action.triggered.connect(self.toggle_window_visibility)
# 添加其他菜单项
chat_action = menu.addAction("开始对话")
gen_image_action = menu.addAction("文本生成图片")
# 添加静音选项
global sound_flag
mute_text = "静音" if sound_flag == 1 else "取消静音"
mute_action = menu.addAction(mute_text)
mute_action.triggered.connect(self.toggle_mute)
exit_action = menu.addAction("退出")
# 连接信号
chat_action.triggered.connect(self.show_chat_window)
gen_image_action.triggered.connect(self.show_image_gen_window)
exit_action.triggered.connect(self.close_app)
# 在鼠标位置显示菜单
menu.exec_(event.globalPos())
def init_audio(self):
"""初始化音频系统"""
try:
pygame.mixer.init()
except Exception as e:
print(f"音频初始化失败: {e}")
def auto_speak(self):
"""自动发言"""
self.auto_speak_timer.start(random.randint(20000, 60000))
try:
if self.image_gen_window or self.chat_window or not self.isVisible():
return
auto_messages = [
"You look great today.",
"Are you ready to do something that makes you comfortable together?",
"Is there anything I can do for you?",
"I'm always here to serve you at any time.",
"Click here and I can start the conversation."
]
message = random.choice(auto_messages)
self.show_speech_bubble(message)
# 检查全局静音状态
global sound_flag
# if sound_flag == 1:
# self.play_audio_from_text(message)
except Exception as e:
print(f"自动发言出错: {e}")
self.auto_speak_timer.start(30000)
def set_bubble_size(self, size_level):
self.bubble_size = size_level
if self.current_bubble:
self.current_bubble.set_bubble_size(size_level)
def show_speech_bubble(self, text):
"""显示聊天气泡"""
def _show():
if self.current_bubble:
self.current_bubble.close_bubble()
self.current_bubble = SpeechBubble(text, self)
self.current_bubble.set_bubble_size(self.bubble_size)
self.current_bubble.show()
# 定位气泡
bubble_x = self.x() + (self.width() - self.current_bubble.width()) // 2
bubble_y = self.y() - self.current_bubble.height() - 10
self.current_bubble.move(bubble_x, bubble_y)
# 确保在主线程执行
if QThread.currentThread() != self.thread():
QMetaObject.invokeMethod(self, '_show_bubble',
Qt.QueuedConnection,
Q_ARG(str, text))
else:
_show()
def close_speech_bubble(self):
if self.current_bubble and self.current_bubble.isVisible():
self.current_bubble.close_bubble()
self.current_bubble = None
def toggle_window_visibility(self):
if self.is_window_visible:
self.hide()
self.toggle_action.setText("显示")
else:
self.show()
self.toggle_action.setText("隐藏")
self.is_window_visible = not self.is_window_visible
def init_ui(self):
self.setWindowTitle("AI对话")
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
self.setAttribute(Qt.WA_TranslucentBackground)
# 创建托盘图标
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon(self.pic_tuopan))
self.tray_icon.setToolTip("glare-A")
# 创建托盘菜单
tray_menu = QMenu()
self.is_window_visible = True
# 添加设置菜单项
settings_action = tray_menu.addAction("设置")
settings_menu = QMenu()
settings_action.setMenu(settings_menu)
# 添加桌宠大小设置子菜单
size_menu = settings_menu.addMenu("设置桌宠大小")
sizes = [("小 (90x90)", 90), ("中 (150x150)", 150), ("大 (200x200)", 200)]
for text, size in sizes:
action = size_menu.addAction(text)
action.triggered.connect(lambda checked, s=size: self.set_pet_size(s))
# 气泡大小设置子菜单
bubble_size_menu = settings_menu.addMenu("设置气泡大小")
bubble_sizes = [("小号气泡", "small"), ("中号气泡", "medium"), ("大号气泡", "large")]
for text, size in bubble_sizes:
action = bubble_size_menu.addAction(text)
action.triggered.connect(lambda checked, s=size: self.set_bubble_size(s))
self.toggle_action = tray_menu.addAction("隐藏")
self.toggle_action.triggered.connect(self.toggle_window_visibility)
self.mute_action = tray_menu.addAction("静音")
self.mute_action.triggered.connect(self.toggle_mute)
chat_action = tray_menu.addAction("开始对话")
gen_image_action = tray_menu.addAction("文本生成图片")
exit_action = tray_menu.addAction("退出")
chat_action.triggered.connect(self.show_chat_window)
gen_image_action.triggered.connect(self.show_image_gen_window)
exit_action.triggered.connect(self.close_app)
self.tray_icon.setContextMenu(tray_menu)
self.tray_icon.show()
# 设置宠物图标
self.pet_label = QLabel(self)
self.movie = None
try:
self.movie = QMovie(self.pifu)
if self.movie.isValid():
self.movie.setCacheMode(QMovie.CacheAll)
self.pet_label.setMovie(self.movie)
self.movie.start()
else:
self.pet_label.setPixmap(QPixmap(self.pifu))
except:
self.pet_label.setPixmap(QPixmap(self.pifu))
self.pet_label.setAlignment(Qt.AlignCenter)
self.set_pet_size(90)
def show_image_gen_window(self):
if self.image_gen_window is None:
self.image_gen_window = ImageGenerationDialog(self)
self.image_gen_window.setWindowFlags(
Qt.WindowStaysOnTopHint | Qt.Tool
)
self.image_gen_window.setAttribute(Qt.WA_ShowWithoutActivating)
self.image_gen_window.show()
self.update_image_gen_window_position()
def update_image_gen_window_position(self):
if self.image_gen_window:
title_bar_height = self.image_gen_window.style().pixelMetric(QStyle.PM_TitleBarHeight)
gen_x = self.x() - (self.image_gen_window.width() - self.width()) // 2
gen_y = self.y() - self.image_gen_window.height() - title_bar_height
gen_y = max(0, gen_y)
self.image_gen_window.move(gen_x, gen_y)
def set_pet_size(self, size):
if self.movie and self.movie.isValid():
self.movie.stop()
original_size = self.movie.frameRect().size()
scaled_size = original_size.scaled(size, size, Qt.KeepAspectRatio)
self.movie.setScaledSize(scaled_size)
self.pet_label.resize(scaled_size)
self.setFixedSize(scaled_size)
self.movie.start()
else:
pixmap = QPixmap(self.pifu)
if not pixmap.isNull():
scaled_pixmap = pixmap.scaled(size, size, Qt.KeepAspectRatio)
self.pet_label.setPixmap(scaled_pixmap)
self.pet_label.resize(scaled_pixmap.size())
self.setFixedSize(scaled_pixmap.size())
self.pet_label.setAlignment(Qt.AlignCenter)
self.update()
def init_model(self):
"""初始化模型"""
def _load_model():
try:
self.vl_chat_processor = VLChatProcessor.from_pretrained(self.model_path)
self.tokenizer = self.vl_chat_processor.tokenizer
self.vl_gpt = AutoModelForCausalLM.from_pretrained(
self.model_path, trust_remote_code=True
)
self.vl_gpt = self.vl_gpt.to(torch.bfloat16).cuda().eval()
print("模型加载完成")
except Exception as e:
print(f"模型加载失败: {e}")
# 在后台线程加载模型
threading.Thread(target=_load_model, daemon=True).start()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(event.globalPos() - self.drag_pos)
if self.chat_window:
self.update_chat_window_position()
if self.image_gen_window:
self.update_image_gen_window_position()
event.accept()
def update_chat_window_position(self):
if self.chat_window:
title_bar_height = self.chat_window.style().pixelMetric(QStyle.PM_TitleBarHeight)
chat_x = self.x() - (self.chat_window.width() - self.width()) // 2
chat_y = self.y() - self.chat_window.height() - title_bar_height
chat_y = max(0, chat_y)
self.chat_window.move(chat_x, chat_y)
def toggle_mute(self):
global sound_flag
try:
if sound_flag == 1:
sound_flag = 0
self.mute_action.setText("取消静音")
else:
sound_flag = 1
self.mute_action.setText("静音")
except Exception as e:
print(f"静音切换出错: {e}")
def show_chat_window(self):
if hasattr(self, 'chat_window') and self.chat_window:
self.chat_window.close()
if not self.chat_window:
self.chat_window = ChatWindow(self)
self.chat_window.setWindowFlags(
Qt.WindowStaysOnTopHint | Qt.Tool
)
self.chat_window.setAttribute(Qt.WA_ShowWithoutActivating)
self.chat_window.closeEvent = self._handle_chat_window_close
self.chat_window.show()
self.update_chat_window_position()
self.chat_window.raise_()
def _handle_chat_window_close(self, event):
self.chat_window.close()
event.accept()
self.chat_window = None
def close_app(self):
if self.chat_window:
self.chat_window.close()
if self.image_gen_window:
self.image_gen_window.close()
self.tray_icon.hide()
QApplication.quit()
def play_audio_from_text(self, text):
"""实现TTS播放功能"""
def _tts_task():
try:
output_dir = os.path.join(os.getcwd(), "outputs")
os.makedirs(output_dir, exist_ok=True)
timestamp = int(time.time())
save_path = os.path.join(output_dir, f"output_{timestamp}.wav")
tts_payload = {
"text": text,
"text_lang": "auto",
"ref_audio_path": r"GPT_SoVITS\ref\cyn.wav",
"prompt_lang": "zh",
"text_split_method": "cut5",
"batch_size": 1,
"media_type": "wav",
"speed_factor": 0.9,
"streaming_mode": False
}
response = requests.post(self.API_URL, json=tts_payload)
if response.status_code == 200:
with open(save_path, "wb") as audio_file:
audio_file.write(response.content)
return save_path
else:
print("TTS合成失败:", response.json())
return None
except Exception as e:
print(f"TTS请求出错: {e}")
return None
def _tts_done(future):
try:
audio_path = future.result()
if audio_path:
self.play_audio(audio_path)
except Exception as e:
print(f"TTS处理出错: {e}")
# 提交TTS任务到线程池
future = executor.submit(_tts_task)
future.add_done_callback(_tts_done)
def play_audio(self, file_path):
"""播放音频"""
def _play():
try:
if pygame.mixer.get_init() is None:
pygame.mixer.init()
global sound_flag
pygame.mixer.music.set_volume(1.0 if sound_flag == 1 else 0.0)
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.05)
self.close_speech_bubble_signal.emit()
except Exception as e:
print(f"音频播放出错: {e}")
finally:
pygame.mixer.music.stop()
try:
os.remove(file_path)
except Exception as e:
print(f"删除文件失败: {e}")
# 在单独线程中播放音频
threading.Thread(target=_play, daemon=True).start()
def generate_response(self, question):
"""生成AI回答"""
try:
conversation = [{
"role": "<|User|>",
"content": f"<image_placeholder>\n{question}" if self.chat_window.image_path else question,
"images": [self.chat_window.image_path] if self.chat_window.image_path else []
}, {"role": "<|Assistant|>", "content": ""}]
pil_images = load_pil_images(conversation) if self.chat_window.image_path else []
prepare_inputs = self.vl_chat_processor(
conversations=conversation,
images=pil_images,
force_batchify=True
).to(self.vl_gpt.device)
inputs_embeds = self.vl_gpt.prepare_inputs_embeds(**prepare_inputs)
outputs = self.vl_gpt.language_model.generate(
inputs_embeds=inputs_embeds,
attention_mask=prepare_inputs.attention_mask,
pad_token_id=self.tokenizer.eos_token_id,
bos_token_id=self.tokenizer.bos_token_id,
eos_token_id=self.tokenizer.eos_token_id,
max_new_tokens=512,
do_sample=False,
use_cache=True,
)
self.chat_window.image_path = ""
return self.tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
except Exception as e:
print(f"生成回答出错: {e}")
return "抱歉,我遇到了一些问题,请稍后再试。"
def main():
pic_tuopan = "test_pic/desk0.jpg"
pifu = "test_pic/desk1.gif"
model_path = "./Janus-Pro-7B_0"
API_URL = "http://127.0.0.1:9880/tts"
if not torch.cuda.is_available():
print("错误: 未检测到CUDA设备,需要NVIDIA GPU支持")
return
app = QApplication(sys.argv)
pet = DesktopPet(pic_tuopan, pifu, model_path, API_URL)
pet.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()