Spaces:
Sleeping
Sleeping
Delete handler.py
Browse files- handler.py +0 -40
handler.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import string
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from torchvision import transforms
|
| 5 |
-
import io
|
| 6 |
-
|
| 7 |
-
class EndpointHandler():
|
| 8 |
-
def __init__(self, path=""):
|
| 9 |
-
# TorchScript model load ho raha hai
|
| 10 |
-
self.model = torch.jit.load(f"{path}/master_brain_2026_final.pt")
|
| 11 |
-
self.model.eval()
|
| 12 |
-
self.chars = string.ascii_uppercase + string.ascii_lowercase + string.digits + "@#$%&="
|
| 13 |
-
self.blank_index = len(self.chars)
|
| 14 |
-
self.preprocess = transforms.Compose([
|
| 15 |
-
transforms.Resize((35, 142)),
|
| 16 |
-
transforms.ToTensor(),
|
| 17 |
-
transforms.Normalize((0.5,), (0.5,))
|
| 18 |
-
])
|
| 19 |
-
|
| 20 |
-
def __call__(self, data):
|
| 21 |
-
inputs = data.pop("inputs", data)
|
| 22 |
-
# Base64 ya raw bytes ko handle karne ke liye
|
| 23 |
-
if isinstance(inputs, str):
|
| 24 |
-
import base64
|
| 25 |
-
inputs = Image.open(io.BytesIO(base64.b64decode(inputs))).convert('RGB')
|
| 26 |
-
else:
|
| 27 |
-
inputs = Image.open(io.BytesIO(inputs)).convert('RGB')
|
| 28 |
-
|
| 29 |
-
img_tensor = self.preprocess(inputs).unsqueeze(0)
|
| 30 |
-
with torch.no_grad():
|
| 31 |
-
logits = self.model(img_tensor)
|
| 32 |
-
max_indices = torch.argmax(logits, dim=2).squeeze()
|
| 33 |
-
res = []
|
| 34 |
-
prev = -1
|
| 35 |
-
for idx in max_indices:
|
| 36 |
-
val = idx.item()
|
| 37 |
-
if val != self.blank_index and val != prev:
|
| 38 |
-
res.append(self.chars[val])
|
| 39 |
-
prev = val
|
| 40 |
-
return {"prediction": "".join(res).strip()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|