Spaces:
Build error
Build error
| from fastapi import FastAPI, File, UploadFile | |
| from fastapi.responses import JSONResponse | |
| from PIL import Image | |
| import tensorflow as tf | |
| import numpy as np | |
| import io | |
| app = FastAPI() | |
| # Load model | |
| model = tf.keras.models.load_model("klasifikasi_batik_model.keras") | |
| # Label yang kamu pakai | |
| labels = ['Kawung', 'Mega_Mendung', 'Parang', 'Truntum'] | |
| def preprocess_image(image: Image.Image): | |
| image = image.resize((224, 224)) # Ubah jika input size berbeda | |
| image = np.array(image) / 255.0 | |
| image = np.expand_dims(image, axis=0) | |
| return image | |
| async def predict(file: UploadFile = File(...)): | |
| contents = await file.read() | |
| image = Image.open(io.BytesIO(contents)).convert("RGB") | |
| processed_image = preprocess_image(image) | |
| predictions = model.predict(processed_image) | |
| predicted_probabilities = predictions[0] | |
| if np.max(predicted_probabilities) < 0.80: | |
| result = "Gambar bukan batik" | |
| else: | |
| result = labels[np.argmax(predicted_probabilities)] | |
| probabilities = {labels[i]: float(predicted_probabilities[i]) for i in range(len(labels))} | |
| return JSONResponse(content={ | |
| "prediction": result, | |
| "probabilities": probabilities | |
| }) | |