pycoze 0.1.75__tar.gz → 0.1.76__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. {pycoze-0.1.75 → pycoze-0.1.76}/PKG-INFO +1 -1
  2. pycoze-0.1.76/pycoze/ai/__init__.py +1 -0
  3. pycoze-0.1.76/pycoze/ai/comfyui.py +33 -0
  4. pycoze-0.1.75/pycoze/ai/gpu_reserve.py → pycoze-0.1.76/pycoze/ai/vram_reserve.py +28 -31
  5. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/bot.py +6 -2
  6. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze.egg-info/PKG-INFO +1 -1
  7. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze.egg-info/SOURCES.txt +2 -1
  8. {pycoze-0.1.75 → pycoze-0.1.76}/setup.py +1 -1
  9. pycoze-0.1.75/pycoze/ai/__init__.py +0 -1
  10. {pycoze-0.1.75 → pycoze-0.1.76}/LICENSE +0 -0
  11. {pycoze-0.1.75 → pycoze-0.1.76}/README.md +0 -0
  12. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/__init__.py +0 -0
  13. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/access/__init__.py +0 -0
  14. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/access/tool_for_bot.py +0 -0
  15. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/__init__.py +0 -0
  16. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/agent/__init__.py +0 -0
  17. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/agent/agent.py +0 -0
  18. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/agent/agent_types/__init__.py +0 -0
  19. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/agent/agent_types/openai_func_call_agent.py +0 -0
  20. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/agent/agent_types/react_agent.py +0 -0
  21. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/agent/agent_types/react_prompt.py +0 -0
  22. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/agent/assistant.py +0 -0
  23. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/bot/agent/chat.py +0 -0
  24. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/module.py +0 -0
  25. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/ui/__init__.py +0 -0
  26. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/ui/base.py +0 -0
  27. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/ui/color.py +0 -0
  28. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/ui/typ.py +0 -0
  29. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/ui/ui_def.py +0 -0
  30. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/utils/__init__.py +0 -0
  31. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/utils/arg.py +0 -0
  32. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze/utils/text_or_file.py +0 -0
  33. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze.egg-info/dependency_links.txt +0 -0
  34. {pycoze-0.1.75 → pycoze-0.1.76}/pycoze.egg-info/top_level.txt +0 -0
  35. {pycoze-0.1.75 → pycoze-0.1.76}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.75
3
+ Version: 0.1.76
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -0,0 +1 @@
1
+ from .vram_reserve import reserve_vram_retry, release_vram, reserve_vram
@@ -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"] + "/gpu_usage.db"
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, "gpu_usage.db")
22
- TABLE_NAME = "gpu_usage"
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
- # 检测GPU资源
42
- def get_gpu_resources():
41
+ def get_vram_resources():
43
42
  try:
44
- # 使用nvidia-smi命令获取GPU信息
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
- # 获取正在使用GPU的进程信息
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("total_free_gpu_memory: ", total_free_memory)
80
- print("python_gpu_memory_usage: ", python_memory_usage)
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 GPU resources: {e}")
84
+ print(f"Error getting VRAM resources: {e}")
86
85
  return 0.0
87
86
 
88
87
 
89
- # 预留GPU资源
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 = get_gpu_resources() - total_reserved
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"预留成功,剩余GPU大小: {available_gb - gb} GB")
104
+ print(f"预留成功,剩余VRAM大小: {available_gb - gb} GB")
107
105
  return True
108
106
  else:
109
- print(f"预留失败,剩余GPU大小: {available_gb} GB")
107
+ print(f"预留失败,剩余VRAM大小: {available_gb} GB")
110
108
  return False
111
109
 
112
110
 
113
- def reserve_gpu_retry(gb, retry=None, uid=None):
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 reserve_gpu(gb, uid):
119
+ if reserve_vram(gb, uid):
122
120
  return True
123
121
  return False
124
122
 
125
123
 
126
- # 释放GPU资源
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
- # 计算释放后的剩余GPU大小
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 = get_gpu_resources() - total_reserved
140
- print(f"释放成功,剩余GPU大小: {available_gb} GB")
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
- release_gpu()
146
- print("程序退出,GPU资源已释放")
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 reserve_gpu_retry(5):
177
- print("(1)GPU资源预留成功")
178
- if reserve_gpu_retry(5):
179
- print("(2)GPU资源预留成功")
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
- release_gpu()
182
- print("GPU资源释放成功")
178
+ release_vram()
179
+ print("VRAM资源释放成功")
183
180
  else:
184
- print("GPU资源不足,无法预留")
181
+ print("VRAM资源不足,无法预留")
@@ -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 cfg["model"].startswith("deepseek") and len(tools) > 0:
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pycoze
3
- Version: 0.1.75
3
+ Version: 0.1.76
4
4
  Summary: Package for pycoze only!
5
5
  Author: Yuan Jie Xiong
6
6
  Author-email: aiqqqqqqq@qq.com
@@ -10,7 +10,8 @@ pycoze.egg-info/top_level.txt
10
10
  pycoze/access/__init__.py
11
11
  pycoze/access/tool_for_bot.py
12
12
  pycoze/ai/__init__.py
13
- pycoze/ai/gpu_reserve.py
13
+ pycoze/ai/comfyui.py
14
+ pycoze/ai/vram_reserve.py
14
15
  pycoze/bot/__init__.py
15
16
  pycoze/bot/bot.py
16
17
  pycoze/bot/agent/__init__.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="pycoze",
5
- version="0.1.75",
5
+ version="0.1.76",
6
6
  packages=find_packages(),
7
7
  install_requires=[],
8
8
  author="Yuan Jie Xiong",
@@ -1 +0,0 @@
1
- from .gpu_reserve import reserve_gpu_retry, release_gpu, reserve_gpu
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes