Spaces:
Sleeping
Sleeping
Trisandhya
commited on
Commit
·
6fd5cfa
1
Parent(s):
2e45321
app.py code
Browse files- app.py +175 -0
- images/Screenshot 2024-07-23 132549.png +0 -0
- images/Screenshot 2024-09-03 180627.png +0 -0
- images/Screenshot 2024-09-12 160329.png +0 -0
- images/Screenshot 2024-09-16 184639.png +0 -0
- images/Screenshot 2024-09-17 233848.png +0 -0
- images/Screenshot 2024-09-19 165251.png +0 -0
- images/Screenshot 2024-09-19 165319.png +0 -0
- images/Screenshot 2024-09-19 165402.png +0 -0
- images/Screenshot 2024-09-19 165448.png +0 -0
- images/Screenshot 2024-09-24 123142.png +0 -0
- images/Screenshot 2024-09-28 082507.png +0 -0
- images/Screenshot 2024-09-28 100827.png +0 -0
- images/Screenshot 2024-09-28 183112.png +0 -0
- requirements.txt +14 -0
app.py
ADDED
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModel, AutoTokenizer,AutoProcessor
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
from torchvision import io
|
7 |
+
import torchvision.transforms as transforms
|
8 |
+
import random
|
9 |
+
import pandas as pd
|
10 |
+
import easyocr
|
11 |
+
import numpy as np
|
12 |
+
import re
|
13 |
+
|
14 |
+
|
15 |
+
def start():
|
16 |
+
st.session_state.start = True
|
17 |
+
|
18 |
+
def reset():
|
19 |
+
del st.session_state['start']
|
20 |
+
del st.session_state.language
|
21 |
+
|
22 |
+
@st.cache_resource
|
23 |
+
def model():
|
24 |
+
model_path = "D:\IIT-r"
|
25 |
+
tokenize = AutoTokenizer.from_pretrained('srimanth-d/GOT_CPU',trust_remote_code = True)
|
26 |
+
model = AutoModel.from_pretrained('srimanth-d/GOT_CPU',trust_remote_code= True,use_safetensors= True,pad_token_id=tokenize.eos_token_id)
|
27 |
+
model = model.eval()
|
28 |
+
return model, tokenize
|
29 |
+
|
30 |
+
@st.cache_data
|
31 |
+
def get_text(image_file,_model,_tokenizer):
|
32 |
+
res =_model.chat(_tokenizer,image_file,ocr_type= 'ocr')
|
33 |
+
return res
|
34 |
+
|
35 |
+
@st.cache_resource
|
36 |
+
def highlight_keywords(text, keywords):
|
37 |
+
colors = generate_unique_colors(len(keywords))
|
38 |
+
highlighted_text = text
|
39 |
+
found_keywords = []
|
40 |
+
for keyword, color in zip(keywords, colors):
|
41 |
+
if keyword.lower() in text.lower():
|
42 |
+
highlighted_text = highlighted_text.replace(keyword, f'<mark style="background-color: {color};">{keyword}</mark>')
|
43 |
+
found_keywords.append(keyword)
|
44 |
+
return highlighted_text, found_keywords
|
45 |
+
|
46 |
+
@st.cache_data
|
47 |
+
def generate_unique_colors(n):
|
48 |
+
colors = []
|
49 |
+
for i in range(n):
|
50 |
+
color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
|
51 |
+
while color in colors:
|
52 |
+
color = "#{:06x}".format(random.randint(0, 0xFFFFFF))
|
53 |
+
colors.append(color)
|
54 |
+
return colors
|
55 |
+
|
56 |
+
@st.cache_data
|
57 |
+
def extract_text_easyocr(_image):
|
58 |
+
reader = easyocr.Reader(['hi'],gpu = False)
|
59 |
+
results = reader.readtext(np.array(_image))
|
60 |
+
# return results
|
61 |
+
return " ".join([result[1] for result in results])
|
62 |
+
|
63 |
+
|
64 |
+
def search():
|
65 |
+
st.session_state.search = True
|
66 |
+
|
67 |
+
if 'start' not in st.session_state:
|
68 |
+
st.session_state.start = False
|
69 |
+
|
70 |
+
if 'search' not in st.session_state:
|
71 |
+
st.session_state.search = False
|
72 |
+
|
73 |
+
if 'reset' not in st.session_state:
|
74 |
+
st.session_state.reset = False
|
75 |
+
|
76 |
+
if 'language' not in st.session_state:
|
77 |
+
st.session_state.language = False
|
78 |
+
|
79 |
+
st.header("Optical Character Recognition ")
|
80 |
+
col1, col2 = st.columns(2)
|
81 |
+
|
82 |
+
with col1:
|
83 |
+
if st.button('English'):
|
84 |
+
|
85 |
+
st.session_state.language = 'English'
|
86 |
+
st.rerun()
|
87 |
+
|
88 |
+
|
89 |
+
with col2:
|
90 |
+
if st.button('Hindi'):
|
91 |
+
|
92 |
+
st.session_state.language = 'Hindi'
|
93 |
+
st.rerun()
|
94 |
+
|
95 |
+
|
96 |
+
if st.session_state.language == 'English':
|
97 |
+
st.title("GOT OCR - Extract Text from Images")
|
98 |
+
st.write("Upload an image and let the GOT model extract the text!")
|
99 |
+
|
100 |
+
try:
|
101 |
+
MODEL, TOKENIZER = model()
|
102 |
+
st.success("GOT model loaded successfully")
|
103 |
+
except Exception as e:
|
104 |
+
st.error(f"Error loading GOT model: {str(e)}")
|
105 |
+
|
106 |
+
image_file = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
107 |
+
|
108 |
+
if image_file is not None:
|
109 |
+
st.image(image_file, caption="Uploaded Image", use_column_width=True)
|
110 |
+
|
111 |
+
if not os.path.exists("images"):
|
112 |
+
os.makedirs("images")
|
113 |
+
with open(f"images/{image_file.name}", "wb") as f:
|
114 |
+
f.write(image_file.getbuffer())
|
115 |
+
|
116 |
+
extracted_text = get_text(f"images/{image_file.name}", MODEL, TOKENIZER)
|
117 |
+
# st.session_state.extracted_text = extracted_text
|
118 |
+
|
119 |
+
st.subheader("Extracted Text:")
|
120 |
+
st.write(extracted_text)
|
121 |
+
|
122 |
+
keywords_input = st.text_input("Enter keywords to search within the extracted text (comma-separated):")
|
123 |
+
if keywords_input:
|
124 |
+
keywords = [keyword.strip().lower() for keyword in keywords_input.split(',')] # Convert keywords to lowercase
|
125 |
+
lower_extracted_text = extracted_text.lower() # Convert extracted text to lowercase
|
126 |
+
highlighted_text, found_keywords = highlight_keywords(lower_extracted_text, keywords)
|
127 |
+
st.button("Search", on_click=search)
|
128 |
+
if st.session_state.search:
|
129 |
+
if found_keywords:
|
130 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
131 |
+
st.write(f"Found keywords: {', '.join(found_keywords)}")
|
132 |
+
else:
|
133 |
+
st.warning("No keywords found in the extracted text.")
|
134 |
+
|
135 |
+
not_found_keywords = set(keywords) - set(found_keywords)
|
136 |
+
if not_found_keywords:
|
137 |
+
st.error(f"Keywords not found: {', '.join(not_found_keywords)}")
|
138 |
+
|
139 |
+
st.button("Reset and Upload New Image",on_click=reset)
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
elif st.session_state.language == 'Hindi':
|
144 |
+
st.title("HINDI OCR - Extract Text from Images")
|
145 |
+
st.write("Upload an image and let EasyOCR extract the text!")
|
146 |
+
|
147 |
+
|
148 |
+
image_file_hi = st.file_uploader("Upload an Image", type=["jpg", "png", "jpeg"])
|
149 |
+
|
150 |
+
if image_file_hi:
|
151 |
+
st.image(image_file_hi, caption="Uploaded Image", use_column_width=True)
|
152 |
+
|
153 |
+
image = Image.open(image_file_hi)
|
154 |
+
extracted_text_hindi =extract_text_easyocr(image)
|
155 |
+
|
156 |
+
st.subheader("Extracted Text:")
|
157 |
+
st.write(extracted_text_hindi)
|
158 |
+
|
159 |
+
keywords_input = st.text_input("Enter keywords to search within the extracted text (comma-separated):")
|
160 |
+
if keywords_input:
|
161 |
+
keywords = [keyword.strip() for keyword in keywords_input.split(',')]
|
162 |
+
highlighted_text, found_keywords = highlight_keywords(extracted_text_hindi, keywords)
|
163 |
+
|
164 |
+
st.subheader("Search Results:")
|
165 |
+
if found_keywords:
|
166 |
+
st.markdown(highlighted_text, unsafe_allow_html=True)
|
167 |
+
st.write(f"Found keywords: {', '.join(found_keywords)}")
|
168 |
+
else:
|
169 |
+
st.warning("No keywords found in the extracted text.")
|
170 |
+
|
171 |
+
not_found_keywords = set(keywords) - set(found_keywords)
|
172 |
+
if not_found_keywords:
|
173 |
+
st.error(f"Keywords not found: {', '.join(not_found_keywords)}")
|
174 |
+
|
175 |
+
st.button("Reset and Upload New Image",on_click=reset)
|
images/Screenshot 2024-07-23 132549.png
ADDED
images/Screenshot 2024-09-03 180627.png
ADDED
images/Screenshot 2024-09-12 160329.png
ADDED
images/Screenshot 2024-09-16 184639.png
ADDED
images/Screenshot 2024-09-17 233848.png
ADDED
images/Screenshot 2024-09-19 165251.png
ADDED
images/Screenshot 2024-09-19 165319.png
ADDED
images/Screenshot 2024-09-19 165402.png
ADDED
images/Screenshot 2024-09-19 165448.png
ADDED
images/Screenshot 2024-09-24 123142.png
ADDED
images/Screenshot 2024-09-28 082507.png
ADDED
images/Screenshot 2024-09-28 100827.png
ADDED
images/Screenshot 2024-09-28 183112.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
torchaudio
|
4 |
+
transformers
|
5 |
+
Pillow
|
6 |
+
verovio
|
7 |
+
tiktoken
|
8 |
+
importlib_resources
|
9 |
+
accelerate>=0.26.0
|
10 |
+
safetensors
|
11 |
+
streamlit
|
12 |
+
easyocr
|
13 |
+
numpy
|
14 |
+
pandas
|