Vinay15 commited on
Commit
66ae2fc
·
verified ·
1 Parent(s): f7acb22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -1,18 +1,26 @@
1
  import gradio as gr
 
2
  from transformers import AutoModel, AutoTokenizer
3
  from PIL import Image
4
- import torch
5
 
6
- # Check if CUDA is available
7
  if torch.cuda.is_available():
8
  print("CUDA is available! GPU is present.")
 
 
9
  else:
10
  print("CUDA is not available. Running on CPU.")
11
 
12
  # Load the tokenizer and model
13
  tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
14
- 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)
15
- model = model.eval().cuda() if torch.cuda.is_available() else model.eval()
 
 
 
 
 
 
16
 
17
  # Define the OCR function
18
  def perform_ocr(image):
@@ -20,12 +28,8 @@ def perform_ocr(image):
20
  if image.mode != "RGB":
21
  image = image.convert("RGB")
22
 
23
- # Save the image to a temporary file to pass to the model
24
- temp_image_path = "temp_image.png"
25
- image.save(temp_image_path)
26
-
27
  # Perform OCR using the model
28
- res = model.chat(tokenizer, temp_image_path, ocr_type='ocr')
29
 
30
  return res
31
 
 
1
  import gradio as gr
2
+ import torch
3
  from transformers import AutoModel, AutoTokenizer
4
  from PIL import Image
 
5
 
6
+ # Check GPU availability
7
  if torch.cuda.is_available():
8
  print("CUDA is available! GPU is present.")
9
+ print(f"Number of GPUs: {torch.cuda.device_count()}")
10
+ print(f"GPU Name: {torch.cuda.get_device_name(0)}")
11
  else:
12
  print("CUDA is not available. Running on CPU.")
13
 
14
  # Load the tokenizer and model
15
  tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
16
+
17
+ # Initialize the model
18
+ if torch.cuda.is_available():
19
+ 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)
20
+ model = model.eval().cuda()
21
+ else:
22
+ model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, pad_token_id=tokenizer.eos_token_id)
23
+ model = model.eval() # Keep model on CPU
24
 
25
  # Define the OCR function
26
  def perform_ocr(image):
 
28
  if image.mode != "RGB":
29
  image = image.convert("RGB")
30
 
 
 
 
 
31
  # Perform OCR using the model
32
+ res = model.chat(tokenizer, image, ocr_type='ocr')
33
 
34
  return res
35