redhairedshanks1 commited on
Commit
e34d4c6
·
verified ·
1 Parent(s): 7ff8c67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -1,13 +1,32 @@
1
- import gradio as gr
2
- import easyocr
 
 
 
 
 
 
3
 
4
- reader = easyocr.Reader(["en"])
 
 
 
 
 
 
 
 
 
5
 
6
- def ocr_image(image):
7
- results = reader.readtext(image)
8
- return "\n".join([text for _, text, _ in results])
 
9
 
10
- demo = gr.Interface(fn=ocr_image, inputs="image", outputs="text")
 
 
 
11
 
12
- if __name__ == "__main__":
13
- demo.launch()
 
1
+ # import gradio as gr
2
+ # import easyocr
3
+
4
+ # reader = easyocr.Reader(["en"])
5
+
6
+ # def ocr_image(image):
7
+ # results = reader.readtext(image)
8
+ # return "\n".join([text for _, text, _ in results])
9
 
10
+ # demo = gr.Interface(fn=ocr_image, inputs="image", outputs="text")
11
+
12
+ # if __name__ == "__main__":
13
+ # demo.launch()
14
+
15
+ from PIL import Image
16
+ from huggingface_hub import snapshot_download
17
+ from transformers import AutoProcessor, AutoModelForCausalLM
18
+ import gradio as gr
19
+ import torch
20
 
21
+ MODEL_ID = "rednote-hilab/dots.ocr"
22
+ local = snapshot_download(MODEL_ID)
23
+ model = AutoModelForCausalLM.from_pretrained(local, trust_remote_code=True, device_map="auto", torch_dtype=torch.bfloat16)
24
+ processor = AutoProcessor.from_pretrained(local, trust_remote_code=True)
25
 
26
+ def parse_document(image):
27
+ inputs = processor(images=[image], return_tensors="pt").to(model.device)
28
+ output = model.generate(**inputs, do_sample=False, max_new_tokens=1024)
29
+ return processor.batch_decode(output, skip_special_tokens=True)[0]
30
 
31
+ demo = gr.Interface(parse_document, inputs=gr.Image(type="pil"), outputs="text")
32
+ if __name__ == "__main__": demo.launch()