Spaces:
Running
on
Zero
Running
on
Zero
Dockerfile(redis), model.py(dir), requirements.txt(redis,flask-limiter), app.py(bug fix)
Browse files- Dockerfile +7 -2
- app.py +3 -3
- requirements.txt +2 -0
- scripts/model.py +0 -4
Dockerfile
CHANGED
@@ -16,6 +16,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
16 |
python3.10 \
|
17 |
python3.10-dev \
|
18 |
python3.10-distutils \
|
|
|
19 |
&& ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
|
20 |
&& apt-get clean \
|
21 |
&& rm -rf /var/lib/apt/lists/*
|
@@ -37,11 +38,15 @@ RUN pip install --no-cache-dir -r requirements.txt \
|
|
37 |
# アプリケーションのコピー
|
38 |
COPY . /app
|
39 |
|
|
|
|
|
|
|
|
|
40 |
# 非rootユーザーの作成と切り替え
|
41 |
RUN useradd -m appuser && chown -R appuser:appuser /app
|
42 |
USER appuser
|
43 |
|
44 |
EXPOSE 80
|
45 |
|
46 |
-
#
|
47 |
-
CMD ["
|
|
|
16 |
python3.10 \
|
17 |
python3.10-dev \
|
18 |
python3.10-distutils \
|
19 |
+
redis-server \
|
20 |
&& ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
|
21 |
&& apt-get clean \
|
22 |
&& rm -rf /var/lib/apt/lists/*
|
|
|
38 |
# アプリケーションのコピー
|
39 |
COPY . /app
|
40 |
|
41 |
+
# Redis の起動スクリプトを作成
|
42 |
+
RUN echo '#!/bin/bash\nredis-server --daemonize yes\npython app.py' > /app/start.sh \
|
43 |
+
&& chmod +x /app/start.sh
|
44 |
+
|
45 |
# 非rootユーザーの作成と切り替え
|
46 |
RUN useradd -m appuser && chown -R appuser:appuser /app
|
47 |
USER appuser
|
48 |
|
49 |
EXPOSE 80
|
50 |
|
51 |
+
# 起動コマンドを変更
|
52 |
+
CMD ["/app/start.sh"]
|
app.py
CHANGED
@@ -30,8 +30,8 @@ redis_client = redis.Redis(host='localhost', port=6379, db=0)
|
|
30 |
|
31 |
# レート制限の設定
|
32 |
limiter = Limiter(
|
33 |
-
|
34 |
-
|
35 |
default_limits=["200 per day", "50 per hour"]
|
36 |
)
|
37 |
|
@@ -189,7 +189,7 @@ def handle_get_task_order(task_id):
|
|
189 |
return jsonify({'task_order': task_order})
|
190 |
|
191 |
@socketio.on('connect')
|
192 |
-
def handle_connect():
|
193 |
# クライアント接続数の制限
|
194 |
if redis_client.get('connected_clients') and int(redis_client.get('connected_clients')) > 100:
|
195 |
return False # 接続を拒否
|
|
|
30 |
|
31 |
# レート制限の設定
|
32 |
limiter = Limiter(
|
33 |
+
get_remote_address,
|
34 |
+
app=app,
|
35 |
default_limits=["200 per day", "50 per hour"]
|
36 |
)
|
37 |
|
|
|
189 |
return jsonify({'task_order': task_order})
|
190 |
|
191 |
@socketio.on('connect')
|
192 |
+
def handle_connect(auth):
|
193 |
# クライアント接続数の制限
|
194 |
if redis_client.get('connected_clients') and int(redis_client.get('connected_clients')) > 100:
|
195 |
return False # 接続を拒否
|
requirements.txt
CHANGED
@@ -19,3 +19,5 @@ tokenizers
|
|
19 |
pytorch_lightning
|
20 |
python-dotenv
|
21 |
peft==0.11.1
|
|
|
|
|
|
19 |
pytorch_lightning
|
20 |
python-dotenv
|
21 |
peft==0.11.1
|
22 |
+
flask_limiter==3.7.0
|
23 |
+
redis==5.0.7
|
scripts/model.py
CHANGED
@@ -153,8 +153,6 @@ def create_model(model, use_local):
|
|
153 |
net = UnetGenerator(3, 1, 8, 64, norm_layer=norm_layer, use_dropout=False)
|
154 |
|
155 |
import os
|
156 |
-
cwd = os.getcwd() # 現在のディレクトリを保存
|
157 |
-
os.chdir(os.path.dirname(__file__)) # このファイルのディレクトリに移動
|
158 |
if model == 'default':
|
159 |
model_path = (lambda filename, subfolder: os.path.join(subfolder, filename) if use_local else download_file(filename, subfolder)) \
|
160 |
("netG.pth", "models/Anime2Sketch")
|
@@ -167,8 +165,6 @@ def create_model(model, use_local):
|
|
167 |
del ckpt[key]
|
168 |
net.load_state_dict(ckpt)
|
169 |
|
170 |
-
os.chdir(cwd) # 元のディレクトリに戻る
|
171 |
-
|
172 |
elif model == 'improved':
|
173 |
ckpt = torch.load('weights/improved.bin', map_location=torch.device('cpu'))
|
174 |
base = net.model.model[1]
|
|
|
153 |
net = UnetGenerator(3, 1, 8, 64, norm_layer=norm_layer, use_dropout=False)
|
154 |
|
155 |
import os
|
|
|
|
|
156 |
if model == 'default':
|
157 |
model_path = (lambda filename, subfolder: os.path.join(subfolder, filename) if use_local else download_file(filename, subfolder)) \
|
158 |
("netG.pth", "models/Anime2Sketch")
|
|
|
165 |
del ckpt[key]
|
166 |
net.load_state_dict(ckpt)
|
167 |
|
|
|
|
|
168 |
elif model == 'improved':
|
169 |
ckpt = torch.load('weights/improved.bin', map_location=torch.device('cpu'))
|
170 |
base = net.model.model[1]
|