Spaces:
Runtime error
Runtime error
Download app.py from capstone-bertani/bertani: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/capstone-bertani/bertani/resolve/main/app.py
- Command line
-
hf download hf://spaces/capstone-bertani/bertani/app.py
-
curl -L -o app.py https://huggingface.co/spaces/capstone-bertani/bertani/resolve/main/app.py
1.17 kB
| 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)" | |
| } | |
| 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) | |
| }) | |
| def health(): | |
| return jsonify({"status": "ok"}) | |