Etrwy commited on
Commit
37863c3
·
verified ·
1 Parent(s): 3cbfcf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -62
app.py CHANGED
@@ -84,76 +84,84 @@ def check_server_ready(port):
84
 
85
 
86
 
87
- @spaces.GPU(duration=172)
88
- def generate_image(prompt, image, image2):
89
- prefix_filename = str(random.randint(0, 999999))
90
- prompt = prompt.replace('ComfyUI', prefix_filename)
91
- prompt = json.loads(prompt)
92
-
93
- image = Image.fromarray(image)
94
- image.save(INPUT_DIR + '/input.png', format='PNG')
95
- if image2 is not None:
96
- image2 = Image.fromarray(image2)
97
- image2.save(INPUT_DIR + '/input2.png', format='PNG')
98
-
99
- process = None
100
- new_port = str(random.randint(8123, 8200))
101
-
102
- try:
103
- # Запускаем скрипт как подпроцесс
104
- process = subprocess.Popen([sys.executable, COMF_PATH, "--listen", "127.0.0.1", "--port", new_port])
105
- logger.debug(f'Subprocess started with PID: {process.pid}')
106
-
107
- # Ожидание запуска сервера
108
- for _ in range(30): # Максимум 20 секунд ожидания
109
- if check_server_ready(new_port):
110
- break
111
- time.sleep(1)
112
- else:
113
- raise TimeoutError("Server did not start in time")
114
-
115
- start_queue(prompt, new_port)
116
-
117
- # Ожидание нового изображения
118
- timeout = 240 # Максимальное время ожидания в секундах
119
- start_time = time.time()
120
- while time.time() - start_time < timeout:
121
- latest_image = wait_for_image_with_prefix(OUTPUT_DIR, prefix_filename)
122
- if latest_image:
123
- logger.debug(f"file is: {latest_image}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  try:
125
- return Image.open(latest_image)
126
- finally:
127
- delete_image_file(latest_image)
128
- delete_image_file(INPUT_DIR + '/input.png')
129
- if image2 is not None:
130
- delete_image_file(INPUT_DIR + '/input2.png')
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=generate_image,
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')