Vinay15 commited on
Commit
9184993
1 Parent(s): fa53aac

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModel, AutoTokenizer
3
+ from PIL import Image
4
+
5
+ # Load the tokenizer and model
6
+ tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
7
+ 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)
8
+ model = model.eval().cuda()
9
+
10
+ # Define the OCR function
11
+ def perform_ocr(image):
12
+ # Convert PIL image to RGB format (if necessary)
13
+ if image.mode != "RGB":
14
+ image = image.convert("RGB")
15
+
16
+ # Save the image to a temporary path
17
+ image_file_path = 'temp_image.jpg'
18
+ image.save(image_file_path)
19
+
20
+ # Perform OCR using the model
21
+ res = model.chat(tokenizer, image_file_path, ocr_type='ocr')
22
+
23
+ return res
24
+
25
+ # Define the Gradio interface
26
+ interface = gr.Interface(
27
+ fn=perform_ocr,
28
+ inputs=gr.Image(type="pil", label="Upload Image"),
29
+ outputs=gr.Textbox(label="Extracted Text"),
30
+ title="OCR and Document Search Web Application",
31
+ description="Upload an image to extract text using the GOT-OCR2_0 model."
32
+ )
33
+
34
+ # Launch the Gradio app
35
+ interface.launch()