-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCode
More file actions
1354 lines (1181 loc) · 47.9 KB
/
Code
File metadata and controls
1354 lines (1181 loc) · 47.9 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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Agente Autónomo by David Ruiz (@viajatech)
#Tus donaciones al proyecto son importantes para seguir creando A.I de código abierto, el tiempo creando estas herramientas conlleva días enteros en escribir el código al igual que el desgaste de componentes en mi hardware.
#Donaciones al proyecto,aqui; https://github.com/sponsors/viajatech
#Se agradece tu estrellita en el repo de github; https://github.com/viajatech/SuperAgente
#Este es un proyecto nacido de la imaginación. al estar escribiendo sobre un personaje de ciencia ficción para mi libro logre entender que queria llevar acabo dicha tecnología en la vida real, y desde entonces los scripts que hago van enfocados en hacer realidad mis sueños y darle vida a mis personajes de mis libros y novelas de ciencia ficción.
#Abre correctamente; Chrome, Firefox, consultas de video a Vimeo,YouTube e incluso abre Spotify.
#Abre programas; Word,bloc de notas,excel y escribe en ellos desde el GUI, luego se autoguarda en dichos programas una vez ha escrito.
# =============================================================================
# Super Agente by Viaja Tech
# - Word, Excel (con reconexión + autoguardado), Notepad (autoguardado)
# - Chrome, Firefox con secciones (Imágenes, Videos, Shopping, Noticias,
# Maps, Libros, Web/Todo)
# - Corrección “videos en youtube/vimeo de X” => “X”
# - Placeholders Redes Sociales
# - Test-time Compute (Simple, Best-of-N, Weighted, Beam, DVTS)
# - Arreglo: en maps, quitamos "en " si detecta "en maps" => "maps"
# =============================================================================
import os
import sys
import time
import datetime
import threading
import subprocess
import tempfile
import logging
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import psutil
import pyperclip
import pythoncom
import win32com.client as win32
# Selenium
import urllib.parse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
import pywintypes
# STT / TTS (opcional)
import speech_recognition as sr
import azure.cognitiveservices.speech as speechsdk
# Gradio
import gradio as gr
# openai -> LM Studio
from openai import OpenAI
###############################################################################
# LOGGING
###############################################################################
logging.basicConfig(
filename='app.log',
level=logging.WARNING,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
###############################################################################
# TKINTER LAUNCHER
###############################################################################
class MultiAppLauncher:
def __init__(self, root):
self.root = root
self.root.title("Super Agente by Viaja Tech")
self.root.geometry("1200x800")
self.root.resizable(False, False)
# Word
self.word_doc = None
# Excel
self.excel_app = None
self.excel_sheet = None
# Notepad
self.notepad_temp_file = None
self.notepad_live_sync = False
# Browsers
self.chrome_driver = None
self.firefox_driver = None
# Social placeholders
self.social_drivers = {
'x': None,
'facebook': None,
'instagram': None,
'threads': None,
'tiktok': None,
'youtube': None,
'github': None,
'vimeo': None
}
self.notebook = ttk.Notebook(self.root)
self.notebook.pack(pady=10, expand=True)
self.text_areas = {}
self.last_update = 0
self.update_delay = 500
if os.name != 'nt':
messagebox.showerror("SO no compatible", "Este script sólo funciona en Windows.")
self.root.destroy()
sys.exit()
self.create_all_tabs()
close_btn = tk.Button(
self.root,
text="Cerrar Aplicación",
command=self.cerrar_aplicacion,
bg="#FF0000",
fg="white",
font=("Arial", 12),
padx=20,
pady=10
)
close_btn.pack(pady=5)
def create_all_tabs(self):
self.create_notepad_tab()
self.create_word_tab()
self.create_excel_tab()
self.create_firefox_tab()
self.create_chrome_tab()
# ------------------ NOTEPAD ------------------
def create_notepad_tab(self):
tab = ttk.Frame(self.notebook)
self.notebook.add(tab, text="Bloc de Notas / Notepad")
lbl = tk.Label(tab, text="Texto para Notepad:", font=("Arial", 12))
lbl.pack(pady=5)
frame_txt = tk.Frame(tab)
frame_txt.pack(pady=5, padx=10, fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(frame_txt)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_area = tk.Text(
frame_txt,
wrap=tk.WORD,
width=80,
height=20,
font=("Arial", 12),
yscrollcommand=scrollbar.set
)
text_area.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=text_area.yview)
self.text_areas['notepad'] = text_area
text_area.bind('<KeyRelease>', lambda e: self.on_text_change(text_area))
btn_notepad_once = tk.Button(
tab,
text="Abrir Notepad (una vez)",
command=lambda: self.enviar_a_notepad_una_vez(text_area),
bg="#555555",
fg="white",
font=("Arial", 12),
padx=20,
pady=10
)
btn_notepad_once.pack(pady=5)
def enviar_a_notepad_una_vez(self, text_widget):
texto = text_widget.get("1.0", tk.END).strip()
if not texto:
messagebox.showwarning("Aviso", "Ingresa texto antes de abrir Notepad.")
return
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode='w', encoding='utf-8') as tmp:
tmp.write(texto)
self.notepad_temp_file = tmp.name
subprocess.Popen(['notepad.exe', self.notepad_temp_file])
self.notepad_live_sync = False
self._auto_save_notepad_immediate()
except Exception as e:
logging.warning(f"Error Notepad: {e}")
def _auto_save_notepad_immediate(self):
if not self.notepad_temp_file:
return
ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
default_path = os.path.join(os.getcwd(), f"auto_saved_notepad_{ts}.txt")
try:
with open(self.notepad_temp_file, 'r', encoding='utf-8') as src:
content = src.read()
with open(default_path, 'w', encoding='utf-8') as dst:
dst.write(content)
logging.warning(f"[Auto-Saved Notepad] => {default_path}")
except Exception as e:
logging.warning(f"Error auto-saving Notepad: {e}")
def cerrar_notepad_process(self):
try:
for proc in psutil.process_iter(['pid', 'name']):
if proc.info['name'] and proc.info['name'].lower() == "notepad.exe":
proc.kill()
except Exception as e:
logging.warning(f"Error cerrando Notepad: {e}")
def update_notepad_live(self, new_text):
if not self.notepad_temp_file:
return
self.cerrar_notepad_process()
time.sleep(0.5)
try:
with open(self.notepad_temp_file, 'w', encoding='utf-8') as f:
f.write(new_text)
subprocess.Popen(['notepad.exe', self.notepad_temp_file])
except Exception as e:
logging.warning(f"Error update_notepad_live: {e}")
def auto_save_notepad(self):
if not self.notepad_temp_file:
messagebox.showwarning("No se puede guardar", "No hay Notepad abierto.")
return
path = filedialog.asksaveasfilename(
title="Guardar Notepad",
defaultextension=".txt",
filetypes=[("Archivo de texto", "*.txt"), ("Todos los archivos", "*.*")]
)
if path:
try:
with open(self.notepad_temp_file, 'r', encoding='utf-8') as src:
content = src.read()
with open(path, 'w', encoding='utf-8') as dst:
dst.write(content)
messagebox.showinfo("Guardado", f"Notepad guardado en:\n{path}")
except Exception as e:
messagebox.showerror("Error", f"No se pudo guardar Notepad:\n{e}")
# ------------------ WORD ------------------
def create_word_tab(self):
tab = ttk.Frame(self.notebook)
self.notebook.add(tab, text="Microsoft Word")
lbl = tk.Label(tab, text="Texto para Word:", font=("Arial", 12))
lbl.pack(pady=5)
frame_txt = tk.Frame(tab)
frame_txt.pack(pady=5, padx=10, fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(frame_txt)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_area = tk.Text(
frame_txt,
wrap=tk.WORD,
width=80,
height=20,
font=("Arial", 12),
yscrollcommand=scrollbar.set
)
text_area.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=text_area.yview)
self.text_areas['word'] = text_area
text_area.bind('<KeyRelease>', lambda e: self.on_text_change(text_area))
btn_word = tk.Button(
tab,
text="Abrir Word",
command=lambda: self.abrir_word(text_area),
bg="#4CAF50",
fg="white",
font=("Arial", 12),
padx=20,
pady=10
)
btn_word.pack(pady=5)
def abrir_word(self, text_widget):
texto = text_widget.get("1.0", tk.END).strip()
try:
pythoncom.CoInitialize()
if not self.word_doc:
word = win32.gencache.EnsureDispatch('Word.Application')
word.Visible = True
self.word_doc = word.Documents.Add()
self.word_doc.Content.Text = texto
self._auto_save_word_immediate()
except Exception as e:
logging.warning(f"Error abrir_word: {e}")
finally:
pythoncom.CoUninitialize()
def _auto_save_word_immediate(self):
try:
if self.word_doc:
ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
default_path = os.path.join(os.getcwd(), f"auto_saved_word_{ts}.docx")
self.word_doc.SaveAs2(default_path)
logging.warning(f"[Auto-Saved Word] => {default_path}")
except Exception as e:
logging.warning(f"Error auto_save_word_immediate: {e}")
def update_word(self, texto):
try:
pythoncom.CoInitialize()
if self.word_doc:
self.word_doc.Content.Text = texto
except Exception as e:
logging.warning(f"Error update_word: {e}")
finally:
pythoncom.CoUninitialize()
def abrir_word_con_historia(self, texto):
try:
pythoncom.CoInitialize()
if not self.word_doc:
word = win32.gencache.EnsureDispatch('Word.Application')
word.Visible = True
self.word_doc = word.Documents.Add()
self.word_doc.Content.Text = texto
self._auto_save_word_immediate()
except Exception as e:
logging.warning(f"Error abrir_word_con_historia: {e}")
finally:
pythoncom.CoUninitialize()
def auto_save_word(self):
try:
pythoncom.CoInitialize()
if not self.word_doc:
messagebox.showwarning("Word no abierto", "No hay documento Word para guardar.")
return
path = filedialog.asksaveasfilename(
title="Guardar Word",
defaultextension=".docx",
filetypes=[("Documento Word", "*.docx"), ("Todos los archivos", "*.*")]
)
if path:
self.word_doc.SaveAs2(path)
messagebox.showinfo("Guardado", f"Documento Word guardado:\n{path}")
except Exception as e:
messagebox.showerror("Error", f"No se pudo guardar Word:\n{e}")
finally:
pythoncom.CoUninitialize()
# ------------------ EXCEL ------------------
def create_excel_tab(self):
tab = ttk.Frame(self.notebook)
self.notebook.add(tab, text="Microsoft Excel")
lbl = tk.Label(tab, text="Texto para Excel (una línea por celda en Columna A):", font=("Arial", 12))
lbl.pack(pady=5)
frame_txt = tk.Frame(tab)
frame_txt.pack(pady=5, padx=10, fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(frame_txt)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_area = tk.Text(
frame_txt,
wrap=tk.WORD,
width=70,
height=12,
font=("Arial", 12),
yscrollcommand=scrollbar.set
)
text_area.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=text_area.yview)
self.text_areas['excel'] = text_area
text_area.bind('<KeyRelease>', lambda e: self.on_text_change(text_area))
btn_excel = tk.Button(
tab,
text="Abrir Excel",
command=lambda: self.abrir_excel(text_area),
bg="#FF9800",
fg="white",
font=("Arial", 12),
padx=20,
pady=10
)
btn_excel.pack(pady=5)
# Frame para fórmula
formula_frame = tk.LabelFrame(tab, text="Aplicar Fórmula en Excel")
formula_frame.pack(pady=5, padx=10, fill=tk.X)
lbl_cell = tk.Label(formula_frame, text="Celda (Ej: A1):", font=("Arial", 10))
lbl_cell.grid(row=0, column=0, padx=5, pady=5, sticky="e")
self.excel_cell_entry = tk.Entry(formula_frame, width=10)
self.excel_cell_entry.grid(row=0, column=1, padx=5, pady=5)
lbl_formula = tk.Label(formula_frame, text="Fórmula (Ej: =1+1):", font=("Arial", 10))
lbl_formula.grid(row=0, column=2, padx=5, pady=5, sticky="e")
self.excel_formula_entry = tk.Entry(formula_frame, width=20)
self.excel_formula_entry.grid(row=0, column=3, padx=5, pady=5)
btn_set_formula = tk.Button(
formula_frame,
text="Aplicar Fórmula",
command=self.aplicar_formula_excel,
bg="#3F51B5",
fg="white",
font=("Arial", 10),
padx=10,
pady=5
)
btn_set_formula.grid(row=0, column=4, padx=5, pady=5)
def abrir_excel(self, text_widget):
texto = text_widget.get("1.0", tk.END)
self.write_in_excel(texto)
def write_in_excel(self, texto):
lines = texto.split('\n')
for intento in range(2):
try:
pythoncom.CoInitialize()
if not self.excel_app or not self.excel_sheet:
self.excel_app = win32.gencache.EnsureDispatch('Excel.Application')
self.excel_app.Visible = True
wb = self.excel_app.Workbooks.Add()
self.excel_sheet = wb.Worksheets(1)
row = 1
for line in lines:
self.excel_sheet.Cells(row, 1).Value = line
row += 1
self._auto_save_excel_immediate()
return
except pywintypes.com_error as e:
logging.warning(f"Error Excel: {e}")
if self.excel_app:
try:
self.excel_app.Quit()
except:
pass
self.excel_app = None
self.excel_sheet = None
if intento == 1:
raise e
finally:
pythoncom.CoUninitialize()
def _auto_save_excel_immediate(self):
try:
pythoncom.CoInitialize()
if self.excel_app and self.excel_sheet:
ts = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
default_path = os.path.join(os.getcwd(), f"auto_saved_excel_{ts}.xlsx")
wb = self.excel_app.ActiveWorkbook
wb.SaveAs(default_path)
logging.warning(f"[Auto-Saved Excel] => {default_path}")
except Exception as e:
logging.warning(f"Error auto_save_excel_immediate: {e}")
finally:
pythoncom.CoUninitialize()
def auto_save_excel(self):
try:
pythoncom.CoInitialize()
if not self.excel_app or not self.excel_sheet:
messagebox.showwarning("Excel no abierto", "No hay libro Excel para guardar.")
return
path = filedialog.asksaveasfilename(
title="Guardar Excel",
defaultextension=".xlsx",
filetypes=[("Libro Excel", "*.xlsx"), ("Todos los archivos", "*.*")]
)
if path:
wb = self.excel_app.ActiveWorkbook
wb.SaveAs(path)
messagebox.showinfo("Guardado", f"Libro Excel guardado:\n{path}")
except Exception as e:
messagebox.showerror("Error", f"No se pudo guardar Excel:\n{e}")
finally:
pythoncom.CoUninitialize()
def aplicar_formula_excel(self):
if not self.excel_app or not self.excel_sheet:
messagebox.showwarning("Excel no abierto", "Primero abre Excel antes de aplicar fórmula.")
return
cell_str = self.excel_cell_entry.get().strip()
formula_str = self.excel_formula_entry.get().strip()
if not cell_str or not formula_str:
messagebox.showwarning("Campos vacíos", "Indica celda y fórmula.")
return
try:
pythoncom.CoInitialize()
wb = self.excel_app.ActiveWorkbook
sht = wb.ActiveSheet
sht.Range(cell_str).Formula = formula_str
messagebox.showinfo("Fórmula aplicada", f"Celda {cell_str} = {formula_str}")
except Exception as e:
messagebox.showerror("Error", f"No se pudo aplicar la fórmula:\n{e}")
finally:
pythoncom.CoUninitialize()
# ------------------- BROWSERS (CHROME / FIREFOX) -------------------
def create_firefox_tab(self):
tab = ttk.Frame(self.notebook)
self.notebook.add(tab, text="Buscar en Firefox")
lbl = tk.Label(tab, text="Texto para Firefox (búsqueda):", font=("Arial", 12))
lbl.pack(pady=5)
frame_txt = tk.Frame(tab)
frame_txt.pack(pady=5, padx=10, fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(frame_txt)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_area = tk.Text(
frame_txt, wrap=tk.WORD, width=80, height=10,
font=("Arial", 12),
yscrollcommand=scrollbar.set
)
text_area.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=text_area.yview)
self.text_areas['firefox'] = text_area
text_area.bind('<KeyRelease>', lambda e: self.on_text_change(text_area))
btn_ff = tk.Button(
tab,
text="Abrir/Búsqueda en Firefox",
command=lambda: self.abrir_firefox(text_area),
bg="#FF7139",
fg="white",
font=("Arial", 12),
padx=20,
pady=10
)
btn_ff.pack(pady=5)
def create_chrome_tab(self):
tab = ttk.Frame(self.notebook)
self.notebook.add(tab, text="Buscar en Chrome")
lbl = tk.Label(tab, text="Texto para Chrome (búsqueda):", font=("Arial", 12))
lbl.pack(pady=5)
frame_txt = tk.Frame(tab)
frame_txt.pack(pady=5, padx=10, fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(frame_txt)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_area = tk.Text(
frame_txt, wrap=tk.WORD, width=80, height=10,
font=("Arial", 12),
yscrollcommand=scrollbar.set
)
text_area.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=text_area.yview)
self.text_areas['chrome'] = text_area
text_area.bind('<KeyRelease>', lambda e: self.on_text_change(text_area))
btn_chrome = tk.Button(
tab,
text="Abrir/Búsqueda en Chrome",
command=lambda: self.abrir_chrome(text_area),
bg="#4285F4",
fg="white",
font=("Arial", 12),
padx=20,
pady=10
)
btn_chrome.pack(pady=5)
def abrir_firefox(self, text_widget):
texto = text_widget.get("1.0", tk.END).strip()
try:
if not self.firefox_driver:
service = FirefoxService(executable_path=GeckoDriverManager().install())
options = webdriver.FirefoxOptions()
options.add_argument("--start-maximized")
self.firefox_driver = webdriver.Firefox(service=service, options=options)
self.update_browsers('firefox', texto, "Todo")
except Exception as e:
logging.warning(f"Error abrir_firefox: {e}")
def abrir_chrome(self, text_widget):
texto = text_widget.get("1.0", tk.END).strip()
try:
if not self.chrome_driver:
service = ChromeService(executable_path=ChromeDriverManager().install())
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
self.chrome_driver = webdriver.Chrome(service=service, options=options)
threading.Thread(
target=self.update_browsers,
args=('chrome', texto, "Todo"),
daemon=True
).start()
except Exception as e:
logging.warning(f"Error abrir_chrome: {e}")
def abrir_chrome_fake(self, raw_search):
try:
if not self.chrome_driver:
service = ChromeService(executable_path=ChromeDriverManager().install())
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
self.chrome_driver = webdriver.Chrome(service=service, options=options)
self.update_browsers('chrome', raw_search, "Todo")
except Exception as e:
logging.warning(f"Error abrir_chrome_fake: {e}")
def abrir_firefox_fake(self, raw_search):
try:
if not self.firefox_driver:
service = FirefoxService(executable_path=GeckoDriverManager().install())
options = webdriver.FirefoxOptions()
options.add_argument("--start-maximized")
self.firefox_driver = webdriver.Firefox(service=service, options=options)
self.update_browsers('firefox', raw_search, "Todo")
except Exception as e:
logging.warning(f"Error abrir_firefox_fake: {e}")
def update_browsers(self, browser_key, texto, default_section):
"""
Corrige "en hoteles para perritos" => "hoteles para perritos"
Mismo con "en maps", etc.
"""
ltxt = texto.lower()
# 1) Revisar si "maps"
# Si el usuario dice "en maps", lo removemos
# por ejemplo: "busca en maps hoteles para perritos" => subject = "hoteles para perritos"
if "map" in ltxt:
# remover "en maps" o "maps" en la parte sobrante
# para no generarle "en++"
parted = ltxt.split("maps", maxsplit=1)
# parted[-1], remove leading "en "
subject = parted[-1].strip()
if subject.startswith("en "):
subject = subject[3:].strip()
# apply normal removal logic
# (handled below with remove_phrases if needed)
# then do google maps search
# we return after building the url
# but let's keep the standard logic for partial synergy
# so we reassign ltxt to some placeholder
ltxt = "maps " + subject
# 2) Revisar "spotify"
if "spotify" in ltxt:
# placeholder => open "https://open.spotify.com/"
try:
url = "https://open.spotify.com/"
if browser_key == 'firefox' and self.firefox_driver:
self.firefox_driver.get(url)
elif browser_key == 'chrome' and self.chrome_driver:
self.chrome_driver.get(url)
return
except Exception as e:
logging.warning(f"Error abrir Spotify: {e}")
return
# 3) videos en youtube
if "video" in ltxt and "youtube" in ltxt:
parted = ltxt.split("youtube", maxsplit=1)
subject = parted[-1].strip()
if subject.startswith("de "):
subject = subject[3:].strip()
for rp in ["videos en", "videos", "video en", "video", "de"]:
idx = subject.find(rp)
if idx != -1:
subject = subject[:idx] + subject[idx+len(rp):]
subject = subject.strip()
if not subject:
subject = "videos"
q = urllib.parse.quote_plus(subject)
url = f"https://www.youtube.com/results?search_query={q}"
try:
if browser_key == 'firefox' and self.firefox_driver:
self.firefox_driver.get(url)
elif browser_key == 'chrome' and self.chrome_driver:
self.chrome_driver.get(url)
except Exception as e:
logging.warning(f"Error youtube: {e}")
return
# 4) videos en vimeo
if "video" in ltxt and "vimeo" in ltxt:
parted = ltxt.split("vimeo", maxsplit=1)
subject = parted[-1].strip()
if subject.startswith("de "):
subject = subject[3:].strip()
for rp in ["videos en", "videos", "video en", "video", "de"]:
idx = subject.find(rp)
if idx != -1:
subject = subject[:idx] + subject[idx+len(rp):]
subject = subject.strip()
if not subject:
subject = "videos"
q = urllib.parse.quote_plus(subject)
url = f"https://vimeo.com/search?q={q}"
try:
if browser_key == 'firefox' and self.firefox_driver:
self.firefox_driver.get(url)
elif browser_key == 'chrome' and self.chrome_driver:
self.chrome_driver.get(url)
except Exception as e:
logging.warning(f"Error vimeo: {e}")
return
# 5) Secciones normales
sec = default_section
if "imagen" in ltxt or "foto" in ltxt:
sec = "Imágenes"
elif "video" in ltxt:
sec = "Videos"
elif "map" in ltxt:
sec = "Maps"
elif "noticia" in ltxt:
sec = "Noticias"
elif "shopping" in ltxt or "comprar" in ltxt:
sec = "Shopping"
elif "libro" in ltxt:
sec = "Libros"
remove_phrases = [
"fotos de", "fotos", "imagenes de", "imágenes de", "imágenes",
"videos de", "videos", "video de", "video", "videos en", "video en",
"noticias de", "noticia de", "noticias", "noticia",
"maps de", "map de", "maps", "map",
"shopping de", "shopping", "comprar",
"libros de", "libro de", "libros", "libro"
]
subject = texto
for rp in remove_phrases:
idx = subject.lower().find(rp)
if idx != -1:
subject = subject[:idx] + subject[idx+len(rp):]
subject = subject.strip()
if subject.startswith("en "):
subject = subject[3:].strip()
if not subject:
subject = texto
section_map = {
"Todo": "",
"Imágenes": "isch",
"Videos": "vid",
"Maps": "maps",
"Noticias": "nws",
"Shopping": "shop",
"Libros": "bks"
}
query = urllib.parse.quote_plus(subject)
if sec == "Maps":
url = f"https://www.google.com/maps/search/?api=1&query={query}"
else:
tbm = section_map.get(sec, "")
if tbm:
url = f"https://www.google.com/search?q={query}&tbm={tbm}"
else:
url = f"https://www.google.com/search?q={query}"
try:
if browser_key == 'firefox' and self.firefox_driver:
self.firefox_driver.get(url)
elif browser_key == 'chrome' and self.chrome_driver:
self.chrome_driver.get(url)
except Exception as e:
logging.warning(f"Error update_browsers: {e}")
# -------------- Placeholders Redes Sociales --------------
def login_social_media(self, platform, user, password):
pass
def post_social_media(self, platform, text, media_path=None):
pass
# -------------- TEXT CHANGE / CERRAR ---------------------
def on_text_change(self, text_widget):
cur_time = time.time() * 1000
if cur_time - self.last_update >= self.update_delay:
self.last_update = cur_time
texto = text_widget.get("1.0", tk.END)
for k, area in self.text_areas.items():
if text_widget == area:
if k == 'word' and self.word_doc:
self.update_word(texto)
elif k == 'excel' and self.excel_sheet:
self.write_in_excel(texto)
elif k == 'notepad' and self.notepad_live_sync:
threading.Thread(
target=self.update_notepad_live,
args=(texto,),
daemon=True
).start()
elif k == 'firefox' and self.firefox_driver:
threading.Thread(
target=self.update_browsers,
args=('firefox', texto, "Todo"),
daemon=True
).start()
elif k == 'chrome' and self.chrome_driver:
threading.Thread(
target=self.update_browsers,
args=('chrome', texto, "Todo"),
daemon=True
).start()
def cerrar_aplicacion(self):
try:
pythoncom.CoInitialize()
if self.word_doc:
self.word_doc.Application.Quit()
self.word_doc = None
if self.excel_app:
self.excel_app.Quit()
self.excel_app = None
self.excel_sheet = None
except:
pass
finally:
pythoncom.CoUninitialize()
if self.chrome_driver:
self.chrome_driver.quit()
self.chrome_driver = None
if self.firefox_driver:
self.firefox_driver.quit()
self.firefox_driver = None
self.cerrar_notepad_process()
self.root.destroy()
sys.exit()
def main(self):
self.root.mainloop()
###############################################################################
# LM STUDIO + TEST TIME
###############################################################################
client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
def speak_text_azure(text, voice_gender):
pass
def transcribe_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
return r.recognize_google(audio, language='es-ES')
except:
return "No pude entender el audio."
def build_conversation_history(messages):
hist = []
for m in messages:
if m["role"] == "user":
hist.append(f"Usuario: {m['content']}")
elif m["role"] == "assistant":
hist.append(f"Asistente: {m['content']}")
return "\n".join(hist)
def dummy_reward_function(text):
return len(text)
def strategy_simple(context, user_message, depth, model, temperature, full_messages=None):
if full_messages is not None:
c_hist = build_conversation_history(full_messages)
context = f"{context}\n\nHistorial:\n{c_hist}"
prompt = f"""
Eres un asistente.
Usuario: "{user_message}"
Genera un razonamiento interno con {depth} pasos.
Luego di "Respuesta Final:" con tu respuesta final.
"""
msgs_for_api = [
{"role": "system", "content": context},
{"role": "system", "content": prompt},
{"role": "user", "content": user_message}
]
try:
completion = client.chat.completions.create(
model=model,
messages=msgs_for_api,
temperature=temperature
)
return completion.choices[0].message.content.strip()
except Exception as e:
return f"Error strategy_simple: Error code: 400 - {str(e)}"
def strategy_best_of_n(context, user_message, n, model, temperature, full_messages=None):
return "Not Implemented"
def strategy_weighted_best_of_n(context, user_message, n, model, temperature, full_messages=None):
return "Not Implemented"
def strategy_beam_search(context, user_message, beam_iterations, model, temperature, full_messages=None):
return "Not Implemented"
def strategy_dvts(context, user_message, total_subtrees, model, temperature, full_messages=None):
return "DVTS placeholder"
###############################################################################
# PARSEO DE COMANDOS
###############################################################################
launcher = None
def get_system_time():
now = datetime.datetime.now()
return now.strftime("%Y-%m-%d %H:%M:%S")
def parse_and_execute_command(
user_text,
context,
messages,
strategy,
test_time_compute,
model,
temperature
):
lt = user_text.lower()
# Manejo de "abre spotify" (chrome/firefox) => open "https://open.spotify.com/"
if "abre spotify" in lt:
if "chrome" in lt:
if not launcher.chrome_driver:
launcher.abrir_chrome_fake("spotify")
return True, "Abriendo Chrome y Spotify"
else:
launcher.update_browsers('chrome', "spotify", "Todo")
return True, "Spotify en Chrome"
elif "firefox" in lt:
if not launcher.firefox_driver:
launcher.abrir_firefox_fake("spotify")
return True, "Abriendo Firefox y Spotify"
else:
launcher.update_browsers('firefox', "spotify", "Todo")
return True, "Spotify en Firefox"
else:
# default
if not launcher.chrome_driver:
launcher.abrir_chrome_fake("spotify")
return True, "Abriendo Chrome y Spotify"
else:
launcher.update_browsers('chrome', "spotify", "Todo")
return True, "Spotify en Chrome"
if "abre la red social" in lt and ("usuario" in lt or "contraseña" in lt):
return True, "Placeholder: abriendo red social"
# Hora/fecha
if "qué hora" in lt or "que hora" in lt:
return True, f"La hora local es: {get_system_time()}"
if "qué fecha" in lt or "que fecha" in lt:
return True, f"La fecha/hora local es: {get_system_time()}"
# Notepad synonyms
synonyms_notepad = ["notepad", "bloc de notas", "block de notas"]
if ("abre" in lt and any(syn in lt for syn in synonyms_notepad) and "escribe" in lt):
ans = handle_app_llm(user_text, context, messages, strategy, test_time_compute, model, temperature, "notepad")
return True, ans
# Word
if "abre" in lt and "word" in lt and "escribe" in lt:
ans = handle_app_llm(user_text, context, messages, strategy, test_time_compute, model, temperature, "word")
return True, ans
# Excel
if "abre" in lt and "excel" in lt and "escribe" in lt:
ans = handle_app_llm(user_text, context, messages, strategy, test_time_compute, model, temperature, "excel")
return True, ans
# busqueda en chrome/firefox
if "busca" in lt:
browser = None