File size: 12,437 Bytes
b38a367
7af3031
c9cc441
90ca682
 
 
 
c9cc441
 
d7a562e
c9cc441
 
 
 
 
 
 
 
 
721391f
c9cc441
2d403dd
 
 
c9cc441
 
 
b38a367
 
c9cc441
90ca682
 
b6c9f2e
 
90ca682
 
 
 
 
 
c9cc441
 
 
 
d859980
b38a367
 
c9cc441
 
b38a367
c9cc441
 
 
 
 
 
90ca682
c6e4f56
b38a367
c9cc441
db52736
7af3031
b38a367
c9cc441
 
 
c6e4f56
c9cc441
 
 
 
 
 
 
 
 
b38a367
 
 
6446714
c9cc441
 
 
b38a367
 
 
 
 
 
 
 
 
 
 
 
c9cc441
5426f86
 
b38a367
 
 
c9cc441
90ca682
d954860
 
b38a367
 
 
 
 
 
 
5426f86
c9cc441
 
 
 
 
b38a367
c9cc441
 
 
 
 
 
 
 
 
 
 
9757452
 
 
b38a367
9757452
b38a367
 
 
 
 
 
9757452
 
 
b38a367
9757452
b38a367
 
 
 
 
 
 
9757452
 
c465a32
 
b38a367
c6e4f56
c465a32
 
 
 
 
 
9757452
c9cc441
90ca682
c9cc441
90ca682
 
 
 
 
 
9757452
90ca682
 
c9cc441
6a66010
 
 
 
c9cc441
90ca682
 
 
 
 
c9cc441
 
 
b38a367
 
c9cc441
 
 
90ca682
9757452
7af3031
b38a367
90ca682
c9cc441
b38a367
 
c9cc441
 
 
90ca682
 
 
b38a367
c9cc441
90ca682
 
 
c9cc441
b38a367
c9cc441
 
 
90ca682
9757452
7af3031
c9cc441
 
90ca682
 
 
 
9757452
90ca682
 
c9cc441
c6e4f56
 
b38a367
 
 
 
 
 
 
 
c6e4f56
c9cc441
3fcb571
 
 
 
 
 
 
 
 
 
 
 
 
 
c9cc441
 
 
 
b38a367
 
 
 
c9cc441
 
ae27b39
 
 
2e48cc7
ae27b39
 
 
c9cc441
6a66010
 
 
ae27b39
 
 
 
 
 
 
 
c9cc441
ae27b39
c9cc441
6a66010
ae27b39
 
 
 
 
 
 
 
c9cc441
ae27b39
c9cc441
6a66010
ae27b39
 
 
 
 
 
 
 
c9cc441
90ca682
 
 
 
 
 
c9cc441
 
d7a562e
dd5dfc8
 
bd4674a
90ca682
d7a562e
 
2c91007
9757452
2e48cc7
2d403dd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
from flask import Flask, request, render_template, send_file, jsonify, send_from_directory, session, copy_current_request_context
from flask_socketio import SocketIO, join_room, leave_room, close_room, rooms, disconnect
from flask_cors import CORS
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import concurrent.futures

import io
import os
import argparse
from PIL import Image
import torch
import gc
from peft import PeftModel

import queue
import threading
import uuid
import concurrent.futures
from scripts.process_utils import *

from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler

app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")
# クライアントIDとルーム情報を保存するグローバル辞書
client_rooms = {}

# レート制限の設定
limiter = Limiter(
    get_remote_address,
    app=app,
    default_limits=["200 per day", "50 per hour"]
)

# タスクキューの作成とサイズ制限
MAX_QUEUE_SIZE = 100
task_queue = queue.Queue(maxsize=MAX_QUEUE_SIZE)
active_tasks = {}
task_futures = {}

# ThreadPoolExecutorの作成
executor = concurrent.futures.ThreadPoolExecutor(max_workers=int(os.environ.get('MAX_WORKERS', 4)))

gpu_lock = threading.Lock()

class Task:
    def __init__(self, task_id, mode, weight1, weight2, file_data, client_ip, client_id):
        self.task_id = task_id
        self.mode = mode
        self.weight1 = weight1
        self.weight2 = weight2
        self.file_data = file_data
        self.cancel_flag = False
        self.client_ip = client_ip
        self.is_processing = False
        self.client_id = client_id

# キューの状態を通知
def update_queue_status(message):
    socketio.emit('queue_update', {'active_tasks': len(active_tasks), 'message': message}, namespace='/demo')

