Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,14 +5,31 @@ import tempfile
|
|
5 |
import os
|
6 |
import easyocr
|
7 |
import re
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Load EasyOCR reader with English and Hindi language support
|
10 |
reader = easyocr.Reader(['en', 'hi']) # 'en' for English, 'hi' for Hindi
|
11 |
|
12 |
# Load the GOT-OCR2 model and tokenizer
|
13 |
tokenizer = AutoTokenizer.from_pretrained('stepfun-ai/GOT-OCR2_0', trust_remote_code=True)
|
14 |
-
|
15 |
-
model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Load MarianMT translation model for Hindi to English translation
|
18 |
translation_tokenizer = MarianTokenizer.from_pretrained('Helsinki-NLP/opus-mt-hi-en')
|
@@ -43,8 +60,12 @@ if image_file is not None:
|
|
43 |
|
44 |
# Button to run OCR
|
45 |
if st.button("Run OCR"):
|
46 |
-
#
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
|
49 |
# Perform formatted text OCR
|
50 |
res_format = model.chat(tokenizer, temp_file_path, ocr_type='format')
|
@@ -99,3 +120,4 @@ if image_file is not None:
|
|
99 |
os.remove(temp_file_path)
|
100 |
|
101 |
# Note: No need for if __name__ == "__main__": st.run()
|
|
|
|
5 |
import os
|
6 |
import easyocr
|
7 |
import re
|
8 |
+
import torch
|
9 |
+
|
10 |
+
# Check if GPU is available, else default to CPU
|
11 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
12 |
+
st.write(f"Using device: {device.upper()}")
|
13 |
|
14 |
# Load EasyOCR reader with English and Hindi language support
|
15 |
reader = easyocr.Reader(['en', 'hi']) # 'en' for English, 'hi' for Hindi
|
16 |
|
17 |
# Load the GOT-OCR2 model and tokenizer
|
18 |
tokenizer = AutoTokenizer.from_pretrained('stepfun-ai/GOT-OCR2_0', trust_remote_code=True)
|
19 |
+
|
20 |
+
# Load the model with low memory usage on CPU or auto-map for GPU if available
|
21 |
+
model = AutoModel.from_pretrained(
|
22 |
+
'stepfun-ai/GOT-OCR2_0',
|
23 |
+
trust_remote_code=True,
|
24 |
+
low_cpu_mem_usage=True,
|
25 |
+
device_map='auto' if device == 'cuda' else None, # Use GPU if available, else None
|
26 |
+
use_safetensors=True,
|
27 |
+
pad_token_id=tokenizer.eos_token_id
|
28 |
+
)
|
29 |
+
|
30 |
+
# Move model to appropriate device (GPU or CPU)
|
31 |
+
model = model.to(device)
|
32 |
+
model = model.eval()
|
33 |
|
34 |
# Load MarianMT translation model for Hindi to English translation
|
35 |
translation_tokenizer = MarianTokenizer.from_pretrained('Helsinki-NLP/opus-mt-hi-en')
|
|
|
60 |
|
61 |
# Button to run OCR
|
62 |
if st.button("Run OCR"):
|
63 |
+
# Ensure model runs on CPU if GPU isn't available
|
64 |
+
if device == 'cuda':
|
65 |
+
res_plain = model.chat(tokenizer, temp_file_path, ocr_type='ocr')
|
66 |
+
else:
|
67 |
+
with torch.no_grad(): # Disable gradient calculations to save memory on CPU
|
68 |
+
res_plain = model.chat(tokenizer, temp_file_path, ocr_type='ocr')
|
69 |
|
70 |
# Perform formatted text OCR
|
71 |
res_format = model.chat(tokenizer, temp_file_path, ocr_type='format')
|
|
|
120 |
os.remove(temp_file_path)
|
121 |
|
122 |
# Note: No need for if __name__ == "__main__": st.run()
|
123 |
+
|