Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,17 @@
|
|
1 |
-
# ブラーの有効/無効をトグルで制御する変数
|
2 |
-
blur_enabled = True
|
3 |
-
# ブラーのトグル用のキー
|
4 |
-
blur_toggle_key = "blur_toggle_unique_key_for_sidebar"
|
5 |
-
|
6 |
-
print("[1/7] Importing streamlit...", end=" ", flush=True)
|
7 |
-
|
8 |
import streamlit as st
|
9 |
-
print("done")
|
10 |
-
print("[2/7] Importing something...", end=" ", flush=True)
|
11 |
import os
|
12 |
import pandas as pd
|
13 |
from datetime import datetime
|
14 |
-
from PIL import Image
|
15 |
-
from PIL import ImageFilter
|
16 |
from io import BytesIO
|
17 |
import zipfile
|
18 |
import base64
|
19 |
-
print("done")
|
20 |
-
print("[3/7] Importing deepdanbooru...", end=" ", flush=True)
|
21 |
-
|
22 |
import deepdanbooru as dd
|
23 |
-
print("done")
|
24 |
-
print("[4/7] Importing huggingface_hub...", end=" ", flush=True)
|
25 |
-
|
26 |
import huggingface_hub
|
27 |
-
print("done")
|
28 |
-
print("[5/7] Importing tensorflow...", end=" ", flush=True)
|
29 |
-
|
30 |
import tensorflow as tf
|
31 |
-
print("done")
|
32 |
-
print("[6/7] Importing numpy...", end=" ", flush=True)
|
33 |
-
|
34 |
import numpy as np
|
35 |
-
print("done")
|
36 |
-
print("[6/7] Importing transformers...", end=" ", flush=True)
|
37 |
-
|
38 |
from transformers import pipeline
|
39 |
-
|
40 |
# ページごとの表示数
|
41 |
PAGE_SIZE = 20
|
42 |
|
@@ -46,6 +21,11 @@ photos_folder = "photos"
|
|
46 |
# インデックスファイルのパス
|
47 |
index_file_path = "index.csv"
|
48 |
|
|
|
|
|
|
|
|
|
|
|
49 |
# タグ付け関数
|
50 |
def predict_tags(image: Image.Image, score_threshold: float) -> tuple[dict[str, float], dict[str, float], str]:
|
51 |
_, height, width, _ = model.input_shape
|
@@ -92,6 +72,10 @@ def load_data():
|
|
92 |
else:
|
93 |
return pd.DataFrame(columns=["File Name", "Timestamp", "Tags"])
|
94 |
|
|
|
|
|
|
|
|
|
95 |
# アップロードされた写真を保存する関数
|
96 |
def save_uploaded_photo(uploaded_photo, file_name):
|
97 |
if not os.path.exists(photos_folder):
|
@@ -101,61 +85,16 @@ def save_uploaded_photo(uploaded_photo, file_name):
|
|
101 |
image = Image.open(uploaded_photo)
|
102 |
|
103 |
# ブラーの有効/無効をトグルで制御
|
104 |
-
blur_enabled =
|
105 |
|
106 |
# ブラーの適用
|
107 |
image = blur_image(image, blur_enabled, 0.75)
|
108 |
|
109 |
image.save(os.path.join(photos_folder, file_name), "PNG")
|
110 |
|
111 |
-
# アップロードされた写真を表示する関数
|
112 |
-
def display_photos(photos):
|
113 |
-
for photo_info in photos.iterrows():
|
114 |
-
row = photo_info[1]
|
115 |
-
photo_path = os.path.join(photos_folder, row["File Name"])
|
116 |
-
image = Image.open(photo_path)
|
117 |
-
|
118 |
-
# タグを予測して表示
|
119 |
-
result_threshold, result_all, result_text = predict_tags(image, 0.7)
|
120 |
-
|
121 |
-
st.image(image, caption=row["File Name"], use_column_width=True)
|
122 |
-
|
123 |
-
# タグを表示
|
124 |
-
st.write("タグ:", ", ".join(result_all.keys()))
|
125 |
-
|
126 |
-
# フォルダーの中身を zip ファイルとしてダウンロード
|
127 |
-
def download_photos_as_zip(file_paths):
|
128 |
-
# Zip ファイル作成
|
129 |
-
with BytesIO() as zip_buffer:
|
130 |
-
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED) as zip_file:
|
131 |
-
for file_path in file_paths:
|
132 |
-
zip_file.write(file_path, os.path.basename(file_path))
|
133 |
-
|
134 |
-
# ダウンロードリンク表示
|
135 |
-
st.markdown(
|
136 |
-
f"**[ダウンロード ZIPファイル](data:application/zip;base64,{base64.b64encode(zip_buffer.getvalue()).decode()})**",
|
137 |
-
unsafe_allow_html=True
|
138 |
-
)
|
139 |
-
|
140 |
-
# モデルとラベルをダウンロードする関数
|
141 |
-
def load_model():
|
142 |
-
path = huggingface_hub.hf_hub_download('public-data/DeepDanbooru', 'model-resnet_custom_v3.h5')
|
143 |
-
model = tf.keras.models.load_model(path)
|
144 |
-
return model
|
145 |
-
|
146 |
-
def load_labels():
|
147 |
-
path = huggingface_hub.hf_hub_download('public-data/DeepDanbooru', 'tags.txt')
|
148 |
-
with open(path) as f:
|
149 |
-
labels = [line.strip() for line in f.readlines()]
|
150 |
-
return labels
|
151 |
-
|
152 |
# Streamlit アプリケーションのメイン部分
|
153 |
def main():
|
154 |
st.sidebar.title("アップロードオプション")
|
155 |
-
|
156 |
-
# ブラーの有効/無効をトグルで制御
|
157 |
-
blur_enabled = st.sidebar.checkbox("NSFW画像にブラーをかける", value=True, key=blur_toggle_key)
|
158 |
-
|
159 |
uploaded_photos = st.sidebar.file_uploader("写真をアップロードしてください", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
160 |
|
161 |
# モデルとラベルをダウンロードする
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import os
|
3 |
import pandas as pd
|
4 |
from datetime import datetime
|
5 |
+
from PIL import Image, ImageFilter
|
|
|
6 |
from io import BytesIO
|
7 |
import zipfile
|
8 |
import base64
|
|
|
|
|
|
|
9 |
import deepdanbooru as dd
|
|
|
|
|
|
|
10 |
import huggingface_hub
|
|
|
|
|
|
|
11 |
import tensorflow as tf
|
|
|
|
|
|
|
12 |
import numpy as np
|
|
|
|
|
|
|
13 |
from transformers import pipeline
|
14 |
+
|
15 |
# ページごとの表示数
|
16 |
PAGE_SIZE = 20
|
17 |
|
|
|
21 |
# インデックスファイルのパス
|
22 |
index_file_path = "index.csv"
|
23 |
|
24 |
+
# ブラーのトグル用のキー
|
25 |
+
import random
|
26 |
+
import string
|
27 |
+
blur_toggle_key = f"blur_toggle_{ ''.join(random.choices(string.ascii_lowercase, k=5)) }"
|
28 |
+
|
29 |
# タグ付け関数
|
30 |
def predict_tags(image: Image.Image, score_threshold: float) -> tuple[dict[str, float], dict[str, float], str]:
|
31 |
_, height, width, _ = model.input_shape
|
|
|
72 |
else:
|
73 |
return pd.DataFrame(columns=["File Name", "Timestamp", "Tags"])
|
74 |
|
75 |
+
# ブラーの有効/無効を取得する関数
|
76 |
+
def get_blur_enabled():
|
77 |
+
return st.sidebar.checkbox("NSFW画像にブラーをかける", value=True, key=blur_toggle_key)
|
78 |
+
|
79 |
# アップロードされた写真を保存する関数
|
80 |
def save_uploaded_photo(uploaded_photo, file_name):
|
81 |
if not os.path.exists(photos_folder):
|
|
|
85 |
image = Image.open(uploaded_photo)
|
86 |
|
87 |
# ブラーの有効/無効をトグルで制御
|
88 |
+
blur_enabled = get_blur_enabled()
|
89 |
|
90 |
# ブラーの適用
|
91 |
image = blur_image(image, blur_enabled, 0.75)
|
92 |
|
93 |
image.save(os.path.join(photos_folder, file_name), "PNG")
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
# Streamlit アプリケーションのメイン部分
|
96 |
def main():
|
97 |
st.sidebar.title("アップロードオプション")
|
|
|
|
|
|
|
|
|
98 |
uploaded_photos = st.sidebar.file_uploader("写真をアップロードしてください", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
99 |
|
100 |
# モデルとラベルをダウンロードする
|