Spaces:
Sleeping
Sleeping
Add application file
Browse files- predictor.py +22 -22
predictor.py
CHANGED
|
@@ -98,28 +98,28 @@ class CaligrafiaPredictor:
|
|
| 98 |
|
| 99 |
|
| 100 |
# Línea ~102 - FIX CRÍTICO
|
| 101 |
-
def predict(self, image): # ✅ NOMBRE DEL PARÁMETRO OBLIGATORIO
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
|
| 125 |
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
# Línea ~102 - FIX CRÍTICO
|
| 101 |
+
def predict(self, image): # ✅ NOMBRE DEL PARÁMETRO OBLIGATORIO
|
| 102 |
+
try:
|
| 103 |
+
if isinstance(image, str): # Path string
|
| 104 |
+
raw_image = Image.open(image).convert('RGB')
|
| 105 |
+
else: # PIL.Image de Gradio
|
| 106 |
+
raw_image = image.convert('RGB') if image.mode != 'RGB' else image
|
| 107 |
+
|
| 108 |
+
tensor = self.transform(raw_image).unsqueeze(0).to(self.device)
|
| 109 |
+
|
| 110 |
+
with torch.no_grad():
|
| 111 |
+
logits = self.model(tensor)
|
| 112 |
+
probs = torch.softmax(logits, dim=1)
|
| 113 |
+
pred_class = probs.argmax(dim=1).cpu().item()
|
| 114 |
+
confidence = probs[0, pred_class].cpu().item()
|
| 115 |
+
|
| 116 |
+
return {
|
| 117 |
+
"caligrafia": self.clases[pred_class],
|
| 118 |
+
"confidence": float(confidence),
|
| 119 |
+
"probs": probs[0].cpu().tolist()
|
| 120 |
+
}
|
| 121 |
+
except Exception as e:
|
| 122 |
+
return {"error": str(e)}
|
| 123 |
|
| 124 |
|
| 125 |
|