Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,12 @@ import os
|
|
| 3 |
import zipfile
|
| 4 |
import csv
|
| 5 |
from PIL import Image, ImageFilter
|
| 6 |
-
from gradio_client import Client
|
| 7 |
import base64
|
| 8 |
import datetime
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# Initialize Gradio client
|
| 11 |
client_nsfw = Client("https://ozoneasai-falconsai-nsfw-image-detection.hf.space/--replicas/0fyhj/")
|
| 12 |
|
|
@@ -16,7 +18,11 @@ if 'page' not in st.session_state:
|
|
| 16 |
|
| 17 |
# Initialize blur option as a toggle
|
| 18 |
if 'blur_option' not in st.session_state:
|
| 19 |
-
st.session_state.blur_option = st.checkbox("NSFW画像にBlurをかける", value=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Lists to store Gradio predictions, good values, timestamps, and file paths
|
| 22 |
gradio_nsfw_predictions = []
|
|
@@ -24,12 +30,7 @@ good_values = []
|
|
| 24 |
timestamps = []
|
| 25 |
file_paths = []
|
| 26 |
|
| 27 |
-
# Function to
|
| 28 |
-
def get_gradio_nsfw_prediction(image_path):
|
| 29 |
-
result = client_nsfw.predict(image_path, api_name="/predict")
|
| 30 |
-
return result # Return the entire dictionary
|
| 31 |
-
|
| 32 |
-
# Define the function to generate a download link for a file
|
| 33 |
def get_binary_file_downloader_html(file_path, label="Download"):
|
| 34 |
with open(file_path, 'rb') as f:
|
| 35 |
data = f.read()
|
|
@@ -37,6 +38,11 @@ def get_binary_file_downloader_html(file_path, label="Download"):
|
|
| 37 |
href = f'<a href="data:file/csv;base64,{b64}" download="{os.path.basename(file_path)}">{label}</a>'
|
| 38 |
return href
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Function to save uploaded files and get Gradio predictions
|
| 41 |
def save_uploaded_files(uploaded_files):
|
| 42 |
if not os.path.exists("temp"):
|
|
@@ -47,20 +53,21 @@ def save_uploaded_files(uploaded_files):
|
|
| 47 |
with open(file_path, "wb") as f:
|
| 48 |
f.write(uploaded_file.getbuffer())
|
| 49 |
|
| 50 |
-
# Get Gradio
|
| 51 |
gradio_nsfw_prediction = get_gradio_nsfw_prediction(file_path)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
if "good" in gradio_nsfw_prediction and "confidences" in gradio_nsfw_prediction["good"]:
|
| 56 |
confidences = gradio_nsfw_prediction["good"]["confidences"]
|
| 57 |
for label_confidence in confidences:
|
| 58 |
if label_confidence["label"] == "good":
|
| 59 |
-
|
| 60 |
break
|
| 61 |
|
| 62 |
-
|
| 63 |
-
good_values.append(good_value_confidence)
|
| 64 |
|
| 65 |
# Get timestamp
|
| 66 |
timestamp = datetime.datetime.now()
|
|
@@ -74,7 +81,7 @@ def apply_blur(image_path):
|
|
| 74 |
img = img.filter(ImageFilter.GaussianBlur(radius=5))
|
| 75 |
return img
|
| 76 |
|
| 77 |
-
# Function to paginate files
|
| 78 |
def paginate_files(files, page, files_per_page):
|
| 79 |
start_index = (page - 1) * files_per_page
|
| 80 |
end_index = start_index + files_per_page
|
|
@@ -94,7 +101,7 @@ def display_images(images):
|
|
| 94 |
st.write(f"**Good value:** {good_values[i]}")
|
| 95 |
st.write(f"**Timestamp:** {timestamp}")
|
| 96 |
|
| 97 |
-
if st.session_state.blur_option and gradio_nsfw_prediction.
|
| 98 |
blurred_img = apply_blur(file_path)
|
| 99 |
st.image(blurred_img, caption=os.path.basename(file_path), use_column_width=True)
|
| 100 |
else:
|
|
@@ -109,13 +116,9 @@ def display_images(images):
|
|
| 109 |
os.remove(file_path)
|
| 110 |
st.experimental_rerun()
|
| 111 |
|
| 112 |
-
# Main part of the
|
| 113 |
-
st.
|
| 114 |
-
|
| 115 |
-
uploaded_files = st.file_uploader("ファイルをアップロードしてください", type=["jpg", "png"], accept_multiple_files=True)
|
| 116 |
-
|
| 117 |
-
if uploaded_files:
|
| 118 |
-
save_uploaded_files(uploaded_files)
|
| 119 |
|
| 120 |
files_per_page = 20
|
| 121 |
num_pages = (len(file_paths) - 1) // files_per_page + 1
|
|
@@ -142,6 +145,9 @@ st.write(f"現在のページ: {st.session_state.page}")
|
|
| 142 |
|
| 143 |
current_page_files = paginate_files(file_paths, st.session_state.page, files_per_page)
|
| 144 |
|
|
|
|
|
|
|
|
|
|
| 145 |
# Display images with predictions and options
|
| 146 |
display_images(current_page_files)
|
| 147 |
|
|
@@ -155,7 +161,10 @@ if st.button("CSVに保存"):
|
|
| 155 |
csv_writer = csv.writer(csv_file)
|
| 156 |
csv_writer.writerow(['ファイル名', 'NSFWの予測', 'Goodの値', '作成時間'])
|
| 157 |
for i, file_path in enumerate(file_paths):
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
st.markdown(get_binary_file_downloader_html(csv_filename, label="CSVをダウンロード"), unsafe_allow_html=True)
|
| 161 |
|
|
|
|
| 3 |
import zipfile
|
| 4 |
import csv
|
| 5 |
from PIL import Image, ImageFilter
|
|
|
|
| 6 |
import base64
|
| 7 |
import datetime
|
| 8 |
|
| 9 |
+
if 'blur_option' not in st.session_state:
|
| 10 |
+
st.session_state.blur_option = True
|
| 11 |
+
|
| 12 |
# Initialize Gradio client
|
| 13 |
client_nsfw = Client("https://ozoneasai-falconsai-nsfw-image-detection.hf.space/--replicas/0fyhj/")
|
| 14 |
|
|
|
|
| 18 |
|
| 19 |
# Initialize blur option as a toggle
|
| 20 |
if 'blur_option' not in st.session_state:
|
| 21 |
+
st.session_state.blur_option = st.checkbox("NSFW画像にBlurをかける", value=st.session_state.blur_option, key="blur_toggle")
|
| 22 |
+
|
| 23 |
+
# Initialize sort options
|
| 24 |
+
if 'sort_option' not in st.session_state:
|
| 25 |
+
st.session_state.sort_option = {"column": "timestamp", "ascending": True}
|
| 26 |
|
| 27 |
# Lists to store Gradio predictions, good values, timestamps, and file paths
|
| 28 |
gradio_nsfw_predictions = []
|
|
|
|
| 30 |
timestamps = []
|
| 31 |
file_paths = []
|
| 32 |
|
| 33 |
+
# Function to generate a download link for a file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def get_binary_file_downloader_html(file_path, label="Download"):
|
| 35 |
with open(file_path, 'rb') as f:
|
| 36 |
data = f.read()
|
|
|
|
| 38 |
href = f'<a href="data:file/csv;base64,{b64}" download="{os.path.basename(file_path)}">{label}</a>'
|
| 39 |
return href
|
| 40 |
|
| 41 |
+
# Function to get Gradio NSFW prediction
|
| 42 |
+
def get_gradio_nsfw_prediction(image_path):
|
| 43 |
+
result = client_nsfw.predict(image_path, api_name="/predict")
|
| 44 |
+
return result["label"] # Assuming the label is the prediction result
|
| 45 |
+
|
| 46 |
# Function to save uploaded files and get Gradio predictions
|
| 47 |
def save_uploaded_files(uploaded_files):
|
| 48 |
if not os.path.exists("temp"):
|
|
|
|
| 53 |
with open(file_path, "wb") as f:
|
| 54 |
f.write(uploaded_file.getbuffer())
|
| 55 |
|
| 56 |
+
# Get Gradio predictions
|
| 57 |
gradio_nsfw_prediction = get_gradio_nsfw_prediction(file_path)
|
| 58 |
|
| 59 |
+
gradio_nsfw_predictions.append((file_path, gradio_nsfw_prediction))
|
| 60 |
+
|
| 61 |
+
# Get good value
|
| 62 |
+
good_value = None
|
| 63 |
if "good" in gradio_nsfw_prediction and "confidences" in gradio_nsfw_prediction["good"]:
|
| 64 |
confidences = gradio_nsfw_prediction["good"]["confidences"]
|
| 65 |
for label_confidence in confidences:
|
| 66 |
if label_confidence["label"] == "good":
|
| 67 |
+
good_value = label_confidence["confidence"]
|
| 68 |
break
|
| 69 |
|
| 70 |
+
good_values.append(good_value)
|
|
|
|
| 71 |
|
| 72 |
# Get timestamp
|
| 73 |
timestamp = datetime.datetime.now()
|
|
|
|
| 81 |
img = img.filter(ImageFilter.GaussianBlur(radius=5))
|
| 82 |
return img
|
| 83 |
|
| 84 |
+
# Function to paginate files
|
| 85 |
def paginate_files(files, page, files_per_page):
|
| 86 |
start_index = (page - 1) * files_per_page
|
| 87 |
end_index = start_index + files_per_page
|
|
|
|
| 101 |
st.write(f"**Good value:** {good_values[i]}")
|
| 102 |
st.write(f"**Timestamp:** {timestamp}")
|
| 103 |
|
| 104 |
+
if st.session_state.blur_option and gradio_nsfw_prediction.lower() == "nsfw":
|
| 105 |
blurred_img = apply_blur(file_path)
|
| 106 |
st.image(blurred_img, caption=os.path.basename(file_path), use_column_width=True)
|
| 107 |
else:
|
|
|
|
| 116 |
os.remove(file_path)
|
| 117 |
st.experimental_rerun()
|
| 118 |
|
| 119 |
+
# Main part of the app
|
| 120 |
+
if 'uploaded_files' in st.session_state:
|
| 121 |
+
save_uploaded_files(st.session_state.uploaded_files)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
files_per_page = 20
|
| 124 |
num_pages = (len(file_paths) - 1) // files_per_page + 1
|
|
|
|
| 145 |
|
| 146 |
current_page_files = paginate_files(file_paths, st.session_state.page, files_per_page)
|
| 147 |
|
| 148 |
+
st.write("ファイル一覧:")
|
| 149 |
+
st.write(current_page_files)
|
| 150 |
+
|
| 151 |
# Display images with predictions and options
|
| 152 |
display_images(current_page_files)
|
| 153 |
|
|
|
|
| 161 |
csv_writer = csv.writer(csv_file)
|
| 162 |
csv_writer.writerow(['ファイル名', 'NSFWの予測', 'Goodの値', '作成時間'])
|
| 163 |
for i, file_path in enumerate(file_paths):
|
| 164 |
+
gradio_nsfw_prediction = gradio_nsfw_predictions[i]
|
| 165 |
+
good_value = good_values[i]
|
| 166 |
+
timestamp = timestamps[i]
|
| 167 |
+
csv_writer.writerow([os.path.basename(file_path), gradio_nsfw_prediction, good_value, timestamp])
|
| 168 |
|
| 169 |
st.markdown(get_binary_file_downloader_html(csv_filename, label="CSVをダウンロード"), unsafe_allow_html=True)
|
| 170 |
|