Spaces:
Runtime error
Runtime error
socketio
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from flask import Flask, request as
|
2 |
from flask_socketio import SocketIO, join_room, leave_room, close_room, rooms, disconnect
|
3 |
from flask_cors import CORS
|
4 |
from flask_limiter import Limiter
|
@@ -59,7 +59,7 @@ def update_queue_status(message):
|
|
59 |
|
60 |
def process_task(task):
|
61 |
try:
|
62 |
-
client_id =
|
63 |
task.is_processing = True
|
64 |
# ファイルデータをPIL Imageに変換
|
65 |
image = Image.open(io.BytesIO(task.file_data))
|
@@ -118,14 +118,14 @@ connected_clients = 0
|
|
118 |
tasks_per_client = {}
|
119 |
@socketio.on('connect')
|
120 |
def handle_connect(auth):
|
121 |
-
client_id =
|
122 |
join_room(client_id) # クライアントを自身のルームに入れる
|
123 |
global connected_clients
|
124 |
connected_clients += 1
|
125 |
|
126 |
@socketio.on('disconnect')
|
127 |
def handle_disconnect():
|
128 |
-
client_id =
|
129 |
leave_room(client_id) # クライアントをルームから出す
|
130 |
global connected_clients
|
131 |
connected_clients -= 1
|
@@ -148,17 +148,17 @@ def submit_task():
|
|
148 |
|
149 |
# クライアントIPアドレスを取得
|
150 |
client_ip = get_remote_address()
|
151 |
-
client_id =
|
152 |
|
153 |
# 同一IPからの同時タスク数を制限
|
154 |
if tasks_per_client.get(client_ip, 0) >= 2:
|
155 |
return jsonify({'error': 'Maximum number of concurrent tasks reached'}), 429
|
156 |
|
157 |
task_id = str(uuid.uuid4())
|
158 |
-
file =
|
159 |
-
mode =
|
160 |
-
weight1 = float(
|
161 |
-
weight2 = float(
|
162 |
|
163 |
# ファイルタイプの制限
|
164 |
allowed_extensions = {'png', 'jpg', 'jpeg', 'gif'}
|
@@ -235,9 +235,9 @@ def root():
|
|
235 |
# process_refined のエンドポイント
|
236 |
@app.route('/process_refined', methods=['POST'])
|
237 |
def process_refined():
|
238 |
-
file =
|
239 |
-
weight1 = float(
|
240 |
-
weight2 = float(
|
241 |
|
242 |
image = ensure_rgb(Image.open(file.stream))
|
243 |
sotai_image, sketch_image = process_image_as_base64(image, "refine", weight1, weight2)
|
@@ -249,7 +249,7 @@ def process_refined():
|
|
249 |
|
250 |
@app.route('/process_original', methods=['POST'])
|
251 |
def process_original():
|
252 |
-
file =
|
253 |
|
254 |
image = ensure_rgb(Image.open(file.stream))
|
255 |
sotai_image, sketch_image = process_image_as_base64(image, "original")
|
@@ -261,7 +261,7 @@ def process_original():
|
|
261 |
|
262 |
@app.route('/process_sketch', methods=['POST'])
|
263 |
def process_sketch():
|
264 |
-
file =
|
265 |
|
266 |
image = ensure_rgb(Image.open(file.stream))
|
267 |
sotai_image, sketch_image = process_image_as_base64(image, "sketch")
|
|
|
1 |
+
from flask import Flask, request as socketio, render_template, send_file, jsonify, send_from_directory
|
2 |
from flask_socketio import SocketIO, join_room, leave_room, close_room, rooms, disconnect
|
3 |
from flask_cors import CORS
|
4 |
from flask_limiter import Limiter
|
|
|
59 |
|
60 |
def process_task(task):
|
61 |
try:
|
62 |
+
client_id = socketio.sid
|
63 |
task.is_processing = True
|
64 |
# ファイルデータをPIL Imageに変換
|
65 |
image = Image.open(io.BytesIO(task.file_data))
|
|
|
118 |
tasks_per_client = {}
|
119 |
@socketio.on('connect')
|
120 |
def handle_connect(auth):
|
121 |
+
client_id = socketio.sid # クライアントIDを取得
|
122 |
join_room(client_id) # クライアントを自身のルームに入れる
|
123 |
global connected_clients
|
124 |
connected_clients += 1
|
125 |
|
126 |
@socketio.on('disconnect')
|
127 |
def handle_disconnect():
|
128 |
+
client_id = socketio.sid # クライアントIDを取得
|
129 |
leave_room(client_id) # クライアントをルームから出す
|
130 |
global connected_clients
|
131 |
connected_clients -= 1
|
|
|
148 |
|
149 |
# クライアントIPアドレスを取得
|
150 |
client_ip = get_remote_address()
|
151 |
+
client_id = socketio.sid # クライアントIDを取得
|
152 |
|
153 |
# 同一IPからの同時タスク数を制限
|
154 |
if tasks_per_client.get(client_ip, 0) >= 2:
|
155 |
return jsonify({'error': 'Maximum number of concurrent tasks reached'}), 429
|
156 |
|
157 |
task_id = str(uuid.uuid4())
|
158 |
+
file = socketio.files['file']
|
159 |
+
mode = socketio.form.get('mode', 'refine')
|
160 |
+
weight1 = float(socketio.form.get('weight1', 0.4))
|
161 |
+
weight2 = float(socketio.form.get('weight2', 0.3))
|
162 |
|
163 |
# ファイルタイプの制限
|
164 |
allowed_extensions = {'png', 'jpg', 'jpeg', 'gif'}
|
|
|
235 |
# process_refined のエンドポイント
|
236 |
@app.route('/process_refined', methods=['POST'])
|
237 |
def process_refined():
|
238 |
+
file = socketio.files['file']
|
239 |
+
weight1 = float(socketio.form.get('weight1', 0.4))
|
240 |
+
weight2 = float(socketio.form.get('weight2', 0.3))
|
241 |
|
242 |
image = ensure_rgb(Image.open(file.stream))
|
243 |
sotai_image, sketch_image = process_image_as_base64(image, "refine", weight1, weight2)
|
|
|
249 |
|
250 |
@app.route('/process_original', methods=['POST'])
|
251 |
def process_original():
|
252 |
+
file = socketio.files['file']
|
253 |
|
254 |
image = ensure_rgb(Image.open(file.stream))
|
255 |
sotai_image, sketch_image = process_image_as_base64(image, "original")
|
|
|
261 |
|
262 |
@app.route('/process_sketch', methods=['POST'])
|
263 |
def process_sketch():
|
264 |
+
file = socketio.files['file']
|
265 |
|
266 |
image = ensure_rgb(Image.open(file.stream))
|
267 |
sotai_image, sketch_image = process_image_as_base64(image, "sketch")
|