Upload 3 files
Browse files- .env +1 -0
- app.py +60 -0
- requirements.txt +4 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
API_KEY="mysecretapikey123456"
|
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
from tensorflow.keras.preprocessing import image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# Ambil api key dari .env
|
| 10 |
+
import os
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
API_KEY = os.getenv('API_KEY')
|
| 15 |
+
|
| 16 |
+
app = Flask(__name__)
|
| 17 |
+
|
| 18 |
+
# Definisikan ukuran gambar
|
| 19 |
+
img_width, img_height = 150, 150
|
| 20 |
+
|
| 21 |
+
# Memuat model
|
| 22 |
+
model = load_model('inceptionv3_nsfw_model.h5')
|
| 23 |
+
|
| 24 |
+
def classify_image(img):
|
| 25 |
+
img = img.resize((img_width, img_height))
|
| 26 |
+
img_array = image.img_to_array(img)
|
| 27 |
+
img_array = np.expand_dims(img_array, axis=0) / 255.0
|
| 28 |
+
|
| 29 |
+
predictions = model.predict(img_array)
|
| 30 |
+
class_names = ['sexy', 'hentai', 'porn', 'neutral', 'drawings']
|
| 31 |
+
result = dict(zip(class_names, predictions[0].astype(float)))
|
| 32 |
+
|
| 33 |
+
return result
|
| 34 |
+
|
| 35 |
+
def check_api_key(request):
|
| 36 |
+
api_key = request.headers.get('x-api-key')
|
| 37 |
+
return api_key == API_KEY
|
| 38 |
+
|
| 39 |
+
@app.route('/classify', methods=['POST'])
|
| 40 |
+
def classify():
|
| 41 |
+
if not check_api_key(request):
|
| 42 |
+
return jsonify({'error': 'Invalid or missing API key'}), 403
|
| 43 |
+
|
| 44 |
+
data = request.json
|
| 45 |
+
img_url = data.get('image_url')
|
| 46 |
+
|
| 47 |
+
if not img_url:
|
| 48 |
+
return jsonify({'error': 'No image URL provided'}), 400
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
response = requests.get(img_url)
|
| 52 |
+
img = Image.open(BytesIO(response.content))
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return jsonify({'error': str(e)}), 400
|
| 55 |
+
|
| 56 |
+
result = classify_image(img)
|
| 57 |
+
return jsonify(result)
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
app.run(host='0.0.0.0', port=5000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
keras
|
| 3 |
+
numpy
|
| 4 |
+
python-dotenv
|