Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import zipfile
|
|
|
|
|
|
|
| 4 |
from PIL import Image, ImageFilter
|
| 5 |
from gradio_client import Client
|
|
|
|
| 6 |
|
| 7 |
# Initialize Gradio client
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
# Initialize pagination
|
| 11 |
if "page" not in st.session_state:
|
|
@@ -15,19 +19,42 @@ if "page" not in st.session_state:
|
|
| 15 |
if "blur_option" not in st.session_state:
|
| 16 |
st.session_state.blur_option = True
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
st.title("ファイルアップロードと共有")
|
| 19 |
|
| 20 |
uploaded_files = st.file_uploader("ファイルをアップロードしてください", type=["jpg", "png"], accept_multiple_files=True)
|
| 21 |
|
| 22 |
# List to store Gradio predictions
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# List to store file paths
|
| 26 |
file_paths = []
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
result =
|
| 30 |
-
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Function to save uploaded files and get Gradio predictions
|
| 33 |
def save_uploaded_files(uploaded_files):
|
|
@@ -39,9 +66,23 @@ def save_uploaded_files(uploaded_files):
|
|
| 39 |
with open(file_path, "wb") as f:
|
| 40 |
f.write(uploaded_file.getbuffer())
|
| 41 |
|
| 42 |
-
# Get Gradio
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
file_paths.append(file_path)
|
| 46 |
|
| 47 |
# Function to apply blur to an image
|
|
@@ -58,14 +99,21 @@ def paginate_files(files, page, files_per_page):
|
|
| 58 |
|
| 59 |
# Display images with pagination
|
| 60 |
def display_images(images, blur_option):
|
| 61 |
-
for file_path in images:
|
| 62 |
file_ext = os.path.splitext(file_path)[1].lower()
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
st.write(f"**Prediction for {os.path.basename(file_path)}:** {
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
if blur_option and
|
| 69 |
# Apply blur if NSFW prediction and blur option is enabled
|
| 70 |
blurred_img = apply_blur(file_path)
|
| 71 |
st.image(blurred_img, caption=os.path.basename(file_path), use_column_width=True)
|
|
@@ -106,15 +154,45 @@ st.write(f"現在のページ: {st.session_state.page}")
|
|
| 106 |
|
| 107 |
# Display images for the current page
|
| 108 |
current_page_files = paginate_files(file_paths, st.session_state.page, files_per_page)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
display_images(current_page_files, st.session_state.blur_option)
|
| 110 |
|
| 111 |
# Blurチェックボックス
|
| 112 |
-
st.checkbox("NSFW画像にBlurをかける", key="blur_option")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# Gradio予測データを保存
|
| 115 |
with open("gradio_predictions.txt", "w") as f:
|
| 116 |
-
for prediction in
|
| 117 |
-
f.write(f"{prediction[0]}: {prediction[1]}\n")
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# Zipファイル作成とダウンロード
|
| 120 |
if st.button("ファイルを一括ダウンロード"):
|
|
@@ -124,6 +202,6 @@ if st.button("ファイルを一括ダウンロード"):
|
|
| 124 |
zipf.write(file_path, os.path.basename(file_path))
|
| 125 |
|
| 126 |
# Zipファイルをリンクとして提供
|
| 127 |
-
st.markdown(get_binary_file_downloader_html(zip_filename), unsafe_allow_html=True)
|
| 128 |
|
| 129 |
st.success("ファイルがアップロードされました。このページのURLを他のクライアントと共有してください。")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import zipfile
|
| 4 |
+
import csv
|
| 5 |
+
import base64 # 追加
|
| 6 |
from PIL import Image, ImageFilter
|
| 7 |
from gradio_client import Client
|
| 8 |
+
import datetime
|
| 9 |
|
| 10 |
# Initialize Gradio client
|
| 11 |
+
client_nsfw = Client("https://ozoneasai-falconsai-nsfw-image-detection.hf.space/--replicas/0fyhj/")
|
| 12 |
+
client_other = Client("https://ozoneasai-sanali209-imclasif-quality-v001.hf.space/--replicas/m6xqz/")
|
| 13 |
|
| 14 |
# Initialize pagination
|
| 15 |
if "page" not in st.session_state:
|
|
|
|
| 19 |
if "blur_option" not in st.session_state:
|
| 20 |
st.session_state.blur_option = True
|
| 21 |
|
| 22 |
+
# Initialize sort options
|
| 23 |
+
if "sort_option" not in st.session_state:
|
| 24 |
+
st.session_state.sort_option = {"column": "timestamp", "ascending": True}
|
| 25 |
+
|
| 26 |
+
# Define the function to generate a download link for a file
|
| 27 |
+
def get_binary_file_downloader_html(file_path, label="Download"):
|
| 28 |
+
with open(file_path, 'rb') as f:
|
| 29 |
+
data = f.read()
|
| 30 |
+
b64 = base64.b64encode(data).decode()
|
| 31 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="{os.path.basename(file_path)}">{label}</a>'
|
| 32 |
+
return href
|
| 33 |
+
|
| 34 |
st.title("ファイルアップロードと共有")
|
| 35 |
|
| 36 |
uploaded_files = st.file_uploader("ファイルをアップロードしてください", type=["jpg", "png"], accept_multiple_files=True)
|
| 37 |
|
| 38 |
# List to store Gradio predictions
|
| 39 |
+
gradio_nsfw_predictions = []
|
| 40 |
+
gradio_other_predictions = []
|
| 41 |
+
|
| 42 |
+
# List to store good values
|
| 43 |
+
good_values = []
|
| 44 |
+
|
| 45 |
+
# List to store timestamps
|
| 46 |
+
timestamps = []
|
| 47 |
|
| 48 |
# List to store file paths
|
| 49 |
file_paths = []
|
| 50 |
|
| 51 |
+
def get_gradio_nsfw_prediction(image_path):
|
| 52 |
+
result = client_nsfw.predict(image_path, api_name="/predict")
|
| 53 |
+
return result["output"] # 追加
|
| 54 |
+
|
| 55 |
+
def get_gradio_other_prediction(image_path):
|
| 56 |
+
result = client_other.predict(image_path, api_name="/predict")
|
| 57 |
+
return result["output"] # 追加
|
| 58 |
|
| 59 |
# Function to save uploaded files and get Gradio predictions
|
| 60 |
def save_uploaded_files(uploaded_files):
|
|
|
|
| 66 |
with open(file_path, "wb") as f:
|
| 67 |
f.write(uploaded_file.getbuffer())
|
| 68 |
|
| 69 |
+
# Get Gradio predictions
|
| 70 |
+
gradio_nsfw_prediction = get_gradio_nsfw_prediction(file_path)
|
| 71 |
+
gradio_other_prediction = get_gradio_other_prediction(file_path)
|
| 72 |
+
|
| 73 |
+
gradio_nsfw_predictions.append((file_path, gradio_nsfw_prediction))
|
| 74 |
+
gradio_other_predictions.append((file_path, gradio_other_prediction))
|
| 75 |
+
|
| 76 |
+
# Get good value
|
| 77 |
+
good_value = None
|
| 78 |
+
if "confidences" in gradio_other_prediction:
|
| 79 |
+
good_value = gradio_other_prediction["confidences"][0]["confidence"]
|
| 80 |
+
good_values.append(good_value)
|
| 81 |
+
|
| 82 |
+
# Get timestamp
|
| 83 |
+
timestamp = datetime.datetime.now()
|
| 84 |
+
timestamps.append(timestamp)
|
| 85 |
+
|
| 86 |
file_paths.append(file_path)
|
| 87 |
|
| 88 |
# Function to apply blur to an image
|
|
|
|
| 99 |
|
| 100 |
# Display images with pagination
|
| 101 |
def display_images(images, blur_option):
|
| 102 |
+
for i, file_path in enumerate(images):
|
| 103 |
file_ext = os.path.splitext(file_path)[1].lower()
|
| 104 |
|
| 105 |
+
gradio_nsfw_prediction = get_gradio_nsfw_prediction(file_path)
|
| 106 |
+
gradio_other_prediction = get_gradio_other_prediction(file_path)
|
| 107 |
+
|
| 108 |
+
good_value = good_values[i]
|
| 109 |
+
timestamp = timestamps[i]
|
| 110 |
|
| 111 |
+
st.write(f"**Prediction for {os.path.basename(file_path)} (NSFW):** {gradio_nsfw_prediction}")
|
| 112 |
+
st.write(f"**Prediction for {os.path.basename(file_path)} (Other):** {gradio_other_prediction}")
|
| 113 |
+
st.write(f"**Good value:** {good_value}")
|
| 114 |
+
st.write(f"**Timestamp:** {timestamp}")
|
| 115 |
|
| 116 |
+
if blur_option and gradio_nsfw_prediction.lower() == "nsfw":
|
| 117 |
# Apply blur if NSFW prediction and blur option is enabled
|
| 118 |
blurred_img = apply_blur(file_path)
|
| 119 |
st.image(blurred_img, caption=os.path.basename(file_path), use_column_width=True)
|
|
|
|
| 154 |
|
| 155 |
# Display images for the current page
|
| 156 |
current_page_files = paginate_files(file_paths, st.session_state.page, files_per_page)
|
| 157 |
+
|
| 158 |
+
# Sort files based on the selected option
|
| 159 |
+
sort_column = st.selectbox("並び替えオプション", ["timestamp", "good"])
|
| 160 |
+
ascending = st.checkbox("昇順", value=st.session_state.sort_option["ascending"])
|
| 161 |
+
st.session_state.sort_option = {"column": sort_column, "ascending": ascending}
|
| 162 |
+
|
| 163 |
+
# Sort files based on the selected option
|
| 164 |
+
if sort_column == "timestamp":
|
| 165 |
+
current_page_files.sort(key=lambda x: timestamps[file_paths.index(x)], reverse=not ascending)
|
| 166 |
+
elif sort_column == "good":
|
| 167 |
+
current_page_files.sort(key=lambda x: good_values[file_paths.index(x)], reverse=not ascending)
|
| 168 |
+
|
| 169 |
display_images(current_page_files, st.session_state.blur_option)
|
| 170 |
|
| 171 |
# Blurチェックボックス
|
| 172 |
+
st.session_state.blur_option = st.checkbox("NSFW画像にBlurをかける", key="blur_option", value=st.session_state.blur_option)
|
| 173 |
+
|
| 174 |
+
# Save Gradio predictions, good values, and timestamps to CSV
|
| 175 |
+
if st.button("CSVに保存"):
|
| 176 |
+
csv_filename = "gradio_predictions_and_good_values.csv"
|
| 177 |
+
with open(csv_filename, mode='w', newline='', encoding='utf-8') as csv_file:
|
| 178 |
+
csv_writer = csv.writer(csv_file)
|
| 179 |
+
csv_writer.writerow(['ファイル名', 'NSFWの予測', 'Otherの予測', 'Goodの値', '作成時間'])
|
| 180 |
+
for i, file_path in enumerate(file_paths):
|
| 181 |
+
gradio_nsfw_prediction = gradio_nsfw_predictions[i]
|
| 182 |
+
gradio_other_prediction = gradio_other_predictions[i]
|
| 183 |
+
good_value = good_values[i]
|
| 184 |
+
timestamp = timestamps[i]
|
| 185 |
+
csv_writer.writerow([os.path.basename(file_path), gradio_nsfw_prediction, gradio_other_prediction, good_value, timestamp])
|
| 186 |
+
|
| 187 |
+
# CSVファイルをリンクとして提供
|
| 188 |
+
st.markdown(get_binary_file_downloader_html(csv_filename, label="CSVをダウンロード"), unsafe_allow_html=True)
|
| 189 |
|
| 190 |
# Gradio予測データを保存
|
| 191 |
with open("gradio_predictions.txt", "w") as f:
|
| 192 |
+
for prediction in gradio_nsfw_predictions:
|
| 193 |
+
f.write(f"{prediction[0]} (NSFW): {prediction[1]}\n")
|
| 194 |
+
for prediction in gradio_other_predictions:
|
| 195 |
+
f.write(f"{prediction[0]} (Other): {prediction[1]}\n")
|
| 196 |
|
| 197 |
# Zipファイル作成とダウンロード
|
| 198 |
if st.button("ファイルを一括ダウンロード"):
|
|
|
|
| 202 |
zipf.write(file_path, os.path.basename(file_path))
|
| 203 |
|
| 204 |
# Zipファイルをリンクとして提供
|
| 205 |
+
st.markdown(get_binary_file_downloader_html(zip_filename, label="Zipファイルをダウンロード"), unsafe_allow_html=True)
|
| 206 |
|
| 207 |
st.success("ファイルがアップロードされました。このページのURLを他のクライアントと共有してください。")
|