Spaces:
Sleeping
Sleeping
test
Browse files
app.py
CHANGED
|
@@ -1,59 +1,6 @@
|
|
| 1 |
-
# app.py (
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import torchvision.transforms as transforms
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import json
|
| 7 |
-
import timm
|
| 8 |
-
from fastapi import FastAPI, UploadFile, File
|
| 9 |
-
from fastapi.responses import JSONResponse
|
| 10 |
-
import io
|
| 11 |
-
|
| 12 |
-
# --- 1. Importar la definición del modelo ---
|
| 13 |
-
from VisionEnsembleModel import VisionEnsembleModel
|
| 14 |
-
|
| 15 |
-
# --- 2. Carga del Modelo y Componentes ---
|
| 16 |
-
device = torch.device("cpu")
|
| 17 |
-
MODEL_PATH = "model/best_vision_ensemble_model.pth"
|
| 18 |
-
NUM_CLASSES = 156
|
| 19 |
-
|
| 20 |
-
# El mapa de etiquetas ya no es necesario en el servidor
|
| 21 |
-
# ya que la app móvil se encargará de la traducción.
|
| 22 |
-
|
| 23 |
-
model = VisionEnsembleModel(num_classes=NUM_CLASSES)
|
| 24 |
-
model.load_state_dict(torch.load(MODEL_PATH, map_location=device))
|
| 25 |
-
model.to(device)
|
| 26 |
-
model.eval()
|
| 27 |
-
print("Modelo Ensamblado Híbrido cargado y listo para servir la API.")
|
| 28 |
-
|
| 29 |
-
transforms_val = transforms.Compose([
|
| 30 |
-
transforms.Resize((224, 224)),
|
| 31 |
-
transforms.ToTensor(),
|
| 32 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 33 |
-
])
|
| 34 |
-
|
| 35 |
-
# --- 3. Crear la App FastAPI ---
|
| 36 |
-
app = FastAPI(title="API de Clasificación de Orquídeas")
|
| 37 |
-
|
| 38 |
@app.get("/")
|
| 39 |
def read_root():
|
| 40 |
-
return {"
|
| 41 |
-
|
| 42 |
-
# --- 4. Endpoint de API para la App Móvil (devuelve IDs) ---
|
| 43 |
-
@app.post("/predict_for_mobile")
|
| 44 |
-
async def predict_for_mobile(file: UploadFile = File(...)):
|
| 45 |
-
image_bytes = await file.read()
|
| 46 |
-
try:
|
| 47 |
-
pil_image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 48 |
-
except Exception:
|
| 49 |
-
return JSONResponse(status_code=400, content={"error": "Archivo de imagen inválido."})
|
| 50 |
-
|
| 51 |
-
input_tensor = transforms_val(pil_image).unsqueeze(0).to(device)
|
| 52 |
-
with torch.no_grad():
|
| 53 |
-
output = model(input_tensor)
|
| 54 |
-
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
| 55 |
-
|
| 56 |
-
top5_prob, top_catid = torch.topk(probabilities, 5)
|
| 57 |
-
|
| 58 |
-
results = [{"species_id": cat_id.item(), "confidence": prob.item()} for prob, cat_id in zip(top5_prob, top_catid)]
|
| 59 |
-
return JSONResponse(content={"predictions": results})
|
|
|
|
| 1 |
+
# app.py (Hola Mundo)
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
@app.get("/")
|
| 5 |
def read_root():
|
| 6 |
+
return {"Hello": "World"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|