-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOllamaAssistant.py
executable file
·184 lines (144 loc) · 5.33 KB
/
OllamaAssistant.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
#!/usr/bin/python3
import tkinter as tk
import requests
import json
from tkinter import scrolledtext
from tkinter.ttk import Progressbar
import subprocess
import os
import datetime as dt
def kill_previous_and_run():
# Ottieni il PID dell'istanza corrente
current_pid = os.getpid()
# Cerca processi in esecuzione con il nome 'GnomeOllama.py'
result = subprocess.run(
["pgrep", "-f", "OllamaAssistant.py"], stdout=subprocess.PIPE
)
pids = result.stdout.decode().split()
# Se esistono tali processi, uccidi quelli con PID inferiore a quello corrente
for pid in pids:
if int(pid) < current_pid:
subprocess.run(["kill", "-9", pid])
# Esegui il tuo script
subprocess.run(["python3", "OllamaAssistant.py"])
# Utilizza la funzione
kill_previous_and_run()
headers = {"Content-Type": "application/json"}
def send_to_LLM(prompt, from_clipboard=False):
# Ottieni il testo copiato
if from_clipboard:
copied_text = root.clipboard_get()
else:
copied_text = ""
data = {"model": "gemma2:2b", "prompt": prompt.replace("--context--", copied_text)}
print(data)
# Inserisci il prompt nella zona di testo
text_area.insert(
tk.END, "-------" + dt.datetime.now().strftime("%H:%M:%S") + "-------\n"
)
text_area.insert(tk.END, prompt + "\n")
# p.stop()
for i in range(1):
p.step()
root.update()
response = requests.post(
"http://localhost:11434/api/generate",
headers=headers,
data=json.dumps(data),
)
for line in response.iter_lines():
if line:
json_line = json.loads(line)
text_area.insert(tk.END, json_line["response"])
text_area.insert(tk.END, "\n\n")
p.step()
root.update()
def send_text(option, RAG=False):
p.pack()
# Invia il testo all'API selezionando l'opzione corrispondente
if option == "Riassumi il testo":
send_to_LLM("Respond using the same language of the text. Sum up the text: \n --context--", from_clipboard=True)
# Codice per inviare il testo all'API per il riassunto
pass
elif option == "Spiega":
# Codice per inviare il testo all'API per la spiegazione
send_to_LLM("Respond using the same language of the text. Explain this text: --context--", from_clipboard=True)
pass
elif option == "Rifromula":
# Codice per inviare il testo all'API per la rifromulazione
send_to_LLM("Respond using the same language of the text. Rephrase this text: --context--", from_clipboard=True)
pass
elif option == "Controlla la grammatica":
# Codice per inviare il testo all'API per la rifromulazione
send_to_LLM(
"Respond using the same language of the text. Check the spelling: --context--", from_clipboard=True
)
pass
elif RAG == True:
# Codice per inviare il testo all'API per la rifromulazione
send_to_LLM(
"""Respond using the same language of the text.
Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Use three sentences maximum and keep the answer as concise as possible.
--context--
Question: {}
Helpful Answer:""".format(option) , from_clipboard=True
)
pass
else:
# Codice per inviare il testo all'API per la generazione di testo
send_to_LLM(option)
pass
def clear_text():
text_area.delete("1.0", tk.END)
# Crea la finestra
root = tk.Tk()
root.title("LLM Desktop Integration")
# Crea i pulsanti
p = Progressbar(root, length=200, mode="determinate", takefocus=True, maximum=2)
# Crea un frame per i pulsanti
button_frame = tk.Frame(root)
button_frame.pack()
button1 = tk.Button(
button_frame,
text="Riassumi il testo",
command=lambda: send_text("Riassumi il testo"),
)
button1.pack(side=tk.LEFT)
button2 = tk.Button(button_frame, text="Spiega", command=lambda: send_text("Spiega"))
button2.pack(side=tk.LEFT)
button3 = tk.Button(
button_frame, text="Rifromula", command=lambda: send_text("Rifromula")
)
button3.pack(side=tk.LEFT)
button4 = tk.Button(
button_frame, text="Controlla", command=lambda: send_text("Controlla la grammatica")
)
button4.pack(side=tk.LEFT)
# Crea il pulsante per pulire il testo
clear_button = tk.Button(button_frame, text="Pulisci", command=clear_text)
clear_button.pack(side=tk.LEFT)
# Crea la zona di testo
text_area = scrolledtext.ScrolledText(root, wrap=tk.WORD)
text_area.pack(fill=tk.BOTH, expand=True)
# Crea un frame per l'input e il pulsante di invio
input_frame = tk.Frame(root)
input_frame.pack(fill=tk.X)
# Crea la casella di testo in input
input_text = tk.Entry(input_frame)
input_text.pack(side=tk.LEFT, fill=tk.X, expand=True)
# Crea il pulsante di invio
send_button = tk.Button(
input_frame, text="Invia", command=lambda: send_text(input_text.get())
)
send_button.pack(side=tk.RIGHT)
# Crea il pulsante di invio
send_button = tk.Button(
input_frame, text="RAG", command=lambda: send_text(input_text.get(), RAG=True)
)
send_button.pack(side=tk.RIGHT)
# Associa l'evento di pressione del tasto Invio alla funzione send_text
input_text.bind("<Return>", lambda event: send_text(input_text.get()))
# Avvia la finestra
root.mainloop()