Download app.py from DeimosJS3/Final: direct link, hf CLI and curl.
- Browser
- Download file 980 Bytes
-
https://huggingface.co/spaces/DeimosJS3/Final/resolve/main/app.py
- Command line
-
hf download hf://spaces/DeimosJS3/Final/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DeimosJS3/Final/resolve/main/app.py
980 Bytes
| from fastapi import FastAPI, File, UploadFile | |
| from fastapi.responses import JSONResponse | |
| import numpy as np | |
| from PIL import Image | |
| import io | |
| from tensorflow.keras.models import load_model | |
| app = FastAPI() | |
| # Cargar el modelo al iniciar (solo 1 vez) | |
| model = load_model("modelo_cirrosis.h5") | |
| # Definir las clases en el mismo orden del entrenamiento | |
| class_names = ["sano", "cirrosis"] | |
| async def predict(file: UploadFile = File(...)): | |
| # Leer la imagen enviada por POST | |
| contents = await file.read() | |
| image = Image.open(io.BytesIO(contents)).convert("RGB") | |
| image = image.resize((150, 150)) # O el tamaño de tu modelo | |
| img_array = np.array(image) / 255.0 | |
| img_array = np.expand_dims(img_array, 0) | |
| # Predicción | |
| prediction = model.predict(img_array) | |
| pred_index = np.argmax(prediction) | |
| result = { | |
| "class": class_names[pred_index], | |
| "confidence": float(np.max(prediction)) | |
| } | |
| return JSONResponse(result) | |