Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify, send_from_directory | |
| import io | |
| import os | |
| from PIL import Image | |
| import numpy as np | |
| import tensorflow as tf | |
| app = Flask(__name__, static_folder='../frontend', static_url_path='') | |
| model = tf.keras.models.load_model("backend/model.keras") | |
| def index(): | |
| return send_from_directory(app.static_folder, "index.html") | |
| def predict(): | |
| file = request.files.get("file") | |
| if file is None: | |
| return jsonify({"error": "No file uploaded"}), 400 | |
| try: | |
| img = Image.open(io.BytesIO(file.read())).convert("RGB").resize((32, 32)) | |
| arr = np.array(img).astype("float32") / 255.0 | |
| arr = np.expand_dims(arr, axis=0) | |
| preds = model.predict(arr) | |
| top_idx = int(np.argmax(preds[0])) | |
| result = { | |
| "class": top_idx, | |
| "confidence": float(preds[0][top_idx]), | |
| } | |
| return jsonify(result) | |
| except Exception as e: | |
| return jsonify({"error": f"Gagal memproses gambar: {str(e)}"}), 500 |