Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,56 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
from tensorflow.keras.models import load_model
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
-
import requests
|
| 7 |
-
from io import BytesIO
|
| 8 |
-
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
-
CORS(app)
|
| 11 |
-
|
| 12 |
-
model = load_model("model/Model_CNN_256px.keras")
|
| 13 |
-
|
| 14 |
-
class_labels = {
|
| 15 |
-
0: "Bercak Daun (Leaf Spot)",
|
| 16 |
-
1: "Hawar Daun (Blight)",
|
| 17 |
-
2: "Karat Daun (Rust)",
|
| 18 |
-
3: "Sehat (Healthy)"
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
@app.route("/api/model", methods=["POST"])
|
| 22 |
-
def predict():
|
| 23 |
-
data = request.json
|
| 24 |
-
image_url = data.get("image_url")
|
| 25 |
-
|
| 26 |
-
if not image_url:
|
| 27 |
-
return jsonify({"error": "image_url missing"}), 400
|
| 28 |
-
|
| 29 |
-
response = requests.get(image_url)
|
| 30 |
-
img = Image.open(BytesIO(response.content)).convert("RGB")
|
| 31 |
-
img = img.resize((256, 256))
|
| 32 |
-
img = np.array(img) / 255.0
|
| 33 |
-
img = np.expand_dims(img, axis=0)
|
| 34 |
-
|
| 35 |
-
preds = model.predict(img)
|
| 36 |
-
idx = int(np.argmax(preds[0]))
|
| 37 |
-
|
| 38 |
-
return jsonify({
|
| 39 |
-
"nama_penyakit": class_labels[idx],
|
| 40 |
-
"akurasi": round(float(preds[0][idx]) * 100, 2)
|
| 41 |
-
})
|
| 42 |
-
|
| 43 |
-
@app.route("/
|
| 44 |
-
def
|
| 45 |
-
return {"
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from tensorflow.keras.models import load_model
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import requests
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
CORS(app)
|
| 11 |
+
|
| 12 |
+
model = load_model("model/Model_CNN_256px.keras")
|
| 13 |
+
|
| 14 |
+
class_labels = {
|
| 15 |
+
0: "Bercak Daun (Leaf Spot)",
|
| 16 |
+
1: "Hawar Daun (Blight)",
|
| 17 |
+
2: "Karat Daun (Rust)",
|
| 18 |
+
3: "Sehat (Healthy)"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
@app.route("/api/model", methods=["POST"])
|
| 22 |
+
def predict():
|
| 23 |
+
data = request.json
|
| 24 |
+
image_url = data.get("image_url")
|
| 25 |
+
|
| 26 |
+
if not image_url:
|
| 27 |
+
return jsonify({"error": "image_url missing"}), 400
|
| 28 |
+
|
| 29 |
+
response = requests.get(image_url)
|
| 30 |
+
img = Image.open(BytesIO(response.content)).convert("RGB")
|
| 31 |
+
img = img.resize((256, 256))
|
| 32 |
+
img = np.array(img) / 255.0
|
| 33 |
+
img = np.expand_dims(img, axis=0)
|
| 34 |
+
|
| 35 |
+
preds = model.predict(img)
|
| 36 |
+
idx = int(np.argmax(preds[0]))
|
| 37 |
+
|
| 38 |
+
return jsonify({
|
| 39 |
+
"nama_penyakit": class_labels[idx],
|
| 40 |
+
"akurasi": round(float(preds[0][idx]) * 100, 2)
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
@app.route("/")
|
| 44 |
+
def home():
|
| 45 |
+
return jsonify({"message": "API Bertani running"})
|
| 46 |
+
|
| 47 |
+
@app.route("/health")
|
| 48 |
+
def health():
|
| 49 |
+
return {"status": "ok"}
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
app.run(
|
| 53 |
+
host="0.0.0.0",
|
| 54 |
+
port=7860,
|
| 55 |
+
debug=False
|
| 56 |
+
)
|