Spaces:
Runtime error
Runtime error
File size: 1,169 Bytes
10c3c7d 7cf4d1c 10c3c7d 7cf4d1c 10c3c7d 7cf4d1c 10c3c7d 7cf4d1c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | from flask import Flask, request, jsonify
from flask_cors import CORS
from tensorflow.keras.models import load_model
from PIL import Image
import numpy as np
import requests
from io import BytesIO
app = Flask(__name__)
CORS(app) # biar bisa diakses dari frontend
# Load model
model = load_model("model/Model_CNN_256px.keras")
class_labels = {
0: "Bercak Daun (Leaf Spot)",
1: "Hawar Daun (Blight)",
2: "Karat Daun (Rust)",
3: "Sehat (Healthy)"
}
@app.route("/api/model", methods=["POST"])
def predict():
data = request.json
image_url = data.get("image_url")
if not image_url:
return jsonify({"error": "image_url missing"}), 400
# Preprocess gambar
response = requests.get(image_url)
img = Image.open(BytesIO(response.content)).convert("RGB")
img = img.resize((256, 256))
img = np.array(img) / 255.0
img = np.expand_dims(img, axis=0)
preds = model.predict(img)
idx = int(np.argmax(preds[0]))
return jsonify({
"nama_penyakit": class_labels[idx],
"akurasi": round(float(preds[0][idx]) * 100, 2)
})
@app.route("/health")
def health():
return jsonify({"status": "ok"})
|