Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ COMF_PATH = config('COMF_PATH')
|
|
| 24 |
|
| 25 |
import torch
|
| 26 |
|
| 27 |
-
import spaces
|
| 28 |
|
| 29 |
print(f"Is CUDA available: {torch.cuda.is_available()}")
|
| 30 |
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
|
@@ -69,24 +69,22 @@ def delete_image_file(file_path):
|
|
| 69 |
logger.debug(f"error {file_path}: {str(e)}")
|
| 70 |
|
| 71 |
|
| 72 |
-
def start_queue(prompt_workflow):
|
| 73 |
p = {"prompt": prompt_workflow}
|
| 74 |
data = json.dumps(p).encode('utf-8')
|
| 75 |
-
requests.post(URL, data=data)
|
| 76 |
|
| 77 |
|
| 78 |
-
def check_server_ready():
|
| 79 |
try:
|
| 80 |
-
response = requests.get(f"
|
| 81 |
return response.status_code == 200
|
| 82 |
except requests.RequestException:
|
| 83 |
return False
|
| 84 |
|
| 85 |
|
| 86 |
-
queue_reqs = set()
|
| 87 |
-
process_started = False
|
| 88 |
|
| 89 |
-
|
| 90 |
def generate_image(prompt, image):
|
| 91 |
prefix_filename = str(random.randint(0, 999999))
|
| 92 |
prompt = prompt.replace('ComfyUI', prefix_filename)
|
|
@@ -95,25 +93,23 @@ def generate_image(prompt, image):
|
|
| 95 |
image = Image.fromarray(image)
|
| 96 |
image.save(INPUT_DIR + '/input.png', format='PNG')
|
| 97 |
|
| 98 |
-
queue_reqs.add(prefix_filename)
|
| 99 |
process = None
|
| 100 |
-
|
|
|
|
| 101 |
try:
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
process = subprocess.Popen([sys.executable, COMF_PATH, "--listen", "127.0.0.1"])
|
| 106 |
-
logger.debug(f'Subprocess started with PID: {process.pid}')
|
| 107 |
|
| 108 |
# Ожидание запуска сервера
|
| 109 |
for _ in range(20): # Максимум 20 секунд ожидания
|
| 110 |
-
if check_server_ready():
|
| 111 |
break
|
| 112 |
time.sleep(1)
|
| 113 |
else:
|
| 114 |
raise TimeoutError("Server did not start in time")
|
| 115 |
|
| 116 |
-
start_queue(prompt)
|
| 117 |
|
| 118 |
# Ожидание нового изображения
|
| 119 |
timeout = 220 # Максимальное время ожидания в секундах
|
|
@@ -134,15 +130,14 @@ def generate_image(prompt, image):
|
|
| 134 |
logger.error(f"Error in generate_image: {e}")
|
| 135 |
return None
|
| 136 |
|
| 137 |
-
finally:
|
| 138 |
-
|
| 139 |
-
if len(queue_reqs) == 0 and process and process.poll() is None:
|
| 140 |
process.terminate()
|
| 141 |
try:
|
| 142 |
process.wait(timeout=5)
|
| 143 |
except subprocess.TimeoutExpired:
|
| 144 |
process.kill()
|
| 145 |
-
|
| 146 |
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|
|
|
|
| 24 |
|
| 25 |
import torch
|
| 26 |
|
| 27 |
+
#import spaces
|
| 28 |
|
| 29 |
print(f"Is CUDA available: {torch.cuda.is_available()}")
|
| 30 |
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
|
|
|
| 69 |
logger.debug(f"error {file_path}: {str(e)}")
|
| 70 |
|
| 71 |
|
| 72 |
+
def start_queue(prompt_workflow, port):
|
| 73 |
p = {"prompt": prompt_workflow}
|
| 74 |
data = json.dumps(p).encode('utf-8')
|
| 75 |
+
requests.post(URL+port+'/prompt', data=data)
|
| 76 |
|
| 77 |
|
| 78 |
+
def check_server_ready(port):
|
| 79 |
try:
|
| 80 |
+
response = requests.get(f"{URL}{port}/history/123", timeout=5)
|
| 81 |
return response.status_code == 200
|
| 82 |
except requests.RequestException:
|
| 83 |
return False
|
| 84 |
|
| 85 |
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
#@spaces.GPU(duration=240)
|
| 88 |
def generate_image(prompt, image):
|
| 89 |
prefix_filename = str(random.randint(0, 999999))
|
| 90 |
prompt = prompt.replace('ComfyUI', prefix_filename)
|
|
|
|
| 93 |
image = Image.fromarray(image)
|
| 94 |
image.save(INPUT_DIR + '/input.png', format='PNG')
|
| 95 |
|
|
|
|
| 96 |
process = None
|
| 97 |
+
new_port = str(random.randint(8123, 15000))
|
| 98 |
+
|
| 99 |
try:
|
| 100 |
+
# Запускаем скрипт как подпроцесс
|
| 101 |
+
process = subprocess.Popen([sys.executable, COMF_PATH, "--listen", "127.0.0.1", "--port", new_port])
|
| 102 |
+
logger.debug(f'Subprocess started with PID: {process.pid}')
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Ожидание запуска сервера
|
| 105 |
for _ in range(20): # Максимум 20 секунд ожидания
|
| 106 |
+
if check_server_ready(new_port):
|
| 107 |
break
|
| 108 |
time.sleep(1)
|
| 109 |
else:
|
| 110 |
raise TimeoutError("Server did not start in time")
|
| 111 |
|
| 112 |
+
start_queue(prompt, new_port)
|
| 113 |
|
| 114 |
# Ожидание нового изображения
|
| 115 |
timeout = 220 # Максимальное время ожидания в секундах
|
|
|
|
| 130 |
logger.error(f"Error in generate_image: {e}")
|
| 131 |
return None
|
| 132 |
|
| 133 |
+
finally:
|
| 134 |
+
if 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 |
if __name__ == "__main__":
|