Spaces:
Sleeping
Sleeping
Commit
·
3b72630
1
Parent(s):
bc723d8
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import requests
|
4 |
+
import io
|
5 |
+
|
6 |
+
def classify_image(image):
|
7 |
+
# Open the image file using Pillow
|
8 |
+
img = Image.open(io.BytesIO(image.read()))
|
9 |
+
|
10 |
+
# Convert the image to base64 string
|
11 |
+
buffered = io.BytesIO()
|
12 |
+
img.save(buffered, format="PNG")
|
13 |
+
img_base64 = "data:image/png;base64," + buffered.getvalue().hex()
|
14 |
+
|
15 |
+
# Send a request to the endpoint with the image data
|
16 |
+
response = requests.get(
|
17 |
+
"https://4937ea62-c0b0-414f-afbc-53be1f5b0d06.id.repl.co/scan",
|
18 |
+
data={"base64": img_base64}
|
19 |
+
)
|
20 |
+
|
21 |
+
# Process the response and return the result
|
22 |
+
if response.text == "sfw":
|
23 |
+
return "Safe for work"
|
24 |
+
elif response.text == "nsfw":
|
25 |
+
return "Not safe for work"
|
26 |
+
else:
|
27 |
+
return "Unknown"
|
28 |
+
|
29 |
+
# Create a Gradio interface
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=classify_image,
|
32 |
+
inputs="file",
|
33 |
+
outputs="text",
|
34 |
+
title="AntiNSFW",
|
35 |
+
description="Classify if an image is safe for work or not.",
|
36 |
+
thumbnail="path_to_thumbnail_image.png"
|
37 |
+
)
|
38 |
+
|
39 |
+
# Launch the interface
|
40 |
+
iface.launch()
|