-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
813 lines (716 loc) · 32.1 KB
/
Copy pathmain.py
File metadata and controls
813 lines (716 loc) · 32.1 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
from __future__ import annotations
import html
import json
import os
import sys
import traceback
import uuid
import markdown2
from PyQt6.QtCore import QThread, Qt, pyqtSignal
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import (
QApplication,
QCheckBox,
QComboBox,
QFileDialog,
QHBoxLayout,
QLabel,
QLineEdit,
QListWidget,
QListWidgetItem,
QMenu,
QPushButton,
QSplitter,
QTextEdit,
QVBoxLayout,
QWidget,
)
from core.catalog import (
Catalog,
ModelEntry,
get_catalog,
gguf_quant_label,
list_gguf_files,
parse_model_ref,
pick_gguf_file,
refresh_catalog,
)
from core.config import load_settings, save_settings
from core.device import describe_device
from core.generate import generate
from core.models import LoadedModel, is_gguf_model, is_vl_model, load_model, unload_model
from core.rag import RagIndex
from settings import SettingsWindow
class ModelWorker(QThread):
model_loaded = pyqtSignal(object)
error = pyqtSignal(str)
status_update = pyqtSignal(str)
def __init__(self, model_name: str):
super().__init__()
self.model_name = model_name
def run(self):
try:
loaded = load_model(self.model_name, status_callback=self.status_update.emit)
self.model_loaded.emit(loaded)
except Exception as exc:
self.error.emit(
f"Erreur de chargement du modèle : {exc}\n\nTraceback:\n{traceback.format_exc()}"
)
class GenerationWorker(QThread):
generation_complete = pyqtSignal(str)
new_token = pyqtSignal(str)
error = pyqtSignal(str)
stats = pyqtSignal(float)
def __init__(self, loaded_model: LoadedModel, conversation_history, settings, image_path=None):
super().__init__()
self.loaded_model = loaded_model
self.conversation_history = conversation_history
self.settings = settings
self.image_path = image_path
def run(self):
try:
result, tokens_per_sec = generate(
self.loaded_model,
self.conversation_history,
self.settings,
image_path=self.image_path,
on_token=self.new_token.emit,
)
self.stats.emit(tokens_per_sec)
self.generation_complete.emit(result)
except Exception as exc:
self.error.emit(
f"Erreur de génération : {exc}\n\nTraceback:\n{traceback.format_exc()}"
)
class RagWorker(QThread):
status_update = pyqtSignal(str)
index_ready = pyqtSignal(int)
error = pyqtSignal(str)
def __init__(self, rag_index: RagIndex, files, chunk_size: int, chunk_overlap: int):
super().__init__()
self.rag_index = rag_index
self.files = files
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
def run(self):
try:
self.status_update.emit(f"Copie de {len(self.files)} document(s)...")
self.rag_index.add_files(self.files)
self.status_update.emit("Création de la base de données vectorielle...")
count = self.rag_index.rebuild(self.chunk_size, self.chunk_overlap)
self.index_ready.emit(count)
except Exception as exc:
self.error.emit(
f"Erreur lors de la création de l'index RAG : {exc}\n\nTraceback:\n{traceback.format_exc()}"
)
class LiquidAIApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("LiquidAI Chat")
self.setGeometry(100, 100, 1000, 700)
self.loaded_model = None
self.current_conversation_id = None
self.conversations = {}
self.settings = load_settings()
self.rag_enabled = False
self.rag_index = RagIndex("documents/")
self.current_assistant_message = ""
self.selected_image_path = None
self.busy = False
self.catalog = get_catalog(prefer_cache=True)
self.catalog_entries: list[ModelEntry] = list(self.catalog.models)
self._gguf_fetching: set[str] = set()
self.init_ui()
self.load_conversations()
self.check_device()
self.populate_model_selector()
self.chat_area.append(
"<i>Aucun modèle chargé. Filtrez ou choisissez un modèle puis cliquez sur Charger.</i>"
)
self.chat_area.append(
f"<i>Catalogue : {len(self.catalog_entries)} modèles "
f"(source : {html.escape(self.catalog.source)}). "
"Cliquez sur « Actualiser HF » pour scanner LiquidAI sur Hugging Face.</i>"
)
if self.catalog.is_stale():
self.refresh_huggingface_catalog(silent=True)
if not self.conversations:
self.start_new_conversation()
else:
self.history_list.setCurrentRow(0)
self.load_selected_conversation(self.history_list.item(0))
def init_ui(self):
main_layout = QHBoxLayout(self)
left_panel = QWidget()
left_layout = QVBoxLayout(left_panel)
left_layout.setContentsMargins(0, 0, 0, 0)
self.history_list = QListWidget(left_panel)
self.history_list.itemClicked.connect(self.load_selected_conversation)
self.history_list.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.history_list.customContextMenuRequested.connect(self.show_conversation_context_menu)
left_layout.addWidget(self.history_list)
new_chat_button = QPushButton("Nouvelle Discussion")
new_chat_button.clicked.connect(self.start_new_conversation)
left_layout.addWidget(new_chat_button)
right_panel = QWidget()
right_layout = QVBoxLayout(right_panel)
right_layout.setContentsMargins(0, 0, 0, 0)
self.chat_area = QTextEdit()
self.chat_area.setReadOnly(True)
self.chat_area.setStyleSheet("font-size: 14px; color: #f2f2f2; background-color: #2b2b2b;")
right_layout.addWidget(self.chat_area)
filter_layout = QHBoxLayout()
self.model_filter = QLineEdit()
self.model_filter.setPlaceholderText("Filtrer (ex. 2.5, GGUF, VL, Thinking)...")
self.model_filter.textChanged.connect(self.apply_model_filter)
self.refresh_hf_button = QPushButton("Actualiser HF")
self.refresh_hf_button.setToolTip(
"Scanner automatiquement tous les modèles LFM du compte LiquidAI, y compris les fichiers .gguf."
)
self.refresh_hf_button.clicked.connect(lambda: self.refresh_huggingface_catalog(silent=False))
filter_layout.addWidget(self.model_filter)
filter_layout.addWidget(self.refresh_hf_button)
right_layout.addLayout(filter_layout)
model_controls_layout = QHBoxLayout()
self.model_selector = QComboBox()
self.model_selector.setEditable(True)
self.model_selector.setInsertPolicy(QComboBox.InsertPolicy.NoInsert)
self.model_selector.setMaxVisibleItems(20)
self.model_selector.currentIndexChanged.connect(self.on_model_index_changed)
self.gguf_selector = QComboBox()
self.gguf_selector.setVisible(False)
self.gguf_selector.setMinimumWidth(160)
self.gguf_selector.setToolTip("Fichier / quantification GGUF détecté sur le dépôt Hugging Face.")
self.gguf_label = QLabel("GGUF:")
self.gguf_label.setVisible(False)
self.load_button = QPushButton("Charger")
self.load_button.clicked.connect(self.load_selected_model)
settings_button = QPushButton("Paramètres")
settings_button.clicked.connect(self.open_settings)
self.eject_button = QPushButton("Éjecter")
self.eject_button.clicked.connect(self.eject_model)
self.eject_button.setEnabled(False)
model_controls_layout.addWidget(QLabel("Modèle:"))
model_controls_layout.addWidget(self.model_selector, 2)
model_controls_layout.addWidget(self.gguf_label)
model_controls_layout.addWidget(self.gguf_selector, 1)
model_controls_layout.addWidget(self.load_button)
model_controls_layout.addWidget(self.eject_button)
model_controls_layout.addWidget(settings_button)
right_layout.addLayout(model_controls_layout)
self.image_input_container = QWidget()
image_input_layout = QHBoxLayout(self.image_input_container)
image_input_layout.setContentsMargins(0, 5, 0, 5)
self.select_image_button = QPushButton("Sélectionner une image")
self.select_image_button.clicked.connect(self.select_image)
self.image_thumbnail_label = QLabel()
self.image_thumbnail_label.setFixedSize(64, 64)
self.image_thumbnail_label.setStyleSheet("border: 1px solid #555;")
self.image_filename_label = QLabel("Aucune image sélectionnée")
self.clear_image_button = QPushButton("X")
self.clear_image_button.setFixedSize(30, 30)
self.clear_image_button.clicked.connect(self.clear_image)
image_input_layout.addWidget(self.select_image_button)
image_input_layout.addWidget(self.image_thumbnail_label)
image_input_layout.addWidget(self.image_filename_label)
image_input_layout.addStretch()
image_input_layout.addWidget(self.clear_image_button)
right_layout.addWidget(self.image_input_container)
self.image_input_container.setVisible(False)
input_layout = QHBoxLayout()
self.input_field = QLineEdit()
self.input_field.setStyleSheet("font-size: 14px;")
self.input_field.setPlaceholderText("Posez votre question ici...")
self.input_field.returnPressed.connect(self.send_message)
self.send_button = QPushButton("Envoyer")
self.send_button.clicked.connect(self.send_message)
input_layout.addWidget(self.input_field)
input_layout.addWidget(self.send_button)
right_layout.addLayout(input_layout)
rag_layout = QHBoxLayout()
self.load_docs_button = QPushButton("Charger Documents")
self.load_docs_button.clicked.connect(self.load_documents)
self.rag_toggle_checkbox = QCheckBox("Activer RAG")
self.rag_toggle_checkbox.stateChanged.connect(self.toggle_rag)
self.rag_status_label = QLabel("RAG: Inactif - Aucun document chargé")
self.stats_label = QLabel("")
rag_layout.addWidget(self.load_docs_button)
rag_layout.addWidget(self.rag_toggle_checkbox)
rag_layout.addWidget(self.rag_status_label)
rag_layout.addStretch()
rag_layout.addWidget(self.stats_label)
right_layout.addLayout(rag_layout)
splitter = QSplitter(Qt.Orientation.Horizontal)
splitter.addWidget(left_panel)
splitter.addWidget(right_panel)
splitter.setSizes([250, 750])
main_layout.addWidget(splitter)
self.setLayout(main_layout)
self.set_ui_enabled(True)
def show_conversation_context_menu(self, position):
item = self.history_list.itemAt(position)
if not item:
return
context_menu = QMenu(self)
delete_action = context_menu.addAction("Supprimer")
action = context_menu.exec(self.history_list.mapToGlobal(position))
if action == delete_action:
self.delete_conversation(item)
def delete_conversation(self, item):
conv_id = item.data(Qt.ItemDataRole.UserRole)
file_path = f"conversations/{conv_id}.json"
if os.path.exists(file_path):
try:
os.remove(file_path)
except OSError as exc:
self.on_error(f"Impossible de supprimer le fichier {file_path}: {exc}")
return
if conv_id in self.conversations:
del self.conversations[conv_id]
row = self.history_list.row(item)
self.history_list.takeItem(row)
if self.current_conversation_id == conv_id:
if self.history_list.count() > 0:
first_item = self.history_list.item(0)
self.history_list.setCurrentItem(first_item)
self.load_selected_conversation(first_item)
else:
self.start_new_conversation()
def check_device(self):
self.chat_area.append(f"<i>Utilisation de l'appareil : {html.escape(describe_device())}</i>")
def apply_model_filter(self, _text: str = ""):
self.populate_model_selector(keep_selection=True)
def _matching_entries(self) -> list[ModelEntry]:
query = self.model_filter.text().strip().lower()
if not query:
return list(self.catalog_entries)
tokens = query.split()
matches = []
for entry in self.catalog_entries:
haystack = f"{entry.repo_id} {entry.label} {entry.kind}".lower()
if all(token in haystack for token in tokens):
matches.append(entry)
return matches
def populate_model_selector(self, keep_selection: bool = False):
previous = self.current_repo_id() if keep_selection else ""
matches = self._matching_entries()
self.model_selector.blockSignals(True)
self.model_selector.clear()
last_kind = None
headers = {"text": "── Texte / Instruct ──", "vl": "── Vision ──", "gguf": "── GGUF ──"}
for entry in matches:
if entry.kind != last_kind:
self.model_selector.addItem(headers.get(entry.kind, f"── {entry.kind} ──"))
index = self.model_selector.count() - 1
self.model_selector.model().item(index).setEnabled(False)
last_kind = entry.kind
self.model_selector.addItem(entry.label, entry.repo_id)
selected = False
if previous:
idx = self.model_selector.findData(previous)
if idx >= 0:
self.model_selector.setCurrentIndex(idx)
selected = True
if not selected:
for row in range(self.model_selector.count()):
if self.model_selector.itemData(row):
self.model_selector.setCurrentIndex(row)
break
self.model_selector.blockSignals(False)
self.on_model_index_changed(self.model_selector.currentIndex())
def current_repo_id(self) -> str:
data = self.model_selector.currentData()
if data:
return str(data)
text = self.model_selector.currentText().strip()
if text.startswith("LiquidAI/"):
return parse_model_ref(text)[0]
for entry in self.catalog_entries:
if entry.label == text or entry.short_name == text:
return entry.repo_id
return text
def current_model_ref(self) -> str:
repo = self.current_repo_id()
if not repo:
return ""
if self.gguf_selector.isVisible() and self.gguf_selector.currentData():
return f"{repo}:{self.gguf_selector.currentData()}"
return repo
def current_entry(self) -> ModelEntry | None:
repo = self.current_repo_id()
for entry in self.catalog_entries:
if entry.repo_id == repo:
return entry
if repo:
kind = "gguf" if is_gguf_model(repo) else "vl" if is_vl_model(repo) else "text"
return ModelEntry(repo_id=repo, kind=kind)
return None
def refresh_huggingface_catalog(self, silent: bool = False):
if not silent:
self.chat_area.append("<i>Actualisation du catalogue depuis Hugging Face...</i>")
self.refresh_hf_button.setEnabled(False)
self.catalog_worker = CatalogWorker()
self.catalog_worker.catalog_ready.connect(self.on_catalog_ready)
self.catalog_worker.status_update.connect(self.on_status_update)
self.catalog_worker.error.connect(self.on_catalog_error)
self.catalog_worker.start()
def on_catalog_ready(self, catalog: Catalog):
self.catalog = catalog
self.catalog_entries = list(catalog.models)
self.populate_model_selector(keep_selection=True)
self.refresh_hf_button.setEnabled(True)
gguf_count = sum(1 for entry in catalog.models if entry.kind == "gguf")
self.chat_area.append(
f"<i>Catalogue Hugging Face prêt : {len(catalog.models)} modèles LFM "
f"dont {gguf_count} dépôts GGUF.</i>"
)
def on_catalog_error(self, message: str):
self.refresh_hf_button.setEnabled(True)
self.on_error(message)
def populate_gguf_files(self, entry: ModelEntry):
self.gguf_selector.blockSignals(True)
self.gguf_selector.clear()
files = list(entry.gguf_files)
if files:
preferred = pick_gguf_file(files)
for filename in files:
self.gguf_selector.addItem(gguf_quant_label(filename), filename)
idx = self.gguf_selector.findData(preferred)
if idx >= 0:
self.gguf_selector.setCurrentIndex(idx)
else:
self.gguf_selector.addItem("Recherche des fichiers...", None)
if entry.repo_id not in self._gguf_fetching:
self._gguf_fetching.add(entry.repo_id)
self.gguf_files_worker = GgufFilesWorker(entry.repo_id)
self.gguf_files_worker.files_ready.connect(self.on_gguf_files_ready)
self.gguf_files_worker.error.connect(self.on_status_update)
self.gguf_files_worker.start()
self.gguf_selector.blockSignals(False)
def on_gguf_files_ready(self, repo_id: str, files: list):
self._gguf_fetching.discard(repo_id)
entry = None
for item in self.catalog_entries:
if item.repo_id == repo_id:
item.gguf_files = list(files)
entry = item
break
if self.current_repo_id() != repo_id:
return
if not files:
self.gguf_selector.clear()
self.gguf_selector.addItem("Aucun .gguf trouvé", None)
return
if entry:
self.populate_gguf_files(entry)
def open_settings(self):
dialog = SettingsWindow(self)
dialog.set_settings(self.settings)
if dialog.exec():
self.settings = dialog.get_settings()
save_settings(self.settings)
self.chat_area.append("<i>Paramètres mis à jour.</i>")
if self.current_conversation_id:
history = self.conversations.get(self.current_conversation_id, [])
if history and history[0].get("role") == "system":
history[0] = {"role": "system", "content": self.settings["system_prompt"]}
else:
history.insert(0, {"role": "system", "content": self.settings["system_prompt"]})
self.display_current_conversation()
def on_model_index_changed(self, _index: int = 0):
entry = self.current_entry()
if entry is None:
self.image_input_container.setVisible(False)
self.gguf_selector.setVisible(False)
self.gguf_label.setVisible(False)
return
vl_selected = entry.kind == "vl"
gguf_selected = entry.kind == "gguf"
self.image_input_container.setVisible(vl_selected)
self.gguf_selector.setVisible(gguf_selected)
self.gguf_label.setVisible(gguf_selected)
if not vl_selected:
self.clear_image()
if gguf_selected:
self.populate_gguf_files(entry)
def load_selected_model(self):
model_identifier = self.current_model_ref()
if not model_identifier:
return
if self.loaded_model and self.loaded_model.name == model_identifier:
self.chat_area.append(f"<i>Le modèle {html.escape(model_identifier)} est déjà chargé.</i>")
return
if self.loaded_model:
unload_model(self.loaded_model)
self.loaded_model = None
self.load_model(model_identifier)
def select_image(self):
file_path, _ = QFileDialog.getOpenFileName(
self, "Sélectionner une image", "", "Images (*.png *.jpg *.jpeg *.webp)"
)
if file_path:
self.selected_image_path = file_path
pixmap = QPixmap(file_path)
self.image_thumbnail_label.setPixmap(
pixmap.scaled(
64,
64,
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation,
)
)
self.image_filename_label.setText(os.path.basename(file_path))
def clear_image(self):
self.selected_image_path = None
self.image_thumbnail_label.clear()
self.image_filename_label.setText("Aucune image sélectionnée")
def load_model(self, model_identifier: str):
self.set_ui_enabled(False)
self.chat_area.append(f"<i>Préparation du chargement du modèle {html.escape(model_identifier)}...</i>")
self.worker = ModelWorker(model_identifier)
self.worker.model_loaded.connect(self.on_model_loaded)
self.worker.error.connect(self.on_error)
self.worker.status_update.connect(self.on_status_update)
self.worker.start()
def on_status_update(self, message: str):
self.chat_area.append(f"<i>{html.escape(message)}</i>")
def on_model_loaded(self, loaded: LoadedModel):
self.loaded_model = loaded
self.chat_area.append(f"<i>Modèle {html.escape(loaded.name)} chargé.</i>")
self.set_ui_enabled(True)
if self.current_conversation_id:
self.display_current_conversation()
def eject_model(self):
if self.loaded_model is None:
return
model_name = self.loaded_model.name
self.chat_area.append(f"<i>Déchargement du modèle {html.escape(model_name)}...</i>")
unload_model(self.loaded_model)
self.loaded_model = None
self.chat_area.append("<i>Modèle déchargé. Choisissez un modèle puis cliquez sur Charger.</i>")
self.set_ui_enabled(True)
def send_message(self):
user_message = self.input_field.text().strip()
loaded_is_vl = bool(self.loaded_model and self.loaded_model.kind == "vl")
if loaded_is_vl and not self.selected_image_path:
self.on_error("Veuillez sélectionner une image pour utiliser ce modèle Vision-Language.")
return
if not user_message and not self.selected_image_path:
return
if not self.loaded_model or not self.current_conversation_id:
self.on_error("Chargez d'abord un modèle avant d'envoyer un message.")
return
rag_context = ""
if self.rag_enabled and self.rag_index.ready and not loaded_is_vl and user_message:
try:
found = self.rag_index.search(user_message, k=3)
if found:
rag_context = f"\n\nContexte des documents:\n{found}"
self.chat_area.append(f"<i>Contexte RAG trouvé:\n{html.escape(rag_context)}</i>")
except Exception as exc:
self.on_error(f"Erreur de recherche RAG: {exc}")
display_message = user_message or "(image)"
self.append_message("user", display_message, save=True)
self.input_field.clear()
self.chat_area.append("<i>L'IA réfléchit...</i>")
self.set_ui_enabled(False)
conversation_history = list(self.conversations[self.current_conversation_id])
if rag_context:
conversation_history[-1] = dict(conversation_history[-1])
conversation_history[-1]["content"] = f"{rag_context}\n\nQuestion: {user_message}"
self.current_assistant_message = ""
self.stats_label.setText("")
self.generation_worker = GenerationWorker(
self.loaded_model,
conversation_history,
self.settings,
self.selected_image_path,
)
self.generation_worker.new_token.connect(self.on_new_token)
self.generation_worker.generation_complete.connect(self.on_generation_complete)
self.generation_worker.stats.connect(self.on_stats_update)
self.generation_worker.error.connect(self.on_error)
self.generation_worker.finished.connect(self.clear_image_after_generation)
self.generation_worker.start()
def clear_image_after_generation(self):
self.clear_image()
def on_new_token(self, token: str):
if not self.current_assistant_message:
cursor = self.chat_area.textCursor()
cursor.movePosition(cursor.MoveOperation.End)
cursor.select(cursor.SelectionType.LineUnderCursor)
cursor.removeSelectedText()
cursor.deletePreviousChar()
self.chat_area.setTextCursor(cursor)
self.append_message("assistant", token, save=False)
else:
cursor = self.chat_area.textCursor()
cursor.movePosition(cursor.MoveOperation.End)
cursor.insertText(token)
self.current_assistant_message += token
def on_stats_update(self, tokens_per_sec: float):
self.stats_label.setText(f"{tokens_per_sec:.2f} tokens/s")
def on_generation_complete(self, response: str):
if not self.current_conversation_id:
self.set_ui_enabled(True)
return
self.conversations[self.current_conversation_id].append(
{"role": "assistant", "content": response}
)
self.save_conversations()
self.display_current_conversation()
self.set_ui_enabled(True)
self.input_field.setFocus()
self.current_assistant_message = ""
def on_error(self, error_message: str):
self.chat_area.append(f"<font color='#ff6b6b'>Erreur : {html.escape(error_message)}</font>")
self.set_ui_enabled(True)
def set_ui_enabled(self, enabled: bool):
self.busy = not enabled
has_model = self.loaded_model is not None
self.input_field.setEnabled(enabled and has_model)
self.send_button.setEnabled(enabled and has_model)
self.model_selector.setEnabled(enabled)
self.load_button.setEnabled(enabled)
self.eject_button.setEnabled(enabled and has_model)
self.history_list.setEnabled(enabled)
self.load_docs_button.setEnabled(enabled)
self.rag_toggle_checkbox.setEnabled(enabled and self.rag_index.ready)
def toggle_rag(self, state):
self.rag_enabled = state == Qt.CheckState.Checked.value
if self.rag_enabled:
self.rag_status_label.setText("RAG: Actif")
self.chat_area.append(
"<i>RAG activé. Les documents chargés seront utilisés comme contexte (modèles texte uniquement).</i>"
)
else:
self.rag_status_label.setText("RAG: Inactif" if not self.rag_index.ready else "RAG: Prêt")
self.chat_area.append("<i>RAG désactivé.</i>")
def load_documents(self):
files, _ = QFileDialog.getOpenFileNames(
self, "Sélectionner des documents", "", "Documents (*.txt *.pdf *.docx)"
)
if not files:
return
self.set_ui_enabled(False)
self.rag_worker = RagWorker(
self.rag_index,
files,
int(self.settings.get("rag_chunk_size", 500)),
int(self.settings.get("rag_chunk_overlap", 50)),
)
self.rag_worker.status_update.connect(self.on_status_update)
self.rag_worker.index_ready.connect(self.on_rag_ready)
self.rag_worker.error.connect(self.on_rag_error)
self.rag_worker.start()
def on_rag_ready(self, chunk_count: int):
self.rag_status_label.setText("RAG: Prêt")
self.rag_toggle_checkbox.setEnabled(True)
self.chat_area.append(
f"<i>Index RAG créé ({chunk_count} morceaux). Vous pouvez maintenant activer le RAG.</i>"
)
self.set_ui_enabled(True)
def on_rag_error(self, message: str):
self.rag_status_label.setText("RAG: Erreur")
self.rag_toggle_checkbox.setChecked(False)
self.rag_enabled = False
self.on_error(message)
def start_new_conversation(self):
self.current_conversation_id = str(uuid.uuid4())
self.conversations[self.current_conversation_id] = [
{"role": "system", "content": self.settings["system_prompt"]}
]
item = QListWidgetItem(f"Nouvelle Discussion - {self.current_conversation_id[:8]}")
item.setData(Qt.ItemDataRole.UserRole, self.current_conversation_id)
self.history_list.insertItem(0, item)
self.history_list.setCurrentItem(item)
self.display_current_conversation()
def load_selected_conversation(self, item):
self.current_conversation_id = item.data(Qt.ItemDataRole.UserRole)
self.display_current_conversation()
def display_current_conversation(self):
self.chat_area.clear()
self.check_device()
if self.loaded_model:
self.chat_area.append(f"<i>Modèle {html.escape(self.loaded_model.name)} chargé.</i>")
else:
self.chat_area.append(
"<i>Aucun modèle chargé. Choisissez un modèle puis cliquez sur Charger.</i>"
)
if not self.current_conversation_id:
return
history = self.conversations.get(self.current_conversation_id, [])
for message in history:
self.append_message(message["role"], message.get("content", ""), save=False)
def _conversation_title(self, history, conv_id: str) -> str:
for message in history:
if message.get("role") == "user":
content = message.get("content", "")
if not isinstance(content, str):
return "Image + texte"
snippet = content[:30]
return snippet + ("..." if len(content) > 30 else "")
return f"Discussion {conv_id[:8]}"
def append_message(self, role, content, save=True):
if not self.current_conversation_id:
return
text = content if isinstance(content, str) else str(content)
if save:
self.conversations[self.current_conversation_id].append({"role": role, "content": text})
safe = html.escape(text)
if role == "user":
bubble = f"""
<div style='background-color: #3a3a3a; color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom: 5px;'>
<b>Vous:</b>
<p style='margin: 0;'>{safe}</p>
</div>
"""
elif role == "assistant":
formatted = markdown2.markdown(
text,
extras=["fenced-code-blocks", "tables"],
safe_mode="escape",
)
bubble = f"""
<div style='background-color: #3a3a3a; color: #f2f2f2; padding: 10px; border-radius: 5px; margin-bottom: 5px;'>
<b>LiquidAI:</b>
{formatted}
</div>
"""
else:
bubble = f"<i>{safe}</i>"
self.chat_area.append(bubble)
if save:
self.save_conversations()
def save_conversations(self):
os.makedirs("conversations", exist_ok=True)
if not self.current_conversation_id:
return
history = self.conversations.get(self.current_conversation_id)
if history is None:
return
path = f"conversations/{self.current_conversation_id}.json"
with open(path, "w", encoding="utf-8") as handle:
json.dump(history, handle, ensure_ascii=False, indent=2)
def load_conversations(self):
if not os.path.exists("conversations"):
return
for filename in os.listdir("conversations"):
if not filename.endswith(".json"):
continue
conv_id = filename.replace(".json", "")
path = os.path.join("conversations", filename)
try:
with open(path, "r", encoding="utf-8") as handle:
self.conversations[conv_id] = json.load(handle)
except (OSError, json.JSONDecodeError):
continue
title = self._conversation_title(self.conversations[conv_id], conv_id)
item = QListWidgetItem(title)
item.setData(Qt.ItemDataRole.UserRole, conv_id)
self.history_list.addItem(item)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = LiquidAIApp()
window.show()
sys.exit(app.exec())