huha3 commited on
Commit
10c3c7d
·
verified ·
1 Parent(s): f63f7d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -52
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("/health")
44
- def health():
45
- return {"status": "ok"}
46
-
47
- if __name__ == "__main__":
48
- app.run(
49
- host="0.0.0.0",
50
- port=7860,
51
- debug=False
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
+ )