pycoze 0.1.75__py3-none-any.whl → 0.1.76__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- pycoze/ai/__init__.py +1 -1
- pycoze/ai/comfyui.py +33 -0
- pycoze/ai/{gpu_reserve.py → vram_reserve.py} +28 -31
- pycoze/bot/bot.py +6 -2
- {pycoze-0.1.75.dist-info → pycoze-0.1.76.dist-info}/METADATA +1 -1
- {pycoze-0.1.75.dist-info → pycoze-0.1.76.dist-info}/RECORD +9 -8
- {pycoze-0.1.75.dist-info → pycoze-0.1.76.dist-info}/LICENSE +0 -0
- {pycoze-0.1.75.dist-info → pycoze-0.1.76.dist-info}/WHEEL +0 -0
- {pycoze-0.1.75.dist-info → pycoze-0.1.76.dist-info}/top_level.txt +0 -0
pycoze/ai/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
from .
|
1
|
+
from .vram_reserve import reserve_vram_retry, release_vram, reserve_vram
|
pycoze/ai/comfyui.py
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
from .vram_reserve import reserve_vram_retry, release_vram
|
2
|
+
|
3
|
+
|
4
|
+
class ComfyUI:
|
5
|
+
def __init__(self, file_path):
|
6
|
+
self.file_path = file_path
|
7
|
+
|
8
|
+
def load(self):
|
9
|
+
reserve_vram_retry(20) # 20GB VRAM
|
10
|
+
with open(self.file_path, "r") as file:
|
11
|
+
return file.read()
|
12
|
+
|
13
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
14
|
+
clear_memory()
|
15
|
+
return None
|
16
|
+
|
17
|
+
def unload(self):
|
18
|
+
clear_memory()
|
19
|
+
return None
|
20
|
+
|
21
|
+
|
22
|
+
def clear_memory():
|
23
|
+
import requests
|
24
|
+
import json
|
25
|
+
|
26
|
+
release_vram()
|
27
|
+
|
28
|
+
url = "http://127.0.0.1:8188/free"
|
29
|
+
data = {"unload_models": True, "free_memory": False}
|
30
|
+
response = requests.post(
|
31
|
+
url, data=json.dumps(data), headers={"Content-Type": "application/json"}
|
32
|
+
)
|
33
|
+
return response.status_code, response.text
|
@@ -12,14 +12,14 @@ try:
|
|
12
12
|
# 定义数据库连接和初始化
|
13
13
|
params = utils.arg.read_params_file()
|
14
14
|
if params:
|
15
|
-
DATABASE_PATH = params["appPath"] + "/
|
15
|
+
DATABASE_PATH = params["appPath"] + "/vram_usage.db"
|
16
16
|
else:
|
17
17
|
raise Exception("No params")
|
18
18
|
except:
|
19
19
|
DATABASE_DIR = os.path.expanduser("~/pycoze")
|
20
20
|
os.makedirs(DATABASE_DIR, exist_ok=True)
|
21
|
-
DATABASE_PATH = os.path.join(DATABASE_DIR, "
|
22
|
-
TABLE_NAME = "
|
21
|
+
DATABASE_PATH = os.path.join(DATABASE_DIR, "vram_usage.db")
|
22
|
+
TABLE_NAME = "vram_usage"
|
23
23
|
|
24
24
|
|
25
25
|
def initialize_db():
|
@@ -38,10 +38,9 @@ def initialize_db():
|
|
38
38
|
conn.close()
|
39
39
|
|
40
40
|
|
41
|
-
|
42
|
-
def get_gpu_resources():
|
41
|
+
def get_vram_resources():
|
43
42
|
try:
|
44
|
-
# 使用nvidia-smi命令获取
|
43
|
+
# 使用nvidia-smi命令获取VRAM信息
|
45
44
|
result = subprocess.run(
|
46
45
|
["nvidia-smi", "--query-gpu=memory.free", "--format=csv,noheader,nounits"],
|
47
46
|
stdout=subprocess.PIPE,
|
@@ -55,7 +54,7 @@ def get_gpu_resources():
|
|
55
54
|
except:
|
56
55
|
pass
|
57
56
|
|
58
|
-
# 获取正在使用
|
57
|
+
# 获取正在使用VRAM的进程信息
|
59
58
|
process_result = subprocess.run(
|
60
59
|
[
|
61
60
|
"nvidia-smi",
|
@@ -76,18 +75,17 @@ def get_gpu_resources():
|
|
76
75
|
python_memory_usage += float(used_memory)
|
77
76
|
except:
|
78
77
|
pass
|
79
|
-
print("
|
80
|
-
print("
|
78
|
+
print("total_free_vram_memory: ", total_free_memory)
|
79
|
+
print("python_vram_memory_usage: ", python_memory_usage)
|
81
80
|
# 计算排除python进程后的总空闲内存
|
82
81
|
total_free_memory -= python_memory_usage
|
83
82
|
return round(total_free_memory / 1024, 2)
|
84
83
|
except Exception as e:
|
85
|
-
print(f"Error getting
|
84
|
+
print(f"Error getting VRAM resources: {e}")
|
86
85
|
return 0.0
|
87
86
|
|
88
87
|
|
89
|
-
|
90
|
-
def reserve_gpu(gb, uid=None):
|
88
|
+
def reserve_vram(gb, uid=None):
|
91
89
|
if uid is None:
|
92
90
|
uid = f"pid:{os.getpid()}"
|
93
91
|
with sqlite3.connect(DATABASE_PATH) as conn:
|
@@ -96,21 +94,21 @@ def reserve_gpu(gb, uid=None):
|
|
96
94
|
total_reserved = cursor.fetchone()[0]
|
97
95
|
if total_reserved is None:
|
98
96
|
total_reserved = 0.0
|
99
|
-
available_gb =
|
97
|
+
available_gb = get_vram_resources() - total_reserved
|
100
98
|
if available_gb >= gb:
|
101
99
|
cursor.execute(
|
102
100
|
f"INSERT INTO {TABLE_NAME} (uid, reserved_gb) VALUES (?, ?)",
|
103
101
|
(uid, gb),
|
104
102
|
)
|
105
103
|
conn.commit()
|
106
|
-
print(f"预留成功,剩余
|
104
|
+
print(f"预留成功,剩余VRAM大小: {available_gb - gb} GB")
|
107
105
|
return True
|
108
106
|
else:
|
109
|
-
print(f"预留失败,剩余
|
107
|
+
print(f"预留失败,剩余VRAM大小: {available_gb} GB")
|
110
108
|
return False
|
111
109
|
|
112
110
|
|
113
|
-
def
|
111
|
+
def reserve_vram_retry(gb, retry=None, uid=None):
|
114
112
|
if retry is None:
|
115
113
|
# 接近无限重试,python中允许无限大的整数,尽管sys.maxsize不是真正的无限大,但足够大
|
116
114
|
retry = sys.maxsize
|
@@ -118,32 +116,31 @@ def reserve_gpu_retry(gb, retry=None, uid=None):
|
|
118
116
|
time.sleep(1)
|
119
117
|
if i % 10 == 0 or i < 10:
|
120
118
|
print(f"重试第{i}次")
|
121
|
-
if
|
119
|
+
if reserve_vram(gb, uid):
|
122
120
|
return True
|
123
121
|
return False
|
124
122
|
|
125
123
|
|
126
|
-
|
127
|
-
def release_gpu(uid=None):
|
124
|
+
def release_vram(uid=None):
|
128
125
|
if uid is None:
|
129
126
|
uid = f"pid:{os.getpid()}"
|
130
127
|
with sqlite3.connect(DATABASE_PATH) as conn:
|
131
128
|
cursor = conn.cursor()
|
132
129
|
cursor.execute(f"DELETE FROM {TABLE_NAME} WHERE uid = ?", (uid,))
|
133
130
|
conn.commit()
|
134
|
-
# 计算释放后的剩余
|
131
|
+
# 计算释放后的剩余VRAM大小
|
135
132
|
cursor.execute(f"SELECT SUM(reserved_gb) FROM {TABLE_NAME}")
|
136
133
|
total_reserved = cursor.fetchone()[0]
|
137
134
|
if total_reserved is None:
|
138
135
|
total_reserved = 0.0
|
139
|
-
available_gb =
|
140
|
-
print(f"释放成功,剩余
|
136
|
+
available_gb = get_vram_resources() - total_reserved
|
137
|
+
print(f"释放成功,剩余VRAM大小: {available_gb} GB")
|
141
138
|
|
142
139
|
|
143
140
|
# 注册退出时的清理函数
|
144
141
|
def cleanup():
|
145
|
-
|
146
|
-
print("程序退出,
|
142
|
+
release_vram()
|
143
|
+
print("程序退出,VRAM资源已释放")
|
147
144
|
|
148
145
|
|
149
146
|
def initialize_and_check():
|
@@ -173,12 +170,12 @@ initialize_and_check()
|
|
173
170
|
atexit.register(cleanup)
|
174
171
|
|
175
172
|
if __name__ == "__main__":
|
176
|
-
if
|
177
|
-
print("(1)
|
178
|
-
if
|
179
|
-
print("(2)
|
173
|
+
if reserve_vram_retry(5):
|
174
|
+
print("(1)VRAM资源预留成功")
|
175
|
+
if reserve_vram_retry(5):
|
176
|
+
print("(2)VRAM资源预留成功")
|
180
177
|
time.sleep(100)
|
181
|
-
|
182
|
-
print("
|
178
|
+
release_vram()
|
179
|
+
print("VRAM资源释放成功")
|
183
180
|
else:
|
184
|
-
print("
|
181
|
+
print("VRAM资源不足,无法预留")
|
pycoze/bot/bot.py
CHANGED
@@ -36,7 +36,7 @@ def agent_chat(bot_setting_file, history):
|
|
36
36
|
model=cfg["model"],
|
37
37
|
temperature=(
|
38
38
|
role_setting["temperature"] * 2
|
39
|
-
if cfg["model"].startswith("deepseek")
|
39
|
+
if cfg["model"].startswith("deepseek") or cfg["model"].startswith("yi-")
|
40
40
|
else role_setting["temperature"]
|
41
41
|
),
|
42
42
|
stop_sequences=[
|
@@ -45,7 +45,11 @@ def agent_chat(bot_setting_file, history):
|
|
45
45
|
], # 停用deepseek的工具调用标记,不然会虚构工具调用过程和结果
|
46
46
|
)
|
47
47
|
prompt = role_setting["prompt"]
|
48
|
-
if
|
48
|
+
if (
|
49
|
+
cfg["model"].startswith("deepseek")
|
50
|
+
or cfg["model"].startswith("yi-")
|
51
|
+
and len(tools) > 0
|
52
|
+
):
|
49
53
|
prompt += """
|
50
54
|
如果需要调用工具,请使用以正确的json格式进行结尾(务必保证json格式正确,不要出现反斜杠未转义等问题):
|
51
55
|
```json
|
@@ -2,10 +2,11 @@ pycoze/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
pycoze/module.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
3
|
pycoze/access/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
4
|
pycoze/access/tool_for_bot.py,sha256=Nv6XlxcqaLaMVQNDkpcthlqaSxyfHoOfWiTScQXO22Y,2656
|
5
|
-
pycoze/ai/__init__.py,sha256=
|
6
|
-
pycoze/ai/
|
5
|
+
pycoze/ai/__init__.py,sha256=Smivpb8qbRnzWkzKRe2IxsmKP5Dh8EvngDFdkD_DVLo,73
|
6
|
+
pycoze/ai/comfyui.py,sha256=u75tZywkuXiOdm7XST2kBAaveJKpPvY_qTQr_TN9sXk,795
|
7
|
+
pycoze/ai/vram_reserve.py,sha256=s55Cy-Q5mTq-k5oIPbAFwCfrjatjN0QTjQxW7WBTPZI,5738
|
7
8
|
pycoze/bot/__init__.py,sha256=6HHMxDQVOyZM9dtSjQm9tjGnhj4h7CixD0JOvEwTi48,41
|
8
|
-
pycoze/bot/bot.py,sha256=
|
9
|
+
pycoze/bot/bot.py,sha256=aEG_B4ML6ONZ6FGcWYfBtlcMSuYBE8CjXo6ZYESQEaU,2869
|
9
10
|
pycoze/bot/agent/__init__.py,sha256=YR9vpkEQn1e4937r_xFPJXUCPBEJ0SFzEQDBe2x3-YA,157
|
10
11
|
pycoze/bot/agent/agent.py,sha256=7xnz4a8LDyAVU0O3tS6n7_jSnr_lukQxfNR2znCWoV4,4611
|
11
12
|
pycoze/bot/agent/assistant.py,sha256=QLeWaPi415P9jruYOm8qcIbC94cXXAhJYmLTkyC9NTQ,1267
|
@@ -22,8 +23,8 @@ pycoze/ui/ui_def.py,sha256=UhhU_yB3GV9ISbvTWT48hsHPHI250BhMILh6bu5Uioo,4206
|
|
22
23
|
pycoze/utils/__init__.py,sha256=TNJhFfY7JYdLlzuP9GvgxfNXUtbgH_NUUJSqHXCxJn4,78
|
23
24
|
pycoze/utils/arg.py,sha256=kA3KBQzXc2WlH5XbF8kfikfpqljiKaW7oY_GE4Qyffc,753
|
24
25
|
pycoze/utils/text_or_file.py,sha256=gpxZVWt2DW6YiEg_MnMuwg36VNf3TX383QD_1oZNB0Y,551
|
25
|
-
pycoze-0.1.
|
26
|
-
pycoze-0.1.
|
27
|
-
pycoze-0.1.
|
28
|
-
pycoze-0.1.
|
29
|
-
pycoze-0.1.
|
26
|
+
pycoze-0.1.76.dist-info/LICENSE,sha256=QStd_Qsd0-kAam_-sOesCIp_uKrGWeoKwt9M49NVkNU,1090
|
27
|
+
pycoze-0.1.76.dist-info/METADATA,sha256=ZPC58zR5Sued7MLMyaiOPY7T7PYzd-MCadxAStH_HV0,719
|
28
|
+
pycoze-0.1.76.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
29
|
+
pycoze-0.1.76.dist-info/top_level.txt,sha256=76dPeDhKvOCleL3ZC5gl1-y4vdS1tT_U1hxWVAn7sFo,7
|
30
|
+
pycoze-0.1.76.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|