yashbyname
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,35 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import pytesseract
|
|
|
|
|
4 |
from transformers import AutoTokenizer, AutoModel
|
5 |
|
6 |
-
#
|
7 |
-
pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract'
|
8 |
|
9 |
# Load the tokenizer and model
|
10 |
tokenizer_eng = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
11 |
model_eng = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True).eval()
|
12 |
|
13 |
def perform_ocr(image, language):
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
return "Unsupported language selected."
|
27 |
|
28 |
def ocr_and_search(image, language):
|
29 |
# Call the perform_ocr function
|
30 |
-
|
31 |
-
# You may also want to implement any searching functionality here
|
32 |
-
# ...
|
33 |
|
34 |
-
return
|
35 |
|
36 |
# Create Gradio interface
|
37 |
iface = gr.Interface(
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import pytesseract
|
4 |
+
import cv2
|
5 |
+
import tempfile
|
6 |
from transformers import AutoTokenizer, AutoModel
|
7 |
|
8 |
+
pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract' # Update this if necessary
|
|
|
9 |
|
10 |
# Load the tokenizer and model
|
11 |
tokenizer_eng = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
12 |
model_eng = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True).eval()
|
13 |
|
14 |
def perform_ocr(image, language):
|
15 |
+
# Save the NumPy array as an image file temporarily
|
16 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
17 |
+
temp_filename = temp_file.name
|
18 |
+
cv2.imwrite(temp_filename, image)
|
19 |
+
|
20 |
+
# Perform OCR for English
|
21 |
+
res_eng = model_eng.chat(tokenizer_eng, temp_filename, ocr_type='ocr')
|
22 |
+
|
23 |
+
# Clean up temporary file if needed
|
24 |
+
# os.remove(temp_filename)
|
25 |
+
|
26 |
+
return res_eng # Return results for English
|
|
|
27 |
|
28 |
def ocr_and_search(image, language):
|
29 |
# Call the perform_ocr function
|
30 |
+
english_text = perform_ocr(image, language)
|
|
|
|
|
31 |
|
32 |
+
return english_text # Return the OCR result for English
|
33 |
|
34 |
# Create Gradio interface
|
35 |
iface = gr.Interface(
|