Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,33 +73,33 @@ def start_queue(prompt_workflow):
|
|
| 73 |
requests.post(URL, data=data)
|
| 74 |
|
| 75 |
|
| 76 |
-
def check_server_ready(
|
| 77 |
try:
|
| 78 |
-
response = requests.get(f"http://127.0.0.1:
|
| 79 |
return response.status_code == 200
|
| 80 |
except requests.RequestException:
|
| 81 |
return False
|
| 82 |
|
|
|
|
| 83 |
|
| 84 |
@spaces.GPU(duration=240)
|
| 85 |
def generate_image(prompt, image):
|
| 86 |
prompt = json.loads(prompt)
|
| 87 |
|
| 88 |
-
previous_image = None
|
| 89 |
-
|
| 90 |
image = Image.fromarray(image)
|
| 91 |
image.save(INPUT_DIR+'/input.png', format='PNG')
|
| 92 |
-
|
| 93 |
-
new_port = random.randint(8188, 8288)
|
| 94 |
prefix_filename = random.randint(0, 999999)
|
| 95 |
-
|
| 96 |
-
process =
|
| 97 |
-
logger.debug(f'Subprocess started with PID: {process.pid}')
|
| 98 |
-
latest_image = None
|
| 99 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Ожидание запуска сервера
|
| 101 |
for _ in range(20): # Максимум 20 секунд ожидания
|
| 102 |
-
if check_server_ready(
|
| 103 |
break
|
| 104 |
time.sleep(1)
|
| 105 |
else:
|
|
@@ -112,31 +112,33 @@ def generate_image(prompt, image):
|
|
| 112 |
start_time = time.time()
|
| 113 |
while time.time() - start_time < timeout:
|
| 114 |
latest_image = wait_for_image_with_prefix(OUTPUT_DIR, prefix_filename)
|
| 115 |
-
if latest_image
|
| 116 |
logger.debug(f"file is: {latest_image}")
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
time.sleep(1)
|
| 123 |
|
| 124 |
raise TimeoutError("New image was not generated in time")
|
| 125 |
|
| 126 |
except Exception as e:
|
| 127 |
logger.error(f"Error in generate_image: {e}")
|
| 128 |
-
|
|
|
|
| 129 |
finally:
|
| 130 |
-
|
| 131 |
-
if process.poll() is None:
|
| 132 |
process.terminate()
|
| 133 |
try:
|
| 134 |
process.wait(timeout=5)
|
| 135 |
except subprocess.TimeoutExpired:
|
| 136 |
-
process.kill()
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
return latest_image
|
| 140 |
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
|
|
|
| 73 |
requests.post(URL, data=data)
|
| 74 |
|
| 75 |
|
| 76 |
+
def check_server_ready():
|
| 77 |
try:
|
| 78 |
+
response = requests.get(f"http://127.0.0.1:8188/history/123", timeout=5)
|
| 79 |
return response.status_code == 200
|
| 80 |
except requests.RequestException:
|
| 81 |
return False
|
| 82 |
|
| 83 |
+
queue_reqs=set()
|
| 84 |
|
| 85 |
@spaces.GPU(duration=240)
|
| 86 |
def generate_image(prompt, image):
|
| 87 |
prompt = json.loads(prompt)
|
| 88 |
|
|
|
|
|
|
|
| 89 |
image = Image.fromarray(image)
|
| 90 |
image.save(INPUT_DIR+'/input.png', format='PNG')
|
| 91 |
+
|
|
|
|
| 92 |
prefix_filename = random.randint(0, 999999)
|
| 93 |
+
queue_reqs.add(prefix_filename)
|
| 94 |
+
process = None
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
+
# Запускаем скрипт как подпроцесс
|
| 97 |
+
process = subprocess.Popen([sys.executable, COMF_PATH, "--listen", "127.0.0.1"])
|
| 98 |
+
logger.debug(f'Subprocess started with PID: {process.pid}')
|
| 99 |
+
|
| 100 |
# Ожидание запуска сервера
|
| 101 |
for _ in range(20): # Максимум 20 секунд ожидания
|
| 102 |
+
if check_server_ready():
|
| 103 |
break
|
| 104 |
time.sleep(1)
|
| 105 |
else:
|
|
|
|
| 112 |
start_time = time.time()
|
| 113 |
while time.time() - start_time < timeout:
|
| 114 |
latest_image = wait_for_image_with_prefix(OUTPUT_DIR, prefix_filename)
|
| 115 |
+
if latest_image:
|
| 116 |
logger.debug(f"file is: {latest_image}")
|
| 117 |
+
try:
|
| 118 |
+
with open(latest_image, 'rb') as f:
|
| 119 |
+
photo = f.read()
|
| 120 |
+
logger.debug(f"file bytes size: {len(photo)}")
|
| 121 |
+
return io.BytesIO(photo)
|
| 122 |
+
finally:
|
| 123 |
+
delete_image_file(latest_image)
|
| 124 |
time.sleep(1)
|
| 125 |
|
| 126 |
raise TimeoutError("New image was not generated in time")
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
logger.error(f"Error in generate_image: {e}")
|
| 130 |
+
return None
|
| 131 |
+
|
| 132 |
finally:
|
| 133 |
+
queue_reqs.remove(prefix_filename)
|
| 134 |
+
if len(queue_reqs) == 0 and process and process.poll() is None:
|
| 135 |
process.terminate()
|
| 136 |
try:
|
| 137 |
process.wait(timeout=5)
|
| 138 |
except subprocess.TimeoutExpired:
|
| 139 |
+
process.kill()
|
| 140 |
+
|
| 141 |
+
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
if __name__ == "__main__":
|