-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (56 loc) · 1.93 KB
/
main.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
import tkinter as tk
from RegexGenerator import find_and_kill_process_by_port
from PDFBuilder import PDFBuilder
def main(llamafile_exists):
root = tk.Tk()
app = PDFBuilder(root, llamafile_exists)
root.mainloop()
if __name__ == "__main__":
import subprocess
import signal
import sys
import os
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and for PyInstaller"""
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except AttributeError:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
llamafile_path = resource_path(
r"llamafiles\Phi-3-mini-4k-instruct.Q4_1.llamafile.exe"
)
llamafile_exists = os.path.exists(llamafile_path)
def signal_handler(sig, frame):
print("Ctrl-C pressed, stopping the subprocess...")
proc.terminate() # or proc.kill() if terminate doesn't work
sys.exit(0)
# Setup signal handler to handle Ctrl-C
signal.signal(signal.SIGINT, signal_handler)
# Start the executable as a subprocess
if llamafile_exists:
port = "3456"
find_and_kill_process_by_port(port)
proc = subprocess.Popen(
[
llamafile_path,
"--nobrowser",
"--port",
port,
],
shell=True,
)
try:
# Keep the main program running; you can do your other tasks in this block
print("Running, press Ctrl-C to stop.")
main(llamafile_exists)
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Ensure the subprocess is terminated when the Python script ends
if llamafile_exists:
proc.kill()
proc.wait()
find_and_kill_process_by_port(port)
print("Subprocess terminated.")