Spaces:
Runtime error
Runtime error
Commit
·
82a076d
1
Parent(s):
a0f311d
added possible detection api
Browse files- README.md +1 -1
- app.py +45 -15
- requirements.txt +2 -1
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 📉
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: yellow
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Image NSFW-Filter API
|
| 3 |
emoji: 📉
|
| 4 |
colorFrom: pink
|
| 5 |
colorTo: yellow
|
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from flask import Flask, request, jsonify, send_from_directory
|
|
|
|
| 2 |
import requests
|
| 3 |
import io
|
| 4 |
import random
|
|
@@ -9,30 +10,55 @@ import string
|
|
| 9 |
import re
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
response.raise_for_status()
|
| 24 |
-
return response.json()
|
| 25 |
|
| 26 |
-
@app.route("/
|
| 27 |
-
def
|
| 28 |
try:
|
| 29 |
data = request.get_json()
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
resp = query(prompt)
|
| 33 |
response = {
|
| 34 |
"success": True,
|
| 35 |
-
"result":
|
| 36 |
}
|
| 37 |
|
| 38 |
except Exception as e:
|
|
@@ -43,5 +69,9 @@ def generate():
|
|
| 43 |
|
| 44 |
return jsonify(response)
|
| 45 |
|
|
|
|
| 46 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
| 47 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
+
from nsfw_detector import predict
|
| 3 |
import requests
|
| 4 |
import io
|
| 5 |
import random
|
|
|
|
| 10 |
import re
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
+
model = predict.load_model('./nsfw_mobilenet2.224x224.h5')
|
| 14 |
|
| 15 |
+
@app.route("/imagebuffer", methods=["POST"])
|
| 16 |
+
def check():
|
| 17 |
+
try:
|
| 18 |
+
data = request.get_json()
|
| 19 |
+
imgdata = data["data"]
|
| 20 |
+
|
| 21 |
+
#imgdata is like 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBD...', save it to a file in ./TEMP
|
| 22 |
+
imgdata = re.sub('^data:image/.+;base64,', '', imgdata)
|
| 23 |
+
imgdata = bytes(imgdata, encoding="utf-8")
|
| 24 |
+
img = Image.open(io.BytesIO(imgdata))
|
| 25 |
+
filename = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) + ".jpg"
|
| 26 |
+
img.save(f"./TEMP/{filename}")
|
| 27 |
|
| 28 |
+
#predict the image
|
| 29 |
+
result = predict.predict(model, f"./TEMP/{filename}")
|
| 30 |
+
|
| 31 |
+
response = {
|
| 32 |
+
"success": True,
|
| 33 |
+
"result": result
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
response = {
|
| 38 |
+
"success": False,
|
| 39 |
+
"error": str(e)
|
| 40 |
+
}
|
| 41 |
|
| 42 |
+
return jsonify(response)
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
@app.route("/imageurl", methods=["POST"])
|
| 45 |
+
def checkUrl():
|
| 46 |
try:
|
| 47 |
data = request.get_json()
|
| 48 |
+
url = data["url"]
|
| 49 |
+
|
| 50 |
+
#download the image
|
| 51 |
+
response = requests.get(url)
|
| 52 |
+
img = Image.open(io.BytesIO(response.content))
|
| 53 |
+
filename = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) + ".jpg"
|
| 54 |
+
img.save(f"./TEMP/{filename}")
|
| 55 |
+
|
| 56 |
+
#predict the image
|
| 57 |
+
result = predict.predict(model, f"./TEMP/{filename}")
|
| 58 |
|
|
|
|
| 59 |
response = {
|
| 60 |
"success": True,
|
| 61 |
+
"result": result
|
| 62 |
}
|
| 63 |
|
| 64 |
except Exception as e:
|
|
|
|
| 69 |
|
| 70 |
return jsonify(response)
|
| 71 |
|
| 72 |
+
|
| 73 |
if __name__ == "__main__":
|
| 74 |
+
if not os.path.exists("./TEMP"):
|
| 75 |
+
os.makedirs("./TEMP")
|
| 76 |
+
|
| 77 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
flask
|
| 2 |
requests
|
| 3 |
pillow
|
| 4 |
-
pycloudflared
|
|
|
|
|
|
| 1 |
flask
|
| 2 |
requests
|
| 3 |
pillow
|
| 4 |
+
pycloudflared
|
| 5 |
+
nsfw-detector
|