Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -84,76 +84,84 @@ def check_server_ready(port):
|
|
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 |
try:
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
time.sleep(1)
|
132 |
-
|
133 |
-
raise TimeoutError("New image was not generated in time")
|
134 |
-
|
135 |
-
except Exception as e:
|
136 |
-
logger.error(f"Error in generate_image: {e}")
|
137 |
-
|
138 |
-
finally:
|
139 |
-
if process and process.poll() is None:
|
140 |
-
process.terminate()
|
141 |
-
logger.debug("process.terminate()")
|
142 |
-
try:
|
143 |
-
logger.debug("process.wait(timeout=5)")
|
144 |
-
process.wait(timeout=5)
|
145 |
-
except subprocess.TimeoutExpired:
|
146 |
-
logger.debug("process.kill()")
|
147 |
-
process.kill()
|
148 |
|
149 |
|
150 |
|
151 |
if __name__ == "__main__":
|
152 |
-
demo = gr.Interface(fn=
|
153 |
inputs=[
|
154 |
"text",
|
155 |
gr.Image(image_mode='RGBA', type="numpy"),
|
156 |
-
gr.Image(image_mode='RGBA', type="numpy")
|
|
|
157 |
],
|
158 |
outputs=[
|
159 |
gr.Image(type="numpy", image_mode='RGBA')
|
|
|
84 |
|
85 |
|
86 |
|
87 |
+
def generate_image_with_allocation(prompt, image, image2, allocation_time):
|
88 |
+
if allocation_time is None:
|
89 |
+
allocation_time = 170
|
90 |
+
else:
|
91 |
+
allocation_time = int(allocation_time)
|
92 |
+
|
93 |
+
@spaces.GPU(duration=allocation_time)
|
94 |
+
def generate_image(prompt, image, image2):
|
95 |
+
prefix_filename = str(random.randint(0, 999999))
|
96 |
+
prompt = prompt.replace('ComfyUI', prefix_filename)
|
97 |
+
prompt = json.loads(prompt)
|
98 |
+
|
99 |
+
image = Image.fromarray(image)
|
100 |
+
image.save(INPUT_DIR + '/input.png', format='PNG')
|
101 |
+
if image2 is not None:
|
102 |
+
image2 = Image.fromarray(image2)
|
103 |
+
image2.save(INPUT_DIR + '/input2.png', format='PNG')
|
104 |
+
|
105 |
+
process = None
|
106 |
+
new_port = str(random.randint(8123, 8200))
|
107 |
+
|
108 |
+
try:
|
109 |
+
# Запускаем скрипт как подпроцесс
|
110 |
+
process = subprocess.Popen([sys.executable, COMF_PATH, "--listen", "127.0.0.1", "--port", new_port])
|
111 |
+
logger.debug(f'Subprocess started with PID: {process.pid}')
|
112 |
+
|
113 |
+
# Ожидание запуска сервера
|
114 |
+
for _ in range(30): # Максимум 30 секунд ожидания
|
115 |
+
if check_server_ready(new_port):
|
116 |
+
break
|
117 |
+
time.sleep(1)
|
118 |
+
else:
|
119 |
+
raise TimeoutError("Server did not start in time")
|
120 |
+
|
121 |
+
start_queue(prompt, new_port)
|
122 |
+
|
123 |
+
# Ожидание нового изображения
|
124 |
+
timeout = 360 # Максимальное время ожидания в секундах
|
125 |
+
start_time = time.time()
|
126 |
+
while time.time() - start_time < timeout:
|
127 |
+
latest_image = wait_for_image_with_prefix(OUTPUT_DIR, prefix_filename)
|
128 |
+
if latest_image:
|
129 |
+
logger.debug(f"file is: {latest_image}")
|
130 |
+
try:
|
131 |
+
return Image.open(latest_image)
|
132 |
+
finally:
|
133 |
+
delete_image_file(latest_image)
|
134 |
+
delete_image_file(INPUT_DIR + '/input.png')
|
135 |
+
if image2 is not None:
|
136 |
+
delete_image_file(INPUT_DIR + '/input2.png')
|
137 |
+
time.sleep(1)
|
138 |
+
|
139 |
+
raise TimeoutError("New image was not generated in time")
|
140 |
+
|
141 |
+
except Exception as e:
|
142 |
+
logger.error(f"Error in generate_image: {e}")
|
143 |
+
|
144 |
+
finally:
|
145 |
+
if process and process.poll() is None:
|
146 |
+
process.terminate()
|
147 |
+
logger.debug("process.terminate()")
|
148 |
try:
|
149 |
+
logger.debug("process.wait(timeout=5)")
|
150 |
+
process.wait(timeout=5)
|
151 |
+
except subprocess.TimeoutExpired:
|
152 |
+
logger.debug("process.kill()")
|
153 |
+
process.kill()
|
154 |
+
return generate_image(prompt, image, image2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
|
157 |
|
158 |
if __name__ == "__main__":
|
159 |
+
demo = gr.Interface(fn=generate_image_with_allocation,
|
160 |
inputs=[
|
161 |
"text",
|
162 |
gr.Image(image_mode='RGBA', type="numpy"),
|
163 |
+
gr.Image(image_mode='RGBA', type="numpy"),
|
164 |
+
"text"
|
165 |
],
|
166 |
outputs=[
|
167 |
gr.Image(type="numpy", image_mode='RGBA')
|