Futi613 commited on
Commit
b8669eb
·
verified ·
1 Parent(s): b410914

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -47
app.py CHANGED
@@ -6,31 +6,36 @@ from PIL import Image
6
  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()
@@ -41,40 +46,51 @@ def initialize_model(model_file):
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()
74
 
75
- # if __name__ == "__main__":
76
- # image_path = "8000.png"
77
- # preds,probs = get_text(image_path)
78
- # print(preds[0])
79
-
80
 
 
 
 
 
6
  from tokenizer_base import Tokenizer
7
  import pathlib
8
  import os
9
+ from fastapi import FastAPI, HTTPException
10
+ from pydantic import BaseModel
11
+ import base64
12
+ from io import BytesIO
13
  from huggingface_hub import Repository
14
 
15
+ repo = Repository(
16
+ local_dir="secret_models",
17
+ repo_type="model",
18
+ clone_from="docparser/captcha",
19
+ token=True
20
+ )
21
+ repo.git_pull()
22
 
23
  cwd = pathlib.Path(__file__).parent.resolve()
24
+ model_file = os.path.join(cwd, "secret_models", "captcha.onnx")
25
+ img_size = (32, 128)
26
  charset = r"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
27
  tokenizer_base = Tokenizer(charset)
28
 
29
+ app = FastAPI(title="Text Captcha Reader API")
30
+
31
  def get_transform(img_size):
32
+ transforms = []
33
+ transforms.extend([
34
+ T.Resize(img_size, T.InterpolationMode.BICUBIC),
35
+ T.ToTensor(),
36
+ T.Normalize(0.5, 0.5)
37
+ ])
38
+ return T.Compose(transforms)
39
 
40
  def to_numpy(tensor):
41
  return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
 
46
  onnx_model = onnx.load(model_file)
47
  onnx.checker.check_model(onnx_model)
48
  ort_session = rt.InferenceSession(model_file)
49
+ return transform, ort_session
50
 
51
  def get_text(img_org):
52
+ # Preprocess. Model expects a batch of images with shape: (B, C, H, W)
53
+ x = transform(img_org.convert('RGB')).unsqueeze(0)
54
+
55
+ # compute ONNX Runtime output prediction
56
+ ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
57
+ logits = ort_session.run(None, ort_inputs)[0]
58
+ probs = torch.tensor(logits).softmax(-1)
59
+ preds, probs = tokenizer_base.decode(probs)
60
+ preds = preds[0]
61
+ print(preds)
62
+ return preds
63
+
64
+ # Initialize model at startup
65
+ transform, ort_session = initialize_model(model_file=model_file)
66
+
67
+ # Pydantic model for request
68
+ class ImageRequest(BaseModel):
69
+ image: str # base64 encoded image
70
+
71
+ # Pydantic model for response
72
+ class TextResponse(BaseModel):
73
+ text: str
74
+
75
+ @app.post("/predict", response_model=TextResponse)
76
+ async def predict_captcha(request: ImageRequest):
77
  try:
78
+ # Decode base64 image
79
+ image_data = base64.b64decode(request.image)
80
+ img = Image.open(BytesIO(image_data))
81
+
82
+ # Get prediction
83
+ text = get_text(img)
84
+
85
+ return TextResponse(text=text)
86
 
 
 
 
 
 
 
 
 
87
  except Exception as e:
88
+ raise HTTPException(status_code=400, detail=f"Error processing image: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
+ @app.get("/health")
91
+ async def health_check():
92
+ return {"status": "ok"}
 
 
93
 
94
+ if __name__ == "__main__":
95
+ import uvicorn
96
+ uvicorn.run(app, host="0.0.0.0", port=8000)