mahesh006 commited on
Commit
5bdb400
·
verified ·
1 Parent(s): 805d7a9

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +88 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModel, AutoTokenizer
3
+ import torch
4
+ from PIL import Image
5
+ import tempfile
6
+ import easyocr
7
+ import re
8
+
9
+
10
+ # Load the pre-trained English OCR model and tokenizer
11
+ @st.cache_resource
12
+ def load_english_model():
13
+ tokenizer = AutoTokenizer.from_pretrained('srimanth-d/GOT_CPU', trust_remote_code=True)
14
+ model = AutoModel.from_pretrained('srimanth-d/GOT_CPU', trust_remote_code=True, low_cpu_mem_usage=True, use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
15
+ model = model.eval()
16
+ return tokenizer, model
17
+
18
+ # Load the multilingual OCR model (EasyOCR) for both Hindi and English
19
+ @st.cache_resource
20
+ def load_multilingual_model():
21
+ reader = easyocr.Reader(['en', 'hi'])
22
+ return reader
23
+
24
+ st.title('OCR Web App')
25
+
26
+ if 'ocr_result' not in st.session_state:
27
+ st.session_state['ocr_result'] = ""
28
+
29
+ # Function to highlight the search word in the OCR results
30
+ def highlight_text(text, search_query):
31
+ if not search_query:
32
+ return text
33
+ highlighted = re.sub(f"({re.escape(search_query)})", r'<mark>\1</mark>', text, flags=re.IGNORECASE)
34
+ return highlighted
35
+
36
+ # Create a two-column layout
37
+ left_column, right_column = st.columns([1, 2])
38
+
39
+ with left_column:
40
+ language = st.selectbox("Select Language", ["English", "Hindi + English"])
41
+
42
+ predict_button = st.button('Predict')
43
+
44
+ # Search functionality after results
45
+ search_query = st.text_input("Search in results")
46
+ search_button = st.button('Search')
47
+
48
+ # Display search results by highlighting the searched word
49
+ if search_button and st.session_state['ocr_result']:
50
+ if search_query.lower() in st.session_state['ocr_result'].lower():
51
+ st.success(f"'{search_query}' found in the OCR results!")
52
+ else:
53
+ st.error(f"'{search_query}' not found in the OCR results.")
54
+
55
+ with right_column:
56
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
57
+
58
+ if uploaded_file is not None:
59
+ image = Image.open(uploaded_file)
60
+ st.image(image, caption="Uploaded Image", use_column_width=True)
61
+
62
+ img_format = image.format if image.format is not None else "JPEG"
63
+ with tempfile.NamedTemporaryFile(delete=False, suffix=f".{img_format.lower()}") as tmp_file:
64
+ img_path = tmp_file.name
65
+ image.save(img_path, format=img_format)
66
+
67
+ # Perform OCR when the Predict button is clicked
68
+ if predict_button:
69
+ if language == "English":
70
+ tokenizer, model = load_english_model()
71
+ with st.spinner('Processing English OCR...'):
72
+ res = model.chat(tokenizer, img_path, ocr_type='ocr')
73
+ st.session_state['ocr_result'] = res
74
+ st.write("OCR Result (English):")
75
+ st.write(st.session_state['ocr_result'], unsafe_allow_html=True)
76
+
77
+ elif language == "Hindi + English":
78
+ reader = load_multilingual_model()
79
+ with st.spinner('Processing Hindi + English OCR...'):
80
+ result = reader.readtext(img_path, detail=0)
81
+ st.session_state['ocr_result'] = " ".join(result)
82
+ st.write("OCR Result (Hindi + English):")
83
+ st.write(st.session_state['ocr_result'], unsafe_allow_html=True)
84
+
85
+ if st.session_state['ocr_result']:
86
+ highlighted_result = highlight_text(st.session_state['ocr_result'], search_query)
87
+ st.write("Highlighted OCR Result:")
88
+ st.markdown(highlighted_result, unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ streamlit==1.25.0
2
+ transformers==4.37.2
3
+ torch==2.0.1
4
+ torchvision==0.15.2
5
+ Pillow
6
+ easyocr==1.7.0
7
+ tiktoken
8
+ verovio
9
+ accelerate==0.28.0