sdafd commited on
Commit
0677caf
·
verified ·
1 Parent(s): 177439e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +227 -0
app.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import tempfile
4
+ import os
5
+ import requests
6
+ import base64
7
+ import random
8
+ import string
9
+ import json
10
+ import ast
11
+
12
+
13
+ def check_nsfw(img_url):
14
+ session_hash = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(7))
15
+ data = {
16
+ 'data': [
17
+ {'path': img_url},
18
+ "chen-convnext",
19
+ 0.5,
20
+ True,
21
+ True
22
+ ],
23
+ 'session_hash': session_hash,
24
+ 'fn_index': 0,
25
+ 'trigger_id': 12
26
+ }
27
+ r = requests.post('https://yoinked-da-nsfw-checker.hf.space/queue/join', json=data)
28
+
29
+ r = requests.get(f'https://yoinked-da-nsfw-checker.hf.space/queue/data?session_hash={session_hash}', stream=True)
30
+ buffer = "" # Buffer to accumulate the chunks
31
+
32
+
33
+ for content in r.iter_content(100):
34
+ # Decode the byte content to a string
35
+ buffer += content.decode('utf-8')
36
+
37
+ return json.loads(buffer.split('data:')[len(buffer.split('data:'))-2])["output"]["data"][0]['label']
38
+
39
+
40
+ def check_nsfw2(img_url):
41
+ session_hash = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(7))
42
+ data = {
43
+ 'data': [
44
+ {'path': img_url}
45
+ ],
46
+ 'session_hash': session_hash,
47
+ 'fn_index': 0,
48
+ 'trigger_id': 9
49
+ }
50
+ r = requests.post('https://jamescookjr90-falconsai-nsfw-image-detection.hf.space/queue/join', json=data)
51
+
52
+ r = requests.get(f'https://jamescookjr90-falconsai-nsfw-image-detection.hf.space/queue/data?session_hash={session_hash}', stream=True)
53
+ buffer = "" # Buffer to accumulate the chunks
54
+
55
+
56
+ for content in r.iter_content(100):
57
+ # Decode the byte content to a string
58
+ buffer += content.decode('utf-8')
59
+ return json.loads(buffer.split('data:')[len(buffer.split('data:'))-2])["output"]["data"][0]['label']
60
+
61
+ def check_nsfw3(img_url):
62
+ data = {
63
+ 'data': [
64
+ {'path': img_url}
65
+ ]
66
+
67
+ }
68
+ r = requests.post('https://zanderlewis-xl-nsfw-detection.hf.space/call/predict',json=data)
69
+ json_data = r.json()
70
+ event_id = json_data['event_id']
71
+ r = requests.get(f'https://zanderlewis-xl-nsfw-detection.hf.space/call/predict/{event_id}', stream=True)
72
+ event_stream = ''
73
+ for chunk in r.iter_content(100):
74
+ event_stream += chunk.decode('utf-8')
75
+ return ast.literal_eval(event_stream.split('data:')[-1])[0]['label']
76
+
77
+ def get_replica_code(url):
78
+ try:
79
+ r = requests.get(url)
80
+
81
+ return r.text.split('replicas/')[1].split('"};')[0]
82
+ except:
83
+ return None
84
+ def check_nsfw4(img_url):
85
+ code = get_replica_code('https://error466-falconsai-nsfw-image-detection.hf.space')
86
+ session_hash = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(7))
87
+ data = {
88
+ 'data': [
89
+ {'path': img_url}
90
+ ],
91
+ 'session_hash': session_hash,
92
+ 'fn_index': 0,
93
+ 'trigger_id': 58
94
+ }
95
+ r = requests.post(f'https://error466-falconsai-nsfw-image-detection.hf.space/--replicas/{code}/queue/join', json=data)
96
+
97
+ r = requests.get(f'https://error466-falconsai-nsfw-image-detection.hf.space/--replicas/{code}/queue/data?session_hash={session_hash}', stream=True)
98
+ buffer = "" # Buffer to accumulate the chunks
99
+
100
+
101
+ for content in r.iter_content(100):
102
+ # Decode the byte content to a string
103
+ buffer += content.decode('utf-8')
104
+ return json.loads(buffer.split('data:')[-1])["output"]["data"][0]['label']
105
+
106
+ def check_nsfw5(img_url):
107
+ session_hash = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(7))
108
+ data = {
109
+ 'data': [
110
+ {'path': img_url}
111
+ ],
112
+ 'session_hash': session_hash,
113
+ 'fn_index': 0,
114
+ 'trigger_id': 9
115
+ }
116
+ r = requests.post('https://phelpsgg-falconsai-nsfw-image-detection.hf.space/queue/join', json=data)
117
+
118
+ r = requests.get(f'https://phelpsgg-falconsai-nsfw-image-detection.hf.space/queue/data?session_hash={session_hash}', stream=True)
119
+ buffer = "" # Buffer to accumulate the chunks
120
+
121
+
122
+ for content in r.iter_content(100):
123
+ # Decode the byte content to a string
124
+ buffer += content.decode('utf-8')
125
+ return json.loads(buffer.split('data:')[-1])["output"]["data"][0]['label']
126
+
127
+ def check_nsfw_final(img_url):
128
+ result = None
129
+
130
+ try:
131
+ check = check_nsfw2(img_url)
132
+ if check == 'nsfw':
133
+ result = True
134
+ else:
135
+ result = False
136
+ except:
137
+ pass
138
+
139
+ if result is None:
140
+ try:
141
+ check = check_nsfw3(img_url)
142
+ if check == 'nsfw':
143
+ result = True
144
+ else:
145
+ result = False
146
+ except:
147
+ pass
148
+
149
+ if result is None:
150
+ try:
151
+ check = check_nsfw4(img_url)
152
+ if check == 'nsfw':
153
+ result = True
154
+ else:
155
+ result = False
156
+ except:
157
+ pass
158
+
159
+ if result is None:
160
+ try:
161
+ check = check_nsfw5(img_url)
162
+ if check == 'nsfw':
163
+ result = True
164
+ else:
165
+ result = False
166
+ except:
167
+ pass
168
+
169
+ return result
170
+
171
+
172
+ def extract_frames(video_path, num_frames):
173
+ vidcap = cv2.VideoCapture(video_path)
174
+ total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
175
+ frames = []
176
+
177
+ for i in range(num_frames):
178
+ frame_number = int(i * total_frames / num_frames) # Sample evenly
179
+ vidcap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
180
+ success, image = vidcap.read()
181
+ if success:
182
+ # Save frame to temporary file and append it to list
183
+ temp_img = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
184
+ cv2.imwrite(temp_img.name, image)
185
+ frames.append(temp_img.name)
186
+ else:
187
+ break
188
+
189
+ vidcap.release()
190
+ return frames
191
+
192
+ # Gradio app logic
193
+ def process_video(video, num_frames):
194
+ video_path = os.path.join(tempfile.gettempdir(), video.name)
195
+ with open(video_path, "wb") as f:
196
+ f.write(video.read())
197
+
198
+ frames = extract_frames(video_path, num_frames)
199
+
200
+ nsfw_count = 0
201
+ total_frames = len(frames)
202
+
203
+ for frame_path in frames:
204
+ if check_nsfw_final(frame_path):
205
+ nsfw_count += 1
206
+ os.remove(frame_path)
207
+
208
+ return f"{nsfw_count} out of {total_frames} frames were NSFW."
209
+
210
+ with gr.Blocks() as app:
211
+ gr.Markdown("## NSFW Frame Detection in Video")
212
+
213
+ with gr.Row():
214
+ video_input = gr.Video(label="Upload Video")
215
+ num_frames_input = gr.Number(label="Number of Frames to Check", value=10, precision=0)
216
+
217
+ output_text = gr.Textbox(label="Result")
218
+
219
+ submit_button = gr.Button("Submit")
220
+
221
+ submit_button.click(
222
+ fn=process_video,
223
+ inputs=[video_input, num_frames_input],
224
+ outputs=output_text
225
+ )
226
+
227
+ app.launch()