ok
Browse files- README.md +2 -0
- api.py +47 -0
- app.py +13 -4
- app2.py +0 -15
- config.json +1 -1
README.md
CHANGED
@@ -25,6 +25,8 @@ tags:
|
|
25 |
- nlp
|
26 |
---
|
27 |
|
|
|
|
|
28 |
# vit-base-nsfw-detector
|
29 |
|
30 |
This model is a fine-tuned version of [vit-base-patch16-384](https://huggingface.co/google/vit-base-patch16-384) on around 25_000 images (drawings, photos...).
|
|
|
25 |
- nlp
|
26 |
---
|
27 |
|
28 |
+
Credit: clone repository from [AdamCodd/vit-base-nsfw-detector](https://https://huggingface.co/AdamCodd/vit-base-nsfw-detector/tree/main)
|
29 |
+
|
30 |
# vit-base-nsfw-detector
|
31 |
|
32 |
This model is a fine-tuned version of [vit-base-patch16-384](https://huggingface.co/google/vit-base-patch16-384) on around 25_000 images (drawings, photos...).
|
api.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import ViTImageProcessor, AutoModelForImageClassification
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
import torch
|
6 |
+
|
7 |
+
# Inisialisasi Flask app
|
8 |
+
app = Flask(__name__)
|
9 |
+
|
10 |
+
# Inisialisasi model dan processor
|
11 |
+
processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
|
12 |
+
model = AutoModelForImageClassification.from_pretrained('AdamCodd/vit-base-nsfw-detector')
|
13 |
+
|
14 |
+
# Fungsi untuk memproses gambar dan membuat prediksi
|
15 |
+
def predict_image(url):
|
16 |
+
try:
|
17 |
+
# Mengambil gambar dari URL
|
18 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
19 |
+
|
20 |
+
# Memproses gambar dan membuat prediksi
|
21 |
+
inputs = processor(images=image, return_tensors="pt")
|
22 |
+
outputs = model(**inputs)
|
23 |
+
logits = outputs.logits
|
24 |
+
|
25 |
+
# Mengambil prediksi kelas
|
26 |
+
predicted_class_idx = logits.argmax(-1).item()
|
27 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
28 |
+
|
29 |
+
return predicted_label
|
30 |
+
except Exception as e:
|
31 |
+
return str(e)
|
32 |
+
|
33 |
+
# Route untuk menerima permintaan POST dengan URL gambar
|
34 |
+
@app.route('/predict', methods=['POST'])
|
35 |
+
def predict():
|
36 |
+
if request.method == 'POST':
|
37 |
+
data = request.get_json()
|
38 |
+
if 'image_url' not in data:
|
39 |
+
return jsonify({'error': 'URL gambar tidak ditemukan dalam request'}), 400
|
40 |
+
|
41 |
+
image_url = data['image_url']
|
42 |
+
prediction = predict_image(image_url)
|
43 |
+
return jsonify({'predicted_class': prediction})
|
44 |
+
|
45 |
+
# Menjalankan Flask app
|
46 |
+
if __name__ == '__main__':
|
47 |
+
app.run(host='0.0.0.0', port=5000, debug=True)
|
app.py
CHANGED
@@ -1,6 +1,15 @@
|
|
1 |
-
from transformers import
|
2 |
from PIL import Image
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import ViTImageProcessor, AutoModelForImageClassification
|
2 |
from PIL import Image
|
3 |
+
import requests
|
4 |
|
5 |
+
url = 'https://images-ng.pixai.art/images/orig/2339688a-b1b0-4646-9091-aea5bc17d834'
|
6 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
7 |
+
processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
|
8 |
+
model = AutoModelForImageClassification.from_pretrained('AdamCodd/vit-base-nsfw-detector')
|
9 |
+
inputs = processor(images=image, return_tensors="pt")
|
10 |
+
outputs = model(**inputs)
|
11 |
+
logits = outputs.logits
|
12 |
+
|
13 |
+
predicted_class_idx = logits.argmax(-1).item()
|
14 |
+
print("Predicted class:", model.config.id2label[predicted_class_idx])
|
15 |
+
# Predicted class: sfw
|
app2.py
DELETED
@@ -1,15 +0,0 @@
|
|
1 |
-
from transformers import ViTImageProcessor, AutoModelForImageClassification
|
2 |
-
from PIL import Image
|
3 |
-
import requests
|
4 |
-
|
5 |
-
url = 'https://images-ng.pixai.art/images/orig/2339688a-b1b0-4646-9091-aea5bc17d834'
|
6 |
-
image = Image.open(requests.get(url, stream=True).raw)
|
7 |
-
processor = ViTImageProcessor.from_pretrained('AdamCodd/vit-base-nsfw-detector')
|
8 |
-
model = AutoModelForImageClassification.from_pretrained('AdamCodd/vit-base-nsfw-detector')
|
9 |
-
inputs = processor(images=image, return_tensors="pt")
|
10 |
-
outputs = model(**inputs)
|
11 |
-
logits = outputs.logits
|
12 |
-
|
13 |
-
predicted_class_idx = logits.argmax(-1).item()
|
14 |
-
print("Predicted class:", model.config.id2label[predicted_class_idx])
|
15 |
-
# Predicted class: sfw
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config.json
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
{
|
2 |
-
"_name_or_path": "
|
3 |
"architectures": [
|
4 |
"ViTForImageClassification"
|
5 |
],
|
|
|
1 |
{
|
2 |
+
"_name_or_path": "yeftakun/vit-nsfw-detection",
|
3 |
"architectures": [
|
4 |
"ViTForImageClassification"
|
5 |
],
|