Vinay15 commited on
Commit
9fca578
·
verified ·
1 Parent(s): b42b1aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -1,32 +1,33 @@
1
  import torch
2
  from transformers import AutoModel, AutoTokenizer
3
  from PIL import Image
4
- import gradio as gr
5
 
6
- # Load the OCR model and tokenizer
7
  tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
8
  model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True,
9
  low_cpu_mem_usage=True,
10
  pad_token_id=tokenizer.eos_token_id).eval()
11
 
12
- # Check if GPU is available and use it, else use CPU
13
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
14
  model = model.to(device)
15
 
16
  # Function to perform OCR on the image
17
- def perform_ocr(image):
18
- # Perform OCR using the model
19
- result = model.chat(tokenizer, image, ocr_type='ocr')
 
 
 
 
 
 
 
20
  return result
21
 
22
- # Create the Gradio interface using the new syntax
23
- interface = gr.Interface(
24
- fn=perform_ocr,
25
- inputs=gr.Image(type="pil"), # Updated to gr.Image
26
- outputs=gr.Textbox(), # Updated to gr.Textbox
27
- title="OCR Web App",
28
- description="Upload an image to extract text using the GOT-OCR2.0 model."
29
- )
30
 
31
- # Launch the app
32
- interface.launch(share=True)
 
1
  import torch
2
  from transformers import AutoModel, AutoTokenizer
3
  from PIL import Image
 
4
 
5
+ # Load the OCR model and tokenizer with low memory usage in mind
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,
8
  low_cpu_mem_usage=True,
9
  pad_token_id=tokenizer.eos_token_id).eval()
10
 
11
+ # Ensure we are using CPU
12
+ device = torch.device('cpu')
13
  model = model.to(device)
14
 
15
  # Function to perform OCR on the image
16
+ def perform_ocr(image_path):
17
+ # Open the image file
18
+ image = Image.open(image_path)
19
+
20
+ # Use torch.no_grad() to avoid unnecessary memory usage
21
+ with torch.no_grad():
22
+ # Perform OCR using the model
23
+ result = model.chat(tokenizer, image_path, ocr_type='ocr')
24
+
25
+ # Return the extracted text
26
  return result
27
 
28
+ # Example usage with an image file path
29
+ image_path = "/content/id.jpg"
30
+ extracted_text = perform_ocr(image_path)
 
 
 
 
 
31
 
32
+ # Output the extracted text
33
+ print("Extracted Text:", extracted_text)