Freak-ppa commited on
Commit
1deaa99
·
verified ·
1 Parent(s): 858088e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ URL = config('URL')
17
+ OUTPUT_DIR = config('OUTPUT_DIR')
18
+ COMF_PATH = config('COMF_PATH')
19
+
20
+ import torch
21
+
22
+ import spaces
23
+
24
+ print(f"Is CUDA available: {torch.cuda.is_available()}")
25
+ print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
26
+ print(torch.version.cuda)
27
+ device = torch.cuda.get_device_name(torch.cuda.current_device())
28
+ print(device)
29
+
30
+
31
+ def get_latest_image(folder):
32
+ files = os.listdir(folder)
33
+ image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
34
+ image_files.sort(key=lambda x: os.path.getmtime(os.path.join(folder, x)))
35
+ latest_image = os.path.join(folder, image_files[-1]) if image_files else None
36
+ return latest_image
37
+
38
+
39
+ def start_queue(prompt_workflow):
40
+ p = {"prompt": prompt_workflow}
41
+ data = json.dumps(p).encode('utf-8')
42
+ requests.post(URL, data=data)
43
+
44
+
45
+ def check_server_ready():
46
+ try:
47
+ response = requests.get(f"http://127.0.0.1:8188/history/123", timeout=5)
48
+ return response.status_code == 200
49
+ except requests.RequestException:
50
+ return False
51
+
52
+
53
+ @spaces.GPU
54
+ def generate_image(prompt):
55
+ previous_image = get_latest_image(OUTPUT_DIR)
56
+
57
+ # Запускаем скрипт как подпроцесс
58
+ process = subprocess.Popen([sys.executable, COMF_PATH, "--listen", "127.0.0.1"])
59
+ logger.debug(f'Subprocess started with PID: {process.pid}')
60
+
61
+ try:
62
+ # Ожидание запуска сервера
63
+ for _ in range(20): # Максимум 20 секунд ожидания
64
+ if check_server_ready(): # Вам нужно реализовать эту функцию
65
+ break
66
+ time.sleep(1)
67
+ else:
68
+ raise TimeoutError("Server did not start in time")
69
+
70
+ start_queue(prompt)
71
+
72
+ # Ожидание нового изображения
73
+ timeout = 400 # Максимальное время ожидания в секундах
74
+ start_time = time.time()
75
+ while time.time() - start_time < timeout:
76
+ latest_image = get_latest_image(OUTPUT_DIR)
77
+ if latest_image != previous_image:
78
+ return latest_image
79
+ time.sleep(1)
80
+
81
+ raise TimeoutError("New image was not generated in time")
82
+
83
+ except Exception as e:
84
+ logger.error(f"Error in generate_image: {e}")
85
+ raise
86
+ finally:
87
+ # Завершаем подпроцесс
88
+ if process.poll() is None:
89
+ process.terminate()
90
+ try:
91
+ process.wait(timeout=5)
92
+ except subprocess.TimeoutExpired:
93
+ process.kill()
94
+
95
+ logger.error("No new image was generated")
96
+ return None
97
+
98
+
99
+ if __name__ == "__main__":
100
+ demo = gr.Interface(fn=generate_image, inputs=["text"], outputs=["image"])
101
+ demo.launch(debug=True)
102
+ logger.debug('demo.launch()')
103
+
104
+ logger.info("Основной скрипт завершил работу.")