Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,6 @@ from gradio_client import Client
|
|
| 7 |
import base64
|
| 8 |
import datetime
|
| 9 |
|
| 10 |
-
if 'blur_option' not in st.session_state:
|
| 11 |
-
st.session_state.blur_option = True
|
| 12 |
-
|
| 13 |
# Initialize Gradio client
|
| 14 |
client_nsfw = Client("https://ozoneasai-falconsai-nsfw-image-detection.hf.space/--replicas/0fyhj/")
|
| 15 |
|
|
@@ -19,7 +16,7 @@ if 'page' not in st.session_state:
|
|
| 19 |
|
| 20 |
# Initialize blur option as a toggle
|
| 21 |
if 'blur_option' not in st.session_state:
|
| 22 |
-
st.session_state.blur_option = st.checkbox("NSFW画像にBlurをかける", value=
|
| 23 |
|
| 24 |
# Lists to store Gradio predictions, good values, timestamps, and file paths
|
| 25 |
gradio_nsfw_predictions = []
|
|
@@ -27,6 +24,11 @@ good_values = []
|
|
| 27 |
timestamps = []
|
| 28 |
file_paths = []
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Define the function to generate a download link for a file
|
| 31 |
def get_binary_file_downloader_html(file_path, label="Download"):
|
| 32 |
with open(file_path, 'rb') as f:
|
|
@@ -35,12 +37,7 @@ def get_binary_file_downloader_html(file_path, label="Download"):
|
|
| 35 |
href = f'<a href="data:file/csv;base64,{b64}" download="{os.path.basename(file_path)}">{label}</a>'
|
| 36 |
return href
|
| 37 |
|
| 38 |
-
# Function to get Gradio
|
| 39 |
-
def get_gradio_nsfw_prediction(image_path):
|
| 40 |
-
result = client_nsfw.predict(image_path, api_name="/predict")
|
| 41 |
-
return result
|
| 42 |
-
|
| 43 |
-
# Inside the save_uploaded_files function, after getting the Gradio predictions
|
| 44 |
def save_uploaded_files(uploaded_files):
|
| 45 |
if not os.path.exists("temp"):
|
| 46 |
os.makedirs("temp")
|
|
@@ -50,7 +47,7 @@ def save_uploaded_files(uploaded_files):
|
|
| 50 |
with open(file_path, "wb") as f:
|
| 51 |
f.write(uploaded_file.getbuffer())
|
| 52 |
|
| 53 |
-
# Get Gradio
|
| 54 |
gradio_nsfw_prediction = get_gradio_nsfw_prediction(file_path)
|
| 55 |
|
| 56 |
# Extract good value confidence
|
|
@@ -90,7 +87,7 @@ def display_images(images):
|
|
| 90 |
|
| 91 |
# Check if index is within the range
|
| 92 |
if i < len(gradio_nsfw_predictions):
|
| 93 |
-
gradio_nsfw_prediction = gradio_nsfw_predictions[i]
|
| 94 |
timestamp = timestamps[i]
|
| 95 |
|
| 96 |
st.write(f"**Prediction for {os.path.basename(file_path)} (NSFW):** {gradio_nsfw_prediction}")
|
|
@@ -112,13 +109,12 @@ def display_images(images):
|
|
| 112 |
os.remove(file_path)
|
| 113 |
st.experimental_rerun()
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
uploaded_files = st.file_uploader("ファイルをアップロードしてください", type=["jpg", "png"], accept_multiple_files=True)
|
| 119 |
|
| 120 |
if uploaded_files:
|
| 121 |
-
st.session_state.uploaded_files = uploaded_files
|
| 122 |
save_uploaded_files(uploaded_files)
|
| 123 |
|
| 124 |
files_per_page = 20
|
|
@@ -146,13 +142,7 @@ st.write(f"現在のページ: {st.session_state.page}")
|
|
| 146 |
|
| 147 |
current_page_files = paginate_files(file_paths, st.session_state.page, files_per_page)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
ascending = st.checkbox("昇順", value=True)
|
| 151 |
-
st.session_state.sort_option = {"column": sort_column, "ascending": ascending}
|
| 152 |
-
|
| 153 |
-
if sort_column == "timestamp":
|
| 154 |
-
current_page_files.sort(key=lambda x: timestamps[file_paths.index(x)], reverse=not ascending)
|
| 155 |
-
|
| 156 |
display_images(current_page_files)
|
| 157 |
|
| 158 |
# Blurのトグルを更新
|
|
@@ -165,17 +155,11 @@ if st.button("CSVに保存"):
|
|
| 165 |
csv_writer = csv.writer(csv_file)
|
| 166 |
csv_writer.writerow(['ファイル名', 'NSFWの予測', 'Goodの値', '作成時間'])
|
| 167 |
for i, file_path in enumerate(file_paths):
|
| 168 |
-
|
| 169 |
-
good_value = good_values[i]
|
| 170 |
-
timestamp = timestamps[i]
|
| 171 |
-
csv_writer.writerow([os.path.basename(file_path), gradio_nsfw_prediction, good_value, timestamp])
|
| 172 |
|
| 173 |
st.markdown(get_binary_file_downloader_html(csv_filename, label="CSVをダウンロード"), unsafe_allow_html=True)
|
| 174 |
|
| 175 |
-
|
| 176 |
-
for prediction in gradio_nsfw_predictions:
|
| 177 |
-
f.write(f"{prediction[0]} (NSFW): {prediction[1]}\n")
|
| 178 |
-
|
| 179 |
if st.button("ファイルを一括ダウンロード"):
|
| 180 |
zip_filename = "files.zip"
|
| 181 |
with zipfile.ZipFile(zip_filename, "w") as zipf:
|
|
|
|
| 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 |
|
| 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=True, key="blur_toggle")
|
| 20 |
|
| 21 |
# Lists to store Gradio predictions, good values, timestamps, and file paths
|
| 22 |
gradio_nsfw_predictions = []
|
|
|
|
| 24 |
timestamps = []
|
| 25 |
file_paths = []
|
| 26 |
|
| 27 |
+
# Function to get Gradio NSFW prediction
|
| 28 |
+
def get_gradio_nsfw_prediction(image_path):
|
| 29 |
+
result = client_nsfw.predict(image_path, api_name="/predict")
|
| 30 |
+
return result["label"] # Assuming the label is the prediction result
|
| 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:
|
|
|
|
| 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"):
|
| 43 |
os.makedirs("temp")
|
|
|
|
| 47 |
with open(file_path, "wb") as f:
|
| 48 |
f.write(uploaded_file.getbuffer())
|
| 49 |
|
| 50 |
+
# Get Gradio NSFW prediction
|
| 51 |
gradio_nsfw_prediction = get_gradio_nsfw_prediction(file_path)
|
| 52 |
|
| 53 |
# Extract good value confidence
|
|
|
|
| 87 |
|
| 88 |
# Check if index is within the range
|
| 89 |
if i < len(gradio_nsfw_predictions):
|
| 90 |
+
gradio_nsfw_prediction = gradio_nsfw_predictions[i][1] # Accessing the prediction result from the tuple
|
| 91 |
timestamp = timestamps[i]
|
| 92 |
|
| 93 |
st.write(f"**Prediction for {os.path.basename(file_path)} (NSFW):** {gradio_nsfw_prediction}")
|
|
|
|
| 109 |
os.remove(file_path)
|
| 110 |
st.experimental_rerun()
|
| 111 |
|
| 112 |
+
# Main part of the Streamlit app
|
| 113 |
+
st.title("ファイルアップロードと共有")
|
| 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
|
|
|
|
| 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 |
|
| 148 |
# Blurのトグルを更新
|
|
|
|
| 155 |
csv_writer = csv.writer(csv_file)
|
| 156 |
csv_writer.writerow(['ファイル名', 'NSFWの予測', 'Goodの値', '作成時間'])
|
| 157 |
for i, file_path in enumerate(file_paths):
|
| 158 |
+
csv_writer.writerow([os.path.basename(file_path), gradio_nsfw_predictions[i][1], good_values[i], timestamps[i]])
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
st.markdown(get_binary_file_downloader_html(csv_filename, label="CSVをダウンロード"), unsafe_allow_html=True)
|
| 161 |
|
| 162 |
+
# ファイルを一括ダウンロード
|
|
|
|
|
|
|
|
|
|
| 163 |
if st.button("ファイルを一括ダウンロード"):
|
| 164 |
zip_filename = "files.zip"
|
| 165 |
with zipfile.ZipFile(zip_filename, "w") as zipf:
|