Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from transformers import TrOCRProcessor, VisionEncoderDecoderModel, DonutProcess
|
|
5 |
import torch
|
6 |
import re
|
7 |
import pytesseract
|
|
|
8 |
|
9 |
|
10 |
def predict_arabic(img, model_name="UBC-NLP/Qalam"):
|
@@ -77,6 +78,8 @@ st.set_page_config(
|
|
77 |
|
78 |
# Upload an image and set some options for demo purposes
|
79 |
st.header("Qalam: A Multilingual OCR System")
|
|
|
|
|
80 |
img_file = st.sidebar.file_uploader(label='Upload a file', type=['png', 'jpg'])
|
81 |
realtime_update = st.sidebar.checkbox(label="Update in Real Time", value=True)
|
82 |
# box_color = st.sidebar.color_picker(label="Box Color", value='#0000FF')
|
@@ -86,6 +89,8 @@ aspect_dict = {
|
|
86 |
"Free": None
|
87 |
}
|
88 |
aspect_ratio = aspect_dict[aspect_choice]
|
|
|
|
|
89 |
Lng = st.sidebar.selectbox(label="Language", options=[
|
90 |
"English", "Arabic", "French", "Korean", "Chinese"])
|
91 |
|
@@ -97,7 +102,9 @@ Models = {
|
|
97 |
"Chinese": "Donut"
|
98 |
}
|
99 |
|
100 |
-
st.sidebar.
|
|
|
|
|
101 |
|
102 |
if img_file:
|
103 |
img = Image.open(img_file)
|
@@ -106,7 +113,7 @@ if img_file:
|
|
106 |
|
107 |
col1, col2 = st.columns(2)
|
108 |
with col1:
|
109 |
-
st.
|
110 |
# Get a cropped image from the frontend
|
111 |
cropped_img = st_cropper(
|
112 |
img,
|
@@ -118,24 +125,42 @@ if img_file:
|
|
118 |
|
119 |
with col2:
|
120 |
# Manipulate cropped image at will
|
121 |
-
st.
|
122 |
# _ = cropped_img.thumbnail((150, 150))
|
123 |
st.image(cropped_img)
|
124 |
button = st.button("Run OCR")
|
125 |
if button:
|
126 |
with st.spinner('Running OCR...'):
|
127 |
if Lng == "Arabic":
|
128 |
-
|
129 |
-
st.
|
|
|
|
|
|
|
130 |
elif Lng == "English":
|
131 |
-
|
132 |
-
st.
|
|
|
|
|
|
|
133 |
elif Lng == "French":
|
134 |
-
|
135 |
-
st.
|
|
|
|
|
|
|
136 |
elif Lng == "Korean":
|
137 |
-
|
138 |
-
st.
|
|
|
|
|
|
|
139 |
elif Lng == "Chinese":
|
140 |
-
|
141 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import torch
|
6 |
import re
|
7 |
import pytesseract
|
8 |
+
from io import BytesIO
|
9 |
|
10 |
|
11 |
def predict_arabic(img, model_name="UBC-NLP/Qalam"):
|
|
|
78 |
|
79 |
# Upload an image and set some options for demo purposes
|
80 |
st.header("Qalam: A Multilingual OCR System")
|
81 |
+
st.sidebar.header("Configuration and Image Upload")
|
82 |
+
st.sidebar.subheader("Adjust Image Enhancement Options")
|
83 |
img_file = st.sidebar.file_uploader(label='Upload a file', type=['png', 'jpg'])
|
84 |
realtime_update = st.sidebar.checkbox(label="Update in Real Time", value=True)
|
85 |
# box_color = st.sidebar.color_picker(label="Box Color", value='#0000FF')
|
|
|
89 |
"Free": None
|
90 |
}
|
91 |
aspect_ratio = aspect_dict[aspect_choice]
|
92 |
+
st.sidebar.subheader("Select OCR Language and Model")
|
93 |
+
|
94 |
Lng = st.sidebar.selectbox(label="Language", options=[
|
95 |
"English", "Arabic", "French", "Korean", "Chinese"])
|
96 |
|
|
|
102 |
"Chinese": "Donut"
|
103 |
}
|
104 |
|
105 |
+
st.sidebar.markdown(f"### Selected Model: {Models[Lng]}")
|
106 |
+
|
107 |
+
|
108 |
|
109 |
if img_file:
|
110 |
img = Image.open(img_file)
|
|
|
113 |
|
114 |
col1, col2 = st.columns(2)
|
115 |
with col1:
|
116 |
+
st.subheader("Input: Upload and Crop Your Image")
|
117 |
# Get a cropped image from the frontend
|
118 |
cropped_img = st_cropper(
|
119 |
img,
|
|
|
125 |
|
126 |
with col2:
|
127 |
# Manipulate cropped image at will
|
128 |
+
st.subheader("Output: Preview and Analyze")
|
129 |
# _ = cropped_img.thumbnail((150, 150))
|
130 |
st.image(cropped_img)
|
131 |
button = st.button("Run OCR")
|
132 |
if button:
|
133 |
with st.spinner('Running OCR...'):
|
134 |
if Lng == "Arabic":
|
135 |
+
ocr_text = predict_arabic(cropped_img)
|
136 |
+
st.subheader(f"OCR Results for {Lng}")
|
137 |
+
st.write(ocr_text)
|
138 |
+
text_file = BytesIO(ocr_text.encode())
|
139 |
+
st.download_button('Download Text', text_file, file_name='ocr_text.txt')
|
140 |
elif Lng == "English":
|
141 |
+
ocr_text = predict_english(cropped_img)
|
142 |
+
st.subheader(f"OCR Results for {Lng}")
|
143 |
+
st.write(ocr_text)
|
144 |
+
text_file = BytesIO(ocr_text.encode())
|
145 |
+
st.download_button('Download Text', text_file, file_name='ocr_text.txt')
|
146 |
elif Lng == "French":
|
147 |
+
ocr_text = predict_tesseract(cropped_img)
|
148 |
+
st.subheader(f"OCR Results for {Lng}")
|
149 |
+
st.write(ocr_text)
|
150 |
+
text_file = BytesIO(ocr_text.encode())
|
151 |
+
st.download_button('Download Text', text_file, file_name='ocr_text.txt')
|
152 |
elif Lng == "Korean":
|
153 |
+
ocr_text = predict_english(cropped_img)
|
154 |
+
st.subheader(f"OCR Results for {Lng}")
|
155 |
+
st.write(ocr_text)
|
156 |
+
text_file = BytesIO(ocr_text.encode())
|
157 |
+
st.download_button('Download Text', text_file, file_name='ocr_text.txt')
|
158 |
elif Lng == "Chinese":
|
159 |
+
ocr_text = predict_english(cropped_img)
|
160 |
+
st.subheader(f"OCR Results for {Lng}")
|
161 |
+
st.write(ocr_text)
|
162 |
+
text_file = BytesIO(ocr_text.encode())
|
163 |
+
st.download_button('Download Text', text_file, file_name='ocr_text.txt')
|
164 |
+
|
165 |
+
|
166 |
+
|