Vinay15 commited on
Commit
d67cd1e
·
verified ·
1 Parent(s): dba283c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -4
app.py CHANGED
@@ -2,6 +2,7 @@ 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 with low memory usage in mind
7
  tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
@@ -15,13 +16,17 @@ model = model.to(device)
15
 
16
  # Function to perform OCR on the image
17
  def perform_ocr(image):
18
- # Open the image using PIL
19
- pil_image = Image.open(image)
 
20
 
21
  # Use torch.no_grad() to avoid unnecessary memory usage
22
  with torch.no_grad():
23
- # Perform OCR using the model (image passed as PIL image)
24
- result = model.chat(tokenizer, pil_image, ocr_type='ocr')
 
 
 
25
 
26
  # Return the extracted text
27
  return result
 
2
  from transformers import AutoModel, AutoTokenizer
3
  from PIL import Image
4
  import gradio as gr
5
+ import os
6
 
7
  # Load the OCR model and tokenizer with low memory usage in mind
8
  tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
 
16
 
17
  # Function to perform OCR on the image
18
  def perform_ocr(image):
19
+ # Save the uploaded image temporarily
20
+ temp_image_path = "temp_image.png"
21
+ image.save(temp_image_path)
22
 
23
  # Use torch.no_grad() to avoid unnecessary memory usage
24
  with torch.no_grad():
25
+ # Perform OCR using the model (pass the file path of the saved image)
26
+ result = model.chat(tokenizer, temp_image_path, ocr_type='ocr')
27
+
28
+ # Clean up the temporary image file
29
+ os.remove(temp_image_path)
30
 
31
  # Return the extracted text
32
  return result