tangjicheng commited on
Commit
f0e6347
1 Parent(s): 96c37db

new file: app.py

Browse files

new file: requirements.txt

Files changed (2) hide show
  1. app.py +31 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import opennsfw2
2
+ import numpy as np
3
+ import gradio as gr
4
+
5
+
6
+ def predict_nsfw(pil_image) -> str:
7
+ image = opennsfw2.preprocess_image(
8
+ pil_image, opennsfw2.Preprocessing.YAHOO)
9
+
10
+ model = opennsfw2.make_open_nsfw_model()
11
+
12
+ # Add batch axis (for single image).
13
+ inputs = np.expand_dims(image, axis=0)
14
+
15
+ predictions = model.predict(inputs)
16
+
17
+ # The shape of predictions is (num_images, 2).
18
+ sfw_probability, nsfw_probability = predictions[0]
19
+ if nsfw_probability > sfw_probability:
20
+ result_str = f"It is NSFW image.\nnsfw_probability: {nsfw_probability:.5f}"
21
+ else:
22
+ result_str = f"It is NOT NSFW image.\nnsfw_probability: {nsfw_probability:.5f}"
23
+ return result_str
24
+
25
+
26
+ inputs = gr.inputs.Image(label="input_image", type="pil")
27
+ outputs = gr.outputs.Textbox(label="output_string")
28
+
29
+ app = gr.Interface(fn=predict_nsfw, inputs=inputs, outputs=outputs)
30
+
31
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ gdown>=4.2.0
2
+ matplotlib>=3.0.0
3
+ numpy>=1.22.0
4
+ opencv-python>=4.0.0.0
5
+ Pillow>=9.0.0
6
+ scikit-image>=0.18.0
7
+ tensorflow>=2.0.0
8
+ tqdm>=4.62
9
+ opennsfw2