DeepDiveDev commited on
Commit
1133214
·
verified ·
1 Parent(s): dd3b7a5

Upload app_English.py

Browse files
Files changed (1) hide show
  1. app_English.py +61 -0
app_English.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModel, AutoTokenizer
3
+ from PIL import Image
4
+ import tempfile
5
+ import os
6
+
7
+
8
+
9
+ # Load the model and tokenizer
10
+ tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
11
+ model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
12
+ model = model.eval().cuda()
13
+
14
+ # Streamlit App Title
15
+ st.title("OCR with GOT-OCR2")
16
+
17
+ # File uploader for image input
18
+ image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
19
+
20
+ if image_file is not None:
21
+ # Display the uploaded image
22
+ image = Image.open(image_file)
23
+ st.image(image, caption='Uploaded Image', use_column_width=True)
24
+
25
+ # Save the uploaded file to a temporary file
26
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
27
+ temp_file.write(image_file.getvalue())
28
+ temp_file_path = temp_file.name
29
+
30
+ # Button to run OCR
31
+ if st.button("Run OCR"):
32
+ # Perform plain text OCR
33
+ res_plain = model.chat(tokenizer, temp_file_path, ocr_type='ocr')
34
+
35
+ # Perform formatted text OCR
36
+ res_format = model.chat(tokenizer, temp_file_path, ocr_type='format')
37
+
38
+ # Display the results
39
+ st.subheader("Plain Text OCR Results:")
40
+ st.write(res_plain)
41
+
42
+
43
+
44
+ st.subheader("Formatted Text OCR Results:")
45
+ st.write(res_format)
46
+
47
+ # You can add more OCR types as needed
48
+ # e.g., fine-grained OCR
49
+ res_fine_grained = model.chat(tokenizer, temp_file_path, ocr_type='ocr', ocr_box='')
50
+ st.subheader("Fine-Grained OCR Results:")
51
+ st.write(res_fine_grained)
52
+
53
+ # Render formatted OCR to HTML
54
+ res_render = model.chat(tokenizer, temp_file_path, ocr_type='format', render=True, save_render_file='./demo.html')
55
+ st.subheader("Rendered OCR Results (HTML):")
56
+ st.write(res_render)
57
+
58
+ # Clean up the temporary file after use
59
+ os.remove(temp_file_path)
60
+
61
+ # Note: No need for if __name__ == "__main__": st.run()