Freak-ppa commited on
Commit
136fa84
1 Parent(s): 4a7a3e7

Upload 3 files

Browse files
Files changed (3) hide show
  1. .env +3 -0
  2. app.py +165 -0
  3. requirements.txt +122 -0
.env ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ OUTPUT_DIR=ComfyUI/output
2
+ INPUT_DIR=ComfyUI/input
3
+ COMF_PATH=ComfyUI/main.py
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import multiprocessing
5
+ import subprocess
6
+ import sys
7
+ import time
8
+ import signal
9
+ import json
10
+ import os
11
+ import requests
12
+
13
+ from loguru import logger
14
+ from decouple import config
15
+
16
+ from pathlib import Path
17
+ from PIL import Image
18
+ import io
19
+
20
+ URL="http://127.0.0.1"
21
+ OUTPUT_DIR = config('OUTPUT_DIR')
22
+ INPUT_DIR = config('INPUT_DIR')
23
+ 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())}")
31
+ print(torch.version.cuda)
32
+ device = torch.cuda.get_device_name(torch.cuda.current_device())
33
+ print(device)
34
+
35
+
36
+ def wait_for_image_with_prefix(folder, prefix):
37
+ def is_file_ready(file_path):
38
+ initial_size = os.path.getsize(file_path)
39
+ time.sleep(1)
40
+ return initial_size == os.path.getsize(file_path)
41
+
42
+
43
+ files = os.listdir(folder)
44
+ image_files = [f for f in files if f.lower().startswith(prefix.lower()) and
45
+ f.lower().endswith(('.png', '.jpg', '.jpeg'))]
46
+
47
+ if image_files:
48
+ # Sort by modification time to get the latest file
49
+ image_files.sort(key=lambda x: os.path.getmtime(os.path.join(folder, x)), reverse=True)
50
+ latest_image = os.path.join(folder, image_files[0])
51
+
52
+ if is_file_ready(latest_image):
53
+ # Wait a bit more to ensure the file is completely written
54
+ time.sleep(3)
55
+ return latest_image
56
+
57
+ # If no matching file found, wait before checking again
58
+ return None
59
+
60
+
61
+ def delete_image_file(file_path):
62
+ try:
63
+ if os.path.exists(file_path):
64
+ os.remove(file_path)
65
+ logger.debug(f"file {file_path} deleted")
66
+ else:
67
+ logger.debug(f"file {file_path} is not exist")
68
+ except Exception as e:
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(f"{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, 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 = 220 # Максимальное время ожидания в секундах
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')
160
+ ]
161
+ )
162
+ demo.launch(debug=True)
163
+ logger.debug('demo.launch()')
164
+
165
+ logger.info("Основной скрипт завершил работу.")
requirements.txt ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ absl-py==2.1.0
2
+ accelerate
3
+ aiofiles==23.2.1
4
+ aiohttp==3.9.5
5
+ aiosignal==1.3.1
6
+ altair==5.3.0
7
+ annotated-types==0.7.0
8
+ anyio==4.4.0
9
+ async-timeout==4.0.3
10
+ attrs==23.2.0
11
+ certifi==2024.6.2
12
+ cffi==1.16.0
13
+ charset-normalizer==3.3.2
14
+ click==8.1.7
15
+ colorama==0.4.6
16
+ contourpy==1.2.1
17
+ cycler==0.12.1
18
+ diffusers
19
+ dnspython==2.6.1
20
+ einops==0.8.0
21
+ email_validator==2.2.0
22
+ exceptiongroup==1.2.1
23
+ fastapi==0.111.0
24
+ fastapi-cli==0.0.4
25
+ ffmpy==0.3.2
26
+ filelock==3.15.4
27
+ fonttools==4.53.0
28
+ frozenlist==1.4.1
29
+ fsspec==2024.6.1
30
+ gevent==24.2.1
31
+ greenlet==3.0.3
32
+ h11==0.14.0
33
+ httpcore==1.0.5
34
+ httptools==0.6.1
35
+ httpx==0.27.0
36
+ huggingface-hub==0.23.4
37
+ idna==3.7
38
+ importlib_metadata==8.0.0
39
+ importlib_resources==6.4.0
40
+ intel-openmp==2021.4.0
41
+ Jinja2==3.1.4
42
+ jsonschema==4.22.0
43
+ jsonschema-specifications==2023.12.1
44
+ kiwisolver==1.4.5
45
+ kornia==0.7.3
46
+ kornia_rs==0.1.3
47
+ loguru==0.7.2
48
+ markdown-it-py==3.0.0
49
+ MarkupSafe==2.1.5
50
+ matplotlib==3.9.0
51
+ mdurl==0.1.2
52
+ mediapipe==0.10.9
53
+ mkl==2021.4.0
54
+ mpmath==1.3.0
55
+ multidict==6.0.5
56
+ networkx==3.3
57
+ numpy==1.26.4
58
+ opencv-contrib-python==4.9.0.80
59
+ orjson==3.10.5
60
+ packaging==24.1
61
+ pandas==2.2.2
62
+ peft==0.11.1
63
+ pillow==10.3.0
64
+ psutil==6.0.0
65
+ pycparser==2.22
66
+ pydantic==2.7.4
67
+ pydantic_core==2.18.4
68
+ pydub==0.25.1
69
+ Pygments==2.18.0
70
+ pyparsing==3.1.2
71
+ python-dateutil==2.9.0.post0
72
+ python-decouple==3.8
73
+ python-dotenv==1.0.1
74
+ python-multipart==0.0.9
75
+ pytz==2024.1
76
+ PyYAML==6.0.1
77
+ referencing==0.35.1
78
+ regex==2024.5.15
79
+ requests==2.32.3
80
+ rich==13.7.1
81
+ rpds-py==0.18.1
82
+ ruff==0.5.0
83
+ safetensors==0.4.3
84
+ scipy==1.14.0
85
+ semantic-version==2.10.0
86
+ shellingham==1.5.4
87
+ six==1.16.0
88
+ sniffio==1.3.1
89
+ sounddevice==0.4.6
90
+ soundfile==0.12.1
91
+ spandrel==0.3.4
92
+ starlette==0.37.2
93
+ sympy==1.12.1
94
+ tbb==2021.13.0
95
+ tokenizers==0.19.1
96
+ tomlkit==0.12.0
97
+ toolz==0.12.1
98
+ torch
99
+ torchaudio
100
+ torchsde
101
+ torchvision
102
+ tqdm==4.66.4
103
+ trampoline==0.1.2
104
+ transformers==4.42.3
105
+ typer==0.12.3
106
+ typing_extensions==4.12.2
107
+ tzdata==2024.1
108
+ ujson==5.10.0
109
+ urllib3==2.2.2
110
+ uvicorn==0.30.1
111
+ watchfiles==0.22.0
112
+ websocket==0.2.1
113
+ websocket-client==1.6.3
114
+ websockets==11.0.3
115
+ win32-setctime==1.1.0
116
+ yarl==1.9.4
117
+ zipp==3.19.2
118
+ zope.event==5.0
119
+ zope.interface==6.4.post2
120
+ opencv-python
121
+ insightface --user
122
+ onnxruntime