-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_models.py
60 lines (41 loc) · 1.5 KB
/
app_models.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
# This code is for checking and stopping the running models:
import os
import subprocess
def check_running_models(temp_folder: str):
"""Check the running models and save the output to a filename.
Args:
temp_folder (str): Temporary folder to store the temp text file
Returns:
str: Filename of the text file
"""
filename = f"{temp_folder}/tmp_running_models.txt"
# Clear the file if it already exists
if os.path.exists(filename):
os.remove(filename)
# Run the command and save the output to the file
subprocess.run(["ollama", "ps"], stdout=open(filename, "w"))
return filename
def get_running_models(temp_folder: str):
"""Get the running models from the file as a list.
Args:
temp_folder (str): Temporary folder to store the temp text file
Returns:
list: List of running models
"""
filename = check_running_models(temp_folder=temp_folder)
running_models = []
ind = 0
with open(filename, "r") as file:
for line_no, line_content in enumerate(file.readlines()):
if line_no == 0:
ind = line_content.find("ID")
else:
running_models.append(line_content[:ind].strip())
return running_models
def stop_running_models(running_models: list):
"""Stop the running models.
Args:
running_models (list): List of running models
"""
for model in running_models:
subprocess.run(["ollama", "stop", model])