Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,39 +18,43 @@ app.add_middleware(
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# Load Model
|
| 22 |
-
# Load Model - Perbaikan untuk membaca folder model
|
| 23 |
try:
|
| 24 |
print("Memuat model...")
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Load CNN
|
| 29 |
-
cnn_model = tf.keras.models.load_model('model_kadar_air_final.h5')
|
| 30 |
print("Model berhasil dimuat!")
|
| 31 |
except Exception as e:
|
| 32 |
print(f"Gagal memuat model: {e}")
|
| 33 |
-
# Jika gagal, buat model dummy agar server tidak mati total (Opsional)
|
| 34 |
yolo_model = None
|
| 35 |
cnn_model = None
|
| 36 |
|
| 37 |
@app.get("/")
|
| 38 |
def root():
|
| 39 |
-
return {"message": "API Smart Roasting Online"}
|
| 40 |
|
| 41 |
@app.post("/predict")
|
| 42 |
async def predict(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
# 1. Baca Gambar
|
| 45 |
contents = await file.read()
|
| 46 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 47 |
|
| 48 |
-
# 2. YOLOv8 (Deteksi)
|
| 49 |
results = yolo_model(np.array(image))
|
| 50 |
count = len(results[0].boxes)
|
| 51 |
|
| 52 |
# 3. CNN (Kadar Air)
|
| 53 |
-
#
|
| 54 |
img_cnn = image.resize((224, 224))
|
| 55 |
img_array = np.expand_dims(np.array(img_cnn) / 255.0, axis=0)
|
| 56 |
prediction = cnn_model.predict(img_array)
|
|
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# Load Model
|
|
|
|
| 22 |
try:
|
| 23 |
print("Memuat model...")
|
| 24 |
+
# 1. YOLOv8 - Menggunakan '/' karena best.pt adalah folder di Space kamu
|
| 25 |
+
# Menambahkan task='detect' agar sistem tidak bingung jenis modelnya
|
| 26 |
+
yolo_model = YOLO('best.pt/', task='detect')
|
| 27 |
+
|
| 28 |
+
# 2. CNN - Menambahkan compile=False untuk melewati error 'keras.metrics.mae'
|
| 29 |
+
cnn_model = tf.keras.models.load_model('model_kadar_air_final.h5', compile=False)
|
| 30 |
|
|
|
|
|
|
|
| 31 |
print("Model berhasil dimuat!")
|
| 32 |
except Exception as e:
|
| 33 |
print(f"Gagal memuat model: {e}")
|
|
|
|
| 34 |
yolo_model = None
|
| 35 |
cnn_model = None
|
| 36 |
|
| 37 |
@app.get("/")
|
| 38 |
def root():
|
| 39 |
+
return {"message": "API Smart Roasting Online", "status": "Ready"}
|
| 40 |
|
| 41 |
@app.post("/predict")
|
| 42 |
async def predict(file: UploadFile = File(...)):
|
| 43 |
+
# Cek apakah model siap
|
| 44 |
+
if yolo_model is None or cnn_model is None:
|
| 45 |
+
return {"success": False, "error": "Model tidak tersedia di server"}
|
| 46 |
+
|
| 47 |
try:
|
| 48 |
# 1. Baca Gambar
|
| 49 |
contents = await file.read()
|
| 50 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 51 |
|
| 52 |
+
# 2. YOLOv8 (Deteksi Biji Kopi)
|
| 53 |
results = yolo_model(np.array(image))
|
| 54 |
count = len(results[0].boxes)
|
| 55 |
|
| 56 |
# 3. CNN (Kadar Air)
|
| 57 |
+
# Pastikan resize (224, 224) sesuai dengan arsitektur modelmu
|
| 58 |
img_cnn = image.resize((224, 224))
|
| 59 |
img_array = np.expand_dims(np.array(img_cnn) / 255.0, axis=0)
|
| 60 |
prediction = cnn_model.predict(img_array)
|