Vinay15 commited on
Commit
1b4f804
·
verified ·
1 Parent(s): de0d96a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -5,22 +5,21 @@ from PIL import Image
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, use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
8
- model = model.eval() # Remove .cuda() to run on CPU
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(
 
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, use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
8
+ model = model.eval() # Ensure model is set to evaluation mode
9
 
10
+ # Define the OCR function with error handling
11
  def perform_ocr(image):
12
+ try:
13
+ # Convert PIL image to RGB format (if necessary)
14
+ if image.mode != "RGB":
15
+ image = image.convert("RGB")
16
 
17
+ # Perform OCR using the model
18
+ res = model.chat(tokenizer, image, ocr_type='ocr')
 
19
 
20
+ return res
21
+ except Exception as e:
22
+ return f"An error occurred: {str(e)}"
 
23
 
24
  # Define the Gradio interface
25
  interface = gr.Interface(