def process_task(task):
    try:
        task.is_processing = True
        # ファイルデータをPIL Imageに変換
        image = Image.open(io.BytesIO(task.file_data))
        image = ensure_rgb(image)
        
        # キャンセルチェック
        if task.cancel_flag:
            return

        # 画像処理ロジックを呼び出す
        # GPU処理部分
        with gpu_lock:
            sotai_image, sketch_image = process_image_as_base64(image, task.mode, task.weight1, task.weight2)

        # キャンセルチェック
        if task.cancel_flag:
            return
        
        # クライアントIDをリクエストヘッダーから取得(クライアント側で設定する必要があります)
        client_id = task.client_id
        if client_id and client_id in client_rooms:
            room = client_rooms[client_id]
            # ルームにメッセージをemit
            socketio.emit('task_complete', {
                'task_id': task.task_id, 
                'sotai_image': sotai_image, 
                'sketch_image': sketch_image
            }, to=room, namespace='/demo')
        
    except Exception as e:
        print(f"Task error: {str(e)}")
        if not task.cancel_flag:
            client_id = task.client_id
            room = client_rooms[client_id]
            socketio.emit('task_error', {'task_id': task.task_id, 'error': str(e)}, to=room, namespace='/demo')
    finally:
        # タスク数をデクリメント
        client_ip = task.client_ip
        tasks_per_client[client_ip] = tasks_per_client.get(client_ip, 0) - 1
        print(f'Task {task.task_id} completed')
        task.is_processing = False
        if task.task_id in active_tasks.keys():
            del active_tasks[task.task_id]
        if task.task_id in task_futures.keys():
            del task_futures[task.task_id]

        update_queue_status('Task completed or cancelled')

def worker():
    while True:
        try:
            task = task_queue.get()
            if task.task_id in active_tasks.keys():
                future = executor.submit(process_task, task)
                task_futures[task.task_id] = future
        except Exception as e:
            print(f"Worker error: {str(e)}")
        finally:
            # Ensure the task is always removed from the queue
            task_queue.task_done()

# ワーカースレッドの開始
threading.Thread(target=worker, daemon=True).start()

# グローバル変数を使用して接続数とタスク数を管理
connected_clients = 0
tasks_per_client = {}
@socketio.on('connect', namespace='/demo')
def handle_connect(auth):
    client_id = request.sid
    room = f"room_{client_id}"  # クライアントごとに一意のルーム名を生成
    join_room(room)
    client_rooms[client_id] = room
    print(f"Client {client_id} connected and joined room {room}")
    
    global connected_clients
    connected_clients += 1

@socketio.on('disconnect' )
def handle_disconnect():
    client_id = request.sid
    if client_id in client_rooms:
        room = client_rooms[client_id]
        leave_room(room)
        del client_rooms[client_id]
        print(f"Client {client_id} disconnected and removed from room {room}")

    global connected_clients
    connected_clients -= 1
    # キャンセル処理:接続が切断された場合、そのクライアントに関連するタスクをキャンセル。ただし、1番目で処理中のタスクはキャンセルしない
    client_ip = get_remote_address()
    for task_id, task in active_tasks.items():
        if task.client_ip == client_ip and not task.is_processing:
            task.cancel_flag = True
            if task_id in task_futures:
                task_futures[task_id].cancel()
                del task_futures[task_id]
            del active_tasks[task_id]
            tasks_per_client[client_ip] = tasks_per_client.get(client_ip, 0) - 1

@app.route('/submit_task', methods=['POST'])
@limiter.limit("10 per minute")  # 1分間に10回までのリクエストに制限
def submit_task():
    if task_queue.full():
        return jsonify({'error': 'Task queue is full. Please try again later.'}), 503

    # クライアントIPアドレスを取得
    client_ip = get_remote_address()
    # 同一IPからの同時タスク数を制限
    if tasks_per_client.get(client_ip, 0) >= 2:
        return jsonify({'error': 'Maximum number of concurrent tasks reached'}), 429

    task_id = str(uuid.uuid4())
    file = request.files['file']
    mode = request.form.get('mode', 'refine')
    weight1 = float(request.form.get('weight1', 0.4))
    weight2 = float(request.form.get('weight2', 0.3))
    
    # ファイルタイプの制限
    allowed_extensions = {'png', 'jpg', 'jpeg', 'gif'}
    if '.' not in file.filename or file.filename.rsplit('.', 1)[1].lower() not in allowed_extensions:
        return jsonify({'error': 'Invalid file type'}), 415

    # ファイルデータをバイト列として保存
    file_data = file.read()
    
    client_id = request.headers.get('X-Client-ID')
    task = Task(task_id, mode, weight1, weight2, file_data, client_ip, client_id)
    task_queue.put(task)
    active_tasks[task_id] = task
    
    # 同一IPからのタスク数をインクリメント
    tasks_per_client[client_ip] = tasks_per_client.get(client_ip, 0) + 1

    update_queue_status('Task submitted') # すべてに通知
    
    queue_size = task_queue.qsize()
    task_order = get_active_task_order(task_id)
    return jsonify({'task_id': task_id, 'task_order': task_order, 'queue_size': queue_size}) 

@app.route('/cancel_task/<task_id>', methods=['POST'])
def cancel_task(task_id):
    # クライアントIPアドレスを取得
    client_ip = get_remote_address()

    if task_id in active_tasks.keys():
        task = active_tasks[task_id]
        # タスクの所有者を確認(IPアドレスで簡易的に判断)
        if task.client_ip != client_ip:
            return jsonify({'error': 'Unauthorized to cancel this task'}), 403
        task.cancel_flag = True
        if task_id in task_futures.keys():
            task_futures[task_id].cancel()
            del task_futures[task_id]
        del active_tasks[task_id]
        # タスク数をデクリメント
        tasks_per_client[client_ip] = tasks_per_client.get(client_ip, 0) - 1
        update_queue_status('Task cancelled')
        return jsonify({'message': 'Task cancellation requested'})
    else:
        for task in list(task_queue.queue):
            if task.task_id == task_id and task.client_ip == client_ip:
                task.cancel_flag = True
                # タスク数をデクリメント
                tasks_per_client[client_ip] = tasks_per_client.get(client_ip, 0) - 1
                return jsonify({'message': 'Task cancellation requested for queued task'})
        return jsonify({'error': 'Task not found'}), 404

@app.route('/task_status/<task_id>', methods=['GET'])
def task_status(task_id):
    try:
        if task_id in active_tasks.keys():
            task = active_tasks[task_id]
            return jsonify({'task_id': task_id, 'is_processing': task.is_processing})
        else:
            return jsonify({'task_id': task_id, 'is_processing': False})
    except Exception as e:
        return jsonify({'error': str(e)}), 500

def get_active_task_order(task_id):
    try:
        if task_id not in active_tasks.keys():
            return 0
        if active_tasks[task_id].is_processing:
            return 0
        processing_task_ids = [tid for tid, task in active_tasks.items() if task.is_processing]
        non_processing_task_ids = [tid for tid, task in active_tasks.items() if not task.is_processing]
        if len(processing_task_ids) == 0:
            task_order = 0
        else:
            task_order = non_processing_task_ids.index(task_id) + 1
        return task_order
    except Exception as e:
        print(f"Error getting task order: {str(e)}")

# get_task_orderイベントハンドラー
@app.route('/get_task_order/<task_id>', methods=['GET'])
def handle_get_task_order(task_id):
    if task_id  in active_tasks.keys():
        return jsonify({'task_order': get_active_task_order(task_id)})
    else:
        return jsonify({'task_order': 0})

# Flaskルート
# ルートパスのGETリクエストに対するハンドラ
@app.route('/', methods=['GET'])
def root():
    return render_template("index.html")

# process_refined のエンドポイント
@app.route('/process_refined', methods=['POST'])
def process_refined():
    file = request.files['file']
    weight1 = float(request.form.get('weight1', 0.4))
    weight2 = float(request.form.get('weight2', 0.3))
    
    image = ensure_rgb(Image.open(file.stream))
    sotai_image, sketch_image = process_image_as_base64(image, "refine", weight1, weight2)
    
    return jsonify({
        'sotai_image': sotai_image,
        'sketch_image': sketch_image
    })

@app.route('/process_original', methods=['POST'])
def process_original():
    file = request.files['file']
    
    image = ensure_rgb(Image.open(file.stream))
    sotai_image, sketch_image = process_image_as_base64(image, "original")
    
    return jsonify({
        'sotai_image': sotai_image,
        'sketch_image': sketch_image
    })

@app.route('/process_sketch', methods=['POST'])
def process_sketch():
    file = request.files['file']
    
    image = ensure_rgb(Image.open(file.stream))
    sotai_image, sketch_image = process_image_as_base64(image, "sketch")
    
    return jsonify({
        'sotai_image': sotai_image,
        'sketch_image': sketch_image
    })

# グローバルエラーハンドラー
@app.errorhandler(Exception)
def handle_exception(e):
    # ログにエラーを記録
    app.logger.error(f"Unhandled exception: {str(e)}")
    return jsonify({'error': 'An unexpected error occurred'}), 500

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Server options.')
    parser.add_argument('--use_local', action='store_true', help='Use local model')
    parser.add_argument('--use_gpu', action='store_true', help='Set to True to use GPU but if not available, it will use CPU')
    parser.add_argument('--use_dotenv', action='store_true', help='Use .env file for environment variables')

    args = parser.parse_args()
    
    initialize(args.use_local, args.use_gpu, args.use_dotenv)

    port = int(os.environ.get('PORT', 7860))
    server = pywsgi.WSGIServer(('0.0.0.0', port), app, handler_class=WebSocketHandler)
    server.serve_forever()