bizydraft 0.1.30__py3-none-any.whl → 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of bizydraft might be problematic. Click here for more details.

@@ -0,0 +1,150 @@
1
+ import asyncio
2
+ import math
3
+ import mimetypes
4
+ import os
5
+ import uuid
6
+ from urllib.parse import unquote
7
+
8
+ from aiohttp import ClientSession, ClientTimeout, web
9
+ from loguru import logger
10
+
11
+ try:
12
+ import execution
13
+ from server import PromptServer
14
+
15
+ comfy_server = PromptServer.instance
16
+ except ImportError:
17
+ logger.error(
18
+ "failed to import ComfyUI modules, ensure PYTHONPATH is set correctly. (export PYTHONPATH=$PYTHONPATH:/path/to/ComfyUI)"
19
+ )
20
+ exit(1)
21
+
22
+ BIZYDRAFT_MAX_FILE_SIZE = int(
23
+ os.getenv("BIZYDRAFT_MAX_FILE_SIZE", 100 * 1024 * 1024)
24
+ ) # 100MB
25
+ BIZYDRAFT_REQUEST_TIMEOUT = int(
26
+ os.getenv("BIZYDRAFT_REQUEST_TIMEOUT", 20 * 60)
27
+ ) # 20分钟
28
+ BIZYDRAFT_CHUNK_SIZE = int(os.getenv("BIZYDRAFT_CHUNK_SIZE", 1024 * 16)) # 16KB
29
+
30
+
31
+ async def view_image(request):
32
+ logger.debug(f"Received request for /view with query: {request.rel_url.query}")
33
+ if "filename" not in request.rel_url.query:
34
+ logger.warning("'filename' not provided in query string, returning 404")
35
+ return web.Response(status=404, text="'filename' not provided in query string")
36
+
37
+ filename = request.rel_url.query["filename"]
38
+ subfolder = request.rel_url.query.get("subfolder", "")
39
+
40
+ http_prefix_options = ("http:", "https:")
41
+
42
+ if not filename.startswith(http_prefix_options) and "http" not in subfolder:
43
+ logger.warning(
44
+ f"Invalid filename format: {filename=}, {subfolder=} only URLs are supported"
45
+ )
46
+ return web.Response(
47
+ status=400, text="Invalid filename format(only url supported)"
48
+ )
49
+
50
+ try:
51
+ if "http" in subfolder:
52
+ subfolder = subfolder[subfolder.find("http") :]
53
+ subfolder = unquote(subfolder)
54
+ filename = (
55
+ f"{subfolder}/{filename}"
56
+ if not filename.startswith(http_prefix_options)
57
+ else filename
58
+ ) # preview 3d request: https://host:port/api/view?filename=filename.glb&type=output&subfolder=https://bizyair-dev.oss-cn-shanghai.aliyuncs.com/outputs&rand=0.5763957215362988
59
+
60
+ content_type, _ = mimetypes.guess_type(filename)
61
+ if content_type and any(x in content_type for x in ("image", "video")):
62
+ return web.HTTPFound(filename)
63
+
64
+ timeout = ClientTimeout(total=BIZYDRAFT_REQUEST_TIMEOUT)
65
+ async with ClientSession(timeout=timeout) as session:
66
+ async with session.get(filename) as resp:
67
+ resp.raise_for_status()
68
+ content_length = int(resp.headers.get("Content-Length", 0))
69
+ if content_length > BIZYDRAFT_MAX_FILE_SIZE:
70
+ logger.warning(
71
+ f"File size {human_readable_size(content_length)} exceeds limit {human_readable_size(BIZYDRAFT_MAX_FILE_SIZE)}"
72
+ )
73
+ return web.Response(
74
+ status=413,
75
+ text=f"File size exceeds limit ({human_readable_size(BIZYDRAFT_MAX_FILE_SIZE)})",
76
+ )
77
+
78
+ headers = {
79
+ "Content-Disposition": f'attachment; filename="{uuid.uuid4()}"',
80
+ "Content-Type": "application/octet-stream",
81
+ }
82
+
83
+ proxy_response = web.StreamResponse(headers=headers)
84
+ await proxy_response.prepare(request)
85
+
86
+ total_bytes = 0
87
+ async for chunk in resp.content.iter_chunked(BIZYDRAFT_CHUNK_SIZE):
88
+ total_bytes += len(chunk)
89
+ if total_bytes > BIZYDRAFT_MAX_FILE_SIZE:
90
+ await proxy_response.write(b"")
91
+ return web.Response(
92
+ status=413,
93
+ text=f"File size exceeds limit during streaming ({human_readable_size(BIZYDRAFT_MAX_FILE_SIZE)})",
94
+ )
95
+ await proxy_response.write(chunk)
96
+
97
+ return proxy_response
98
+
99
+ except asyncio.TimeoutError:
100
+ return web.Response(
101
+ status=504,
102
+ text=f"Request timed out (max {BIZYDRAFT_REQUEST_TIMEOUT//60} minutes)",
103
+ )
104
+ except Exception as e:
105
+ return web.Response(
106
+ status=502, text=f"Failed to fetch remote resource: {str(e)}"
107
+ )
108
+
109
+
110
+ def human_readable_size(size_bytes):
111
+ if size_bytes == 0:
112
+ return "0B"
113
+ size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
114
+ i = int(math.floor(math.log(size_bytes, 1024)))
115
+ p = math.pow(1024, i)
116
+ s = round(size_bytes / p, 2)
117
+ return f"{s} {size_name[i]}"
118
+
119
+
120
+ async def post_prompt(request):
121
+ json_data = await request.json()
122
+
123
+ json_data = comfy_server.trigger_on_prompt(json_data)
124
+ if "prompt" in json_data:
125
+ prompt = json_data["prompt"]
126
+ valid = execution.validate_prompt(prompt)
127
+ if valid[0]:
128
+ response = {
129
+ "prompt_id": None,
130
+ "number": None,
131
+ "node_errors": valid[3],
132
+ }
133
+ logger.debug(f"Received POST request to /prompt with valid prompt")
134
+ return web.json_response(response)
135
+ else:
136
+ logger.debug(
137
+ f"Fail to validate prompt: {valid[1]=}, node_errors: {valid[3]=}"
138
+ )
139
+ return web.json_response(
140
+ {"error": valid[1], "node_errors": valid[3]}, status=400
141
+ )
142
+ else:
143
+ error = {
144
+ "type": "no_prompt",
145
+ "message": "No prompt provided",
146
+ "details": "No prompt provided",
147
+ "extra_info": {},
148
+ }
149
+ logger.debug(f"Received POST request to /prompt with no prompt: {error}")
150
+ return web.json_response({"error": error, "node_errors": {}}, status=400)
bizydraft/postload.py CHANGED
@@ -4,21 +4,39 @@ from loguru import logger
4
4
  def lazy_hook():
5
5
  try:
6
6
  import app.database.db
7
+ import comfy.utils
7
8
 
8
- origin_init_db = app.database.db.init_db
9
+ origin_fun_post_add_routes = comfy.utils.set_progress_bar_global_hook
10
+ origin_fun_pre_add_routes = app.database.db.init_db
9
11
 
10
- def hijack_all():
12
+ def hijack_all_pre_add_routes():
13
+ from bizydraft.hijack_routes import hijack_routes_pre_add_routes
14
+
15
+ hijack_routes_pre_add_routes()
16
+
17
+ def new_fun_pre_add_routes(*args, **kwargs):
18
+ hijack_all_pre_add_routes()
19
+ origin_fun_pre_add_routes(*args, **kwargs)
20
+
21
+ def hijack_all_post_add_routes():
11
22
  from bizydraft.hijack_nodes import hijack_nodes
12
- from bizydraft.hijack_routes import hijack_routes
13
23
 
14
24
  hijack_nodes()
15
- hijack_routes()
16
25
 
17
- def new_init_db():
18
- hijack_all()
19
- origin_init_db()
26
+ from bizydraft.hijack_routes import hijack_routes_post_add_routes
27
+
28
+ hijack_routes_post_add_routes()
29
+
30
+ from bizydraft.block_nodes import remove_blacklisted_nodes
31
+
32
+ remove_blacklisted_nodes()
33
+
34
+ def new_fun_post_add_routes(*args, **kwargs):
35
+ hijack_all_post_add_routes()
36
+ origin_fun_post_add_routes(*args, **kwargs)
20
37
 
21
- app.database.db.init_db = new_init_db
38
+ comfy.utils.set_progress_bar_global_hook = new_fun_post_add_routes
39
+ app.database.db.init_db = new_fun_pre_add_routes
22
40
 
23
41
  except Exception as e:
24
42
  logger.error(f"failed to lazyhook: {e}")