-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
173 lines (135 loc) · 4.77 KB
/
__init__.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
import os,sys
import platform
import subprocess
from server import PromptServer
import folder_paths
import requests
import psutil
python = sys.executable
global process
process=None
# from .search.Search import Bing
# from server import PromptServer
try:
import aiohttp
from aiohttp import web,ClientSession
except ImportError:
print("Module 'aiohttp' not installed. Please install it via:")
print("pip install aiohttp")
print("or")
print("pip install -r requirements.txt")
sys.exit()
# 优先使用phi
def phi_sort(lst):
return sorted(lst, key=lambda x: x.lower().count('phi'), reverse=True)
model_path=os.path.join(folder_paths.models_dir, "llamafile")
if not os.path.exists(model_path):
os.mkdir(model_path)
print(f"## llamafile model not found, pls download from https://github.com/Mozilla-Ocho/llamafile")
def get_models():
files = os.listdir(model_path)
res=[]
for file in files:
if os.path.isfile(os.path.join(model_path, file)):
res.append(file)
res=phi_sort(res)
return res
def run_llamafile(llamafile,file_name):
global process
if process:
return 'http://127.0.0.1:8080'
# Check the operating system
operating_system = platform.system()
mp=os.path.join(model_path,file_name)
# Set the file name and command based on the operating system
# file_name = "llava-v1.5-7b-q4.llamafile"
#todo 修改为llama cmd 运行
# ./llamafile.exe -ngl 9999 -m openchat-3.5-0106.Q4_K_M.gguf --server --nobrowser
# 增加判断,是否是视觉模型,--mmproj llava-v1.5-7b-mmproj-Q4_0.gguf
def match_visual_model(model_name):
if model_name.startswith("llava-phi-3-mini-"):
return os.path.join(model_path,"llava-phi-3-mini-mmproj-f16.gguf")
else:
return None
command = f"{llamafile} -m {mp} --n-gpu-layers 999 --server --nobrowser"
if match_visual_model(file_name):
mmproj=match_visual_model(file_name)
command = f"{llamafile} -m {mp} --mmproj {mmproj} --n-gpu-layers 999 --server --nobrowser"
print("\033[91m" + command + "\033[0m")
# Grant execution permission on macOS, Linux, or BSD
if operating_system in ["Darwin", "Linux", "FreeBSD", "OpenBSD"]:
if not os.access(mp, os.X_OK):
os.chmod(mp, 0o755)
# Run the llamafile
process = subprocess.Popen(command, shell=True)
return 'http://127.0.0.1:8080'
def stop_llamafile():
global process
if not process:
return "No running process to kill"
# Get the process ID of the running llamafile process
process_id = process.pid
os.system('taskkill /t /f /pid {}'.format(process_id))
# Check if the process is still running
if psutil.pid_exists(process_id):
return "Failed to kill the process"
process = None
return "Process killed successfully"
def get_server_status():
url = 'http://localhost:8080/health'
try:
response = requests.get(url)
response.raise_for_status() # Raise exception for non-2xx response codes
data = response.json()
return data['status']
except requests.exceptions.RequestException as e:
return f'Error: {e}'
except KeyError:
return 'Error: Failed to retrieve server status'
# 创建路由表
routes = PromptServer.instance.routes
@routes.post('/llamafile')
async def llamafile_hander(request):
data = await request.json()
dirname=os.path.dirname(__file__)
result={
"data":""
}
if 'task' in data:
if data['task']=='list':
result={
"data":get_models()
}
if data['task']=='run':
model_name=get_models()[0]
if 'model_name' in data:
model_name=data['model_name']
#todo 修改为llama cmd 运行
# ./llamafile.exe -ngl 9999 -m openchat-3.5-0106.Q4_K_M.gguf --server --nobrowser
res=run_llamafile(os.path.join(dirname,'llamafile.exe'),model_name)
if res:
result={
"data":res,
"model_name":model_name
}
if data['task']=='stop':
res=stop_llamafile()
result={
"data":res
}
if data['task']=='health':
result={
"data":get_server_status()
}
return web.json_response(result)
# @routes.post('/search/bing')
# async def search_hander(request):
# data = await request.json()
# result=Bing(data['keyword'])
# return web.json_response(result)
NODE_CLASS_MAPPINGS = {
}
NODE_DISPLAY_NAME_MAPPINGS = {
}
WEB_DIRECTORY = "./web"
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS', 'WEB_DIRECTORY']