Exceltic commited on
Commit
186f7d8
·
verified ·
1 Parent(s): aa66722

Fix app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -42
app.py CHANGED
@@ -7,74 +7,61 @@ from tokenizer_base import Tokenizer
7
  import pathlib
8
  import os
9
  import gradio as gr
10
- from huggingface_hub import Repository
11
-
12
- # repo = Repository(
13
- # local_dir="secret_models",
14
- # repo_type="model",
15
- # clone_from="docparser/captcha",
16
- # token=True
17
- # )
18
- # repo.git_pull()
19
 
20
  cwd = pathlib.Path(__file__).parent.resolve()
21
- model_file = os.path.join(cwd,"secret_models","captcha.onnx")
22
- img_size = (32,128)
23
  charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
24
  tokenizer_base = Tokenizer(charset)
25
 
26
  def get_transform(img_size):
27
- transforms = []
28
- transforms.extend([
29
- T.Resize(img_size, T.InterpolationMode.BICUBIC),
30
- T.ToTensor(),
31
- T.Normalize(0.5, 0.5)
32
- ])
33
- return T.Compose(transforms)
34
 
35
  def to_numpy(tensor):
36
  return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
37
 
38
  def initialize_model(model_file):
39
  transform = get_transform(img_size)
40
- # Onnx model loading
41
- onnx_model = onnx.load(model_file)
42
- onnx.checker.check_model(onnx_model)
43
  ort_session = rt.InferenceSession(model_file)
44
- return transform,ort_session
45
 
46
  def get_text(img_org):
47
  try:
48
- # img_org = Image.open(image_path)
49
- # Preprocess. Model expects a batch of images with shape: (B, C, H, W)
50
  x = transform(img_org.convert('RGB')).unsqueeze(0)
51
-
52
- # compute ONNX Runtime output prediction
53
  ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
54
  logits = ort_session.run(None, ort_inputs)[0]
55
  probs = torch.tensor(logits).softmax(-1)
56
  preds, probs = tokenizer_base.decode(probs)
57
- preds = preds[0]
58
- print(preds)
59
- return preds
60
  except Exception as e:
61
- print(str(e))
62
 
63
- transform,ort_session = initialize_model(model_file=model_file)
 
64
 
65
- gr.Interface(
66
- get_text,
 
67
  inputs=gr.Image(type="pil"),
68
  outputs=gr.Textbox(),
69
  title="Text Captcha Reader",
70
  examples=["8000.png","11JW29.png","2a8486.jpg","2nbcx.png",
71
- "000679.png","000HU.png","00Uga.png.jpg","00bAQwhAZU.jpg",
72
- "00h57kYf.jpg","0EoHdtVb.png","0JS21.png","0p98z.png","10010.png"]
73
- ).launch(server_name="0.0.0.0", server_port=7860)
74
-
75
- # if __name__ == "__main__":
76
- # image_path = "8000.png"
77
- # preds,probs = get_text(image_path)
78
- # print(preds[0])
79
-
80
 
 
 
 
 
 
 
 
 
7
  import pathlib
8
  import os
9
  import gradio as gr
 
 
 
 
 
 
 
 
 
10
 
11
  cwd = pathlib.Path(__file__).parent.resolve()
12
+ model_file = os.path.join(cwd, "secret_models", "captcha.onnx")
13
+ img_size = (32, 128)
14
  charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
15
  tokenizer_base = Tokenizer(charset)
16
 
17
  def get_transform(img_size):
18
+ transforms = []
19
+ transforms.extend([
20
+ T.Resize(img_size, T.InterpolationMode.BICUBIC),
21
+ T.ToTensor(),
22
+ T.Normalize(0.5, 0.5)
23
+ ])
24
+ return T.Compose(transforms)
25
 
26
  def to_numpy(tensor):
27
  return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
28
 
29
  def initialize_model(model_file):
30
  transform = get_transform(img_size)
 
 
 
31
  ort_session = rt.InferenceSession(model_file)
32
+ return transform, ort_session
33
 
34
  def get_text(img_org):
35
  try:
36
+ if img_org is None:
37
+ return ""
38
  x = transform(img_org.convert('RGB')).unsqueeze(0)
 
 
39
  ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
40
  logits = ort_session.run(None, ort_inputs)[0]
41
  probs = torch.tensor(logits).softmax(-1)
42
  preds, probs = tokenizer_base.decode(probs)
43
+ return preds[0]
 
 
44
  except Exception as e:
45
+ return str(e)
46
 
47
+ # Inicializamos el modelo una sola vez
48
+ transform, ort_session = initialize_model(model_file=model_file)
49
 
50
+ # Creamos la interfaz
51
+ demo = gr.Interface(
52
+ fn=get_text,
53
  inputs=gr.Image(type="pil"),
54
  outputs=gr.Textbox(),
55
  title="Text Captcha Reader",
56
  examples=["8000.png","11JW29.png","2a8486.jpg","2nbcx.png",
57
+ "000679.png","000HU.png","00Uga.png.jpg","00bAQwhAZU.jpg",
58
+ "00h57kYf.jpg","0EoHdtVb.png","0JS21.png","0p98z.png","10010.png"]
59
+ )
 
 
 
 
 
 
60
 
61
+ # Lanzamiento optimizado para Hugging Face Spaces
62
+ if __name__ == "__main__":
63
+ demo.launch(
64
+ server_name="0.0.0.0",
65
+ server_port=7860,
66
+ show_api=False
67
+ )