OzoneAsai commited on
Commit
266a0e2
·
1 Parent(s): 79cd54f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -19
app.py CHANGED
@@ -2,11 +2,40 @@ import streamlit as st
2
  import os
3
  import zipfile
4
  import base64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  st.title("ファイルアップロードと共有")
7
 
8
  uploaded_files = st.file_uploader("ファイルをアップロードしてください(複数選択可)", type=["jpg", "png", "pdf"], accept_multiple_files=True)
9
 
 
 
 
10
  def save_uploaded_files(uploaded_files):
11
  if not os.path.exists("temp"):
12
  os.makedirs("temp")
@@ -16,14 +45,12 @@ def save_uploaded_files(uploaded_files):
16
  with open(file_path, "wb") as f:
17
  f.write(uploaded_file.getbuffer())
18
  file_paths.append(file_path)
19
- return file_paths
20
 
21
- # ダウンロード用のHTMLを生成
22
- def get_binary_file_downloader_html(file_path):
23
- with open(file_path, 'rb') as f:
24
- data = f.read()
25
- b64 = base64.b64encode(data).decode()
26
- return f'<a href="data:application/zip;base64,{b64}" download="{file_path}">ダウンロード</a>'
27
 
28
  if uploaded_files:
29
  file_paths = save_uploaded_files(uploaded_files)
@@ -50,21 +77,29 @@ if file_paths:
50
 
51
  for file_path in file_paths[start_index:end_index]:
52
  file_ext = os.path.splitext(file_path)[1].lower()
53
- if file_ext in [".jpg", ".png"]:
54
- st.image(file_path, caption=os.path.basename(file_path), use_column_width=True)
55
- elif file_ext == ".pdf":
56
- st.write(os.path.basename(file_path))
57
- col1, col2 = st.columns([4, 1])
58
- with col1:
 
 
59
  if file_ext in [".jpg", ".png"]:
60
  st.image(file_path, caption=os.path.basename(file_path), use_column_width=True)
61
- else:
62
  st.write(os.path.basename(file_path))
63
- st.write(f"URL: {file_path}")
64
- with col2:
65
- if col2.button("削除", key=file_path):
66
- os.remove(file_path)
67
- st.experimental_rerun()
 
 
 
 
 
 
68
 
69
  # 一括ダウンロードボタンとtempフォルダーをZipファイルに作成
70
  if st.button("フォルダーを一括ダウンロード"):
@@ -79,4 +114,12 @@ if file_paths:
79
  # Zipファイルをリンクとして提供
80
  st.markdown(get_binary_file_downloader_html(zip_filename), unsafe_allow_html=True)
81
 
 
 
 
 
 
 
 
 
82
  st.success("ファイルがアップロードされました。このページのURLを他のクライアントと共有してください。")
 
2
  import os
3
  import zipfile
4
  import base64
5
+ from PIL import Image
6
+ from transformers import pipeline
7
+
8
+ # Function to save NSFW data to a CSV file
9
+ def save_nsfw_data_to_csv(data):
10
+ import csv
11
+
12
+ with open("nsfw_data.csv", mode="w", newline="") as file:
13
+ writer = csv.writer(file)
14
+ writer.writerow(["Image_Path", "NSFW_Score"])
15
+ for row in data:
16
+ writer.writerow(row)
17
+
18
+ # Function to apply blur to an image
19
+ def apply_blur(image_path):
20
+ img = Image.open(image_path)
21
+ img = img.filter(ImageFilter.GaussianBlur(radius=5))
22
+ return img
23
+
24
+ # Function to get NSFW score using the provided pipeline
25
+ def get_nsfw_score(image_path):
26
+ img = Image.open(image_path)
27
+ classifier = pipeline("image-classification", model="Falconsai/nsfw_image_detection")
28
+ result = classifier(img)
29
+ nsfw_score = result[0]["score"]
30
+ return nsfw_score
31
 
32
  st.title("ファイルアップロードと共有")
33
 
34
  uploaded_files = st.file_uploader("ファイルをアップロードしてください(複数選択可)", type=["jpg", "png", "pdf"], accept_multiple_files=True)
35
 
36
+ # List to store NSFW data for CSV
37
+ nsfw_data = []
38
+
39
  def save_uploaded_files(uploaded_files):
40
  if not os.path.exists("temp"):
41
  os.makedirs("temp")
 
45
  with open(file_path, "wb") as f:
46
  f.write(uploaded_file.getbuffer())
47
  file_paths.append(file_path)
 
48
 
49
+ # Get NSFW score
50
+ nsfw_score = get_nsfw_score(file_path)
51
+ nsfw_data.append((file_path, nsfw_score))
52
+
53
+ return file_paths
 
54
 
55
  if uploaded_files:
56
  file_paths = save_uploaded_files(uploaded_files)
 
77
 
78
  for file_path in file_paths[start_index:end_index]:
79
  file_ext = os.path.splitext(file_path)[1].lower()
80
+
81
+ nsfw_score = get_nsfw_score(file_path)
82
+
83
+ if nsfw_score >= 0.8 and not st.session_state.display_options:
84
+ # Apply blur if NSFW score is high and display options are hidden
85
+ blurred_img = apply_blur(file_path)
86
+ st.image(blurred_img, caption=os.path.basename(file_path), use_column_width=True)
87
+ else:
88
  if file_ext in [".jpg", ".png"]:
89
  st.image(file_path, caption=os.path.basename(file_path), use_column_width=True)
90
+ elif file_ext == ".pdf":
91
  st.write(os.path.basename(file_path))
92
+ col1, col2 = st.columns([4, 1])
93
+ with col1:
94
+ if file_ext in [".jpg", ".png"]:
95
+ st.image(file_path, caption=os.path.basename(file_path), use_column_width=True)
96
+ else:
97
+ st.write(os.path.basename(file_path))
98
+ st.write(f"URL: {file_path}")
99
+ with col2:
100
+ if col2.button("削除", key=file_path):
101
+ os.remove(file_path)
102
+ st.experimental_rerun()
103
 
104
  # 一括ダウンロードボタンとtempフォルダーをZipファイルに作成
105
  if st.button("フォルダーを一括ダウンロード"):
 
114
  # Zipファイルをリンクとして提供
115
  st.markdown(get_binary_file_downloader_html(zip_filename), unsafe_allow_html=True)
116
 
117
+ # トグルボタンを表示
118
+ st.checkbox("表示オプション", key="display_options")
119
+
120
+ if st.session_state.display_options:
121
+ # NSFWデータをCSVに保存
122
+ save_nsfw_data_to_csv(nsfw_data)
123
+ st.success("NSFWデータがCSVファイルに保存されました。")
124
+
125
  st.success("ファイルがアップロードされました。このページのURLを他のクライアントと共有してください。")