2024-02-11 08:06:50 +00:00
|
|
|
|
import os
|
|
|
|
|
|
import io
|
2024-05-27 17:03:48 +00:00
|
|
|
|
import re
|
2024-02-11 08:06:50 +00:00
|
|
|
|
import base64
|
|
|
|
|
|
import tempfile
|
2024-03-18 15:48:04 +00:00
|
|
|
|
import shutil
|
2024-02-11 08:06:50 +00:00
|
|
|
|
import streamlit as st
|
|
|
|
|
|
|
2024-04-06 07:27:27 +00:00
|
|
|
|
from PIL import Image
|
2024-04-17 09:12:07 +00:00
|
|
|
|
from streamlit_paste_button import paste_image_button as pbutton
|
2024-04-21 00:05:14 +08:00
|
|
|
|
from onnxruntime import InferenceSession
|
2024-05-27 17:03:48 +00:00
|
|
|
|
from models.thrid_party.paddleocr.infer import predict_det, predict_rec
|
|
|
|
|
|
from models.thrid_party.paddleocr.infer import utility
|
2024-04-21 00:05:14 +08:00
|
|
|
|
|
|
|
|
|
|
from models.utils import mix_inference
|
|
|
|
|
|
from models.det_model.inference import PredictConfig
|
|
|
|
|
|
|
2024-02-11 08:06:50 +00:00
|
|
|
|
from models.ocr_model.model.TexTeller import TexTeller
|
2024-04-21 00:05:14 +08:00
|
|
|
|
from models.ocr_model.utils.inference import inference as latex_recognition
|
|
|
|
|
|
from models.ocr_model.utils.to_katex import to_katex
|
2024-02-11 08:06:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 09:12:07 +00:00
|
|
|
|
st.set_page_config(
|
|
|
|
|
|
page_title="TexTeller",
|
|
|
|
|
|
page_icon="🧮"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-03-18 15:48:04 +00:00
|
|
|
|
html_string = '''
|
|
|
|
|
|
<h1 style="color: black; text-align: center;">
|
2024-04-17 09:12:07 +00:00
|
|
|
|
<img src="https://raw.githubusercontent.com/OleehyO/TexTeller/main/assets/fire.svg" width="100">
|
|
|
|
|
|
𝚃𝚎𝚡𝚃𝚎𝚕𝚕𝚎𝚛
|
|
|
|
|
|
<img src="https://raw.githubusercontent.com/OleehyO/TexTeller/main/assets/fire.svg" width="100">
|
2024-03-18 15:48:04 +00:00
|
|
|
|
</h1>
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
suc_gif_html = '''
|
|
|
|
|
|
<h1 style="color: black; text-align: center;">
|
|
|
|
|
|
<img src="https://slackmojis.com/emojis/90621-clapclap-e/download" width="50">
|
|
|
|
|
|
<img src="https://slackmojis.com/emojis/90621-clapclap-e/download" width="50">
|
|
|
|
|
|
<img src="https://slackmojis.com/emojis/90621-clapclap-e/download" width="50">
|
|
|
|
|
|
</h1>
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
fail_gif_html = '''
|
|
|
|
|
|
<h1 style="color: black; text-align: center;">
|
|
|
|
|
|
<img src="https://slackmojis.com/emojis/51439-allthethings_intensifies/download" >
|
|
|
|
|
|
<img src="https://slackmojis.com/emojis/51439-allthethings_intensifies/download" >
|
|
|
|
|
|
<img src="https://slackmojis.com/emojis/51439-allthethings_intensifies/download" >
|
|
|
|
|
|
</h1>
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
2024-02-11 08:06:50 +00:00
|
|
|
|
@st.cache_resource
|
2024-06-22 21:51:51 +08:00
|
|
|
|
def get_texteller(use_onnx, accelerator):
|
|
|
|
|
|
return TexTeller.from_pretrained(os.environ['CHECKPOINT_DIR'], use_onnx=use_onnx, onnx_provider=accelerator)
|
2024-02-11 08:06:50 +00:00
|
|
|
|
|
|
|
|
|
|
@st.cache_resource
|
|
|
|
|
|
def get_tokenizer():
|
|
|
|
|
|
return TexTeller.get_tokenizer(os.environ['TOKENIZER_DIR'])
|
|
|
|
|
|
|
2024-04-21 00:05:14 +08:00
|
|
|
|
@st.cache_resource
|
2024-06-22 21:51:51 +08:00
|
|
|
|
def get_det_models(accelerator):
|
2024-04-21 00:05:14 +08:00
|
|
|
|
infer_config = PredictConfig("./models/det_model/model/infer_cfg.yml")
|
2024-06-22 21:51:51 +08:00
|
|
|
|
latex_det_model = InferenceSession(
|
|
|
|
|
|
"./models/det_model/model/rtdetr_r50vd_6x_coco.onnx",
|
|
|
|
|
|
providers=['CUDAExecutionProvider'] if accelerator == 'cuda' else ['CPUExecutionProvider']
|
|
|
|
|
|
)
|
2024-04-21 00:05:14 +08:00
|
|
|
|
return infer_config, latex_det_model
|
|
|
|
|
|
|
|
|
|
|
|
@st.cache_resource()
|
2024-05-27 17:03:48 +00:00
|
|
|
|
def get_ocr_models(accelerator):
|
|
|
|
|
|
use_gpu = accelerator == 'cuda'
|
|
|
|
|
|
|
|
|
|
|
|
SIZE_LIMIT = 20 * 1024 * 1024
|
|
|
|
|
|
det_model_dir = "./models/thrid_party/paddleocr/checkpoints/det/default_model.onnx"
|
|
|
|
|
|
rec_model_dir = "./models/thrid_party/paddleocr/checkpoints/rec/default_model.onnx"
|
|
|
|
|
|
# The CPU inference of the detection model will be faster than the GPU inference (in onnxruntime)
|
|
|
|
|
|
det_use_gpu = False
|
|
|
|
|
|
rec_use_gpu = use_gpu and not (os.path.getsize(rec_model_dir) < SIZE_LIMIT)
|
|
|
|
|
|
|
|
|
|
|
|
paddleocr_args = utility.parse_args()
|
|
|
|
|
|
paddleocr_args.use_onnx = True
|
|
|
|
|
|
paddleocr_args.det_model_dir = det_model_dir
|
|
|
|
|
|
paddleocr_args.rec_model_dir = rec_model_dir
|
|
|
|
|
|
|
|
|
|
|
|
paddleocr_args.use_gpu = det_use_gpu
|
|
|
|
|
|
detector = predict_det.TextDetector(paddleocr_args)
|
|
|
|
|
|
paddleocr_args.use_gpu = rec_use_gpu
|
|
|
|
|
|
recognizer = predict_rec.TextRecognizer(paddleocr_args)
|
|
|
|
|
|
return [detector, recognizer]
|
|
|
|
|
|
|
2024-04-21 00:05:14 +08:00
|
|
|
|
|
2024-03-18 15:48:04 +00:00
|
|
|
|
def get_image_base64(img_file):
|
|
|
|
|
|
buffered = io.BytesIO()
|
|
|
|
|
|
img_file.seek(0)
|
|
|
|
|
|
img = Image.open(img_file)
|
|
|
|
|
|
img.save(buffered, format="PNG")
|
|
|
|
|
|
return base64.b64encode(buffered.getvalue()).decode()
|
|
|
|
|
|
|
2024-04-17 09:12:07 +00:00
|
|
|
|
def on_file_upload():
|
|
|
|
|
|
st.session_state["UPLOADED_FILE_CHANGED"] = True
|
|
|
|
|
|
|
|
|
|
|
|
def change_side_bar():
|
|
|
|
|
|
st.session_state["CHANGE_SIDEBAR_FLAG"] = True
|
|
|
|
|
|
|
2024-03-18 15:48:04 +00:00
|
|
|
|
if "start" not in st.session_state:
|
|
|
|
|
|
st.session_state["start"] = 1
|
2024-04-06 07:27:27 +00:00
|
|
|
|
st.toast('Hooray!', icon='🎉')
|
2024-02-11 08:06:50 +00:00
|
|
|
|
|
2024-04-17 09:12:07 +00:00
|
|
|
|
if "UPLOADED_FILE_CHANGED" not in st.session_state:
|
|
|
|
|
|
st.session_state["UPLOADED_FILE_CHANGED"] = False
|
|
|
|
|
|
|
|
|
|
|
|
if "CHANGE_SIDEBAR_FLAG" not in st.session_state:
|
|
|
|
|
|
st.session_state["CHANGE_SIDEBAR_FLAG"] = False
|
|
|
|
|
|
|
2024-04-21 00:05:14 +08:00
|
|
|
|
if "INF_MODE" not in st.session_state:
|
2024-06-04 14:23:56 +00:00
|
|
|
|
st.session_state["INF_MODE"] = "Formula recognition"
|
2024-04-21 00:05:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-06-04 14:23:56 +00:00
|
|
|
|
############################## <sidebar> ##############################
|
2024-02-11 08:06:50 +00:00
|
|
|
|
|
2024-04-17 09:12:07 +00:00
|
|
|
|
with st.sidebar:
|
|
|
|
|
|
num_beams = 1
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown("# 🔨️ Config")
|
|
|
|
|
|
st.markdown("")
|
|
|
|
|
|
|
2024-04-21 00:05:14 +08:00
|
|
|
|
inf_mode = st.selectbox(
|
|
|
|
|
|
"Inference mode",
|
2024-06-04 14:23:56 +00:00
|
|
|
|
("Formula recognition", "Paragraph recognition"),
|
2024-04-17 09:12:07 +00:00
|
|
|
|
on_change=change_side_bar
|
|
|
|
|
|
)
|
2024-04-21 00:05:14 +08:00
|
|
|
|
|
|
|
|
|
|
num_beams = st.number_input(
|
|
|
|
|
|
'Number of beams',
|
|
|
|
|
|
min_value=1,
|
|
|
|
|
|
max_value=20,
|
|
|
|
|
|
step=1,
|
|
|
|
|
|
on_change=change_side_bar
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
accelerator = st.radio(
|
|
|
|
|
|
"Accelerator",
|
2024-04-17 09:12:07 +00:00
|
|
|
|
("cpu", "cuda", "mps"),
|
|
|
|
|
|
on_change=change_side_bar
|
|
|
|
|
|
)
|
2024-05-07 07:44:24 +00:00
|
|
|
|
|
2024-07-11 20:33:51 +08:00
|
|
|
|
st.markdown("## Seedup")
|
2024-06-22 21:51:51 +08:00
|
|
|
|
use_onnx = st.toggle("ONNX Runtime ")
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-07 07:44:24 +00:00
|
|
|
|
|
2024-06-04 14:23:56 +00:00
|
|
|
|
############################## </sidebar> ##############################
|
2024-04-17 09:12:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-06-04 14:23:56 +00:00
|
|
|
|
################################ <page> ################################
|
2024-03-18 15:48:04 +00:00
|
|
|
|
|
2024-06-22 21:51:51 +08:00
|
|
|
|
texteller = get_texteller(use_onnx, accelerator)
|
2024-04-21 00:05:14 +08:00
|
|
|
|
tokenizer = get_tokenizer()
|
|
|
|
|
|
latex_rec_models = [texteller, tokenizer]
|
|
|
|
|
|
|
2024-06-04 14:23:56 +00:00
|
|
|
|
if inf_mode == "Paragraph recognition":
|
2024-06-22 21:51:51 +08:00
|
|
|
|
infer_config, latex_det_model = get_det_models(accelerator)
|
2024-05-27 17:03:48 +00:00
|
|
|
|
lang_ocr_models = get_ocr_models(accelerator)
|
2024-04-21 00:05:14 +08:00
|
|
|
|
|
2024-02-11 08:06:50 +00:00
|
|
|
|
st.markdown(html_string, unsafe_allow_html=True)
|
|
|
|
|
|
|
2024-04-17 09:12:07 +00:00
|
|
|
|
uploaded_file = st.file_uploader(
|
|
|
|
|
|
" ",
|
|
|
|
|
|
type=['jpg', 'png'],
|
|
|
|
|
|
on_change=on_file_upload
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
paste_result = pbutton(
|
|
|
|
|
|
label="📋 Paste an image",
|
|
|
|
|
|
background_color="#5BBCFF",
|
|
|
|
|
|
hover_background_color="#3498db",
|
|
|
|
|
|
)
|
|
|
|
|
|
st.write("")
|
|
|
|
|
|
|
|
|
|
|
|
if st.session_state["CHANGE_SIDEBAR_FLAG"] == True:
|
|
|
|
|
|
st.session_state["CHANGE_SIDEBAR_FLAG"] = False
|
|
|
|
|
|
elif uploaded_file or paste_result.image_data is not None:
|
|
|
|
|
|
if st.session_state["UPLOADED_FILE_CHANGED"] == False and paste_result.image_data is not None:
|
|
|
|
|
|
uploaded_file = io.BytesIO()
|
|
|
|
|
|
paste_result.image_data.save(uploaded_file, format='PNG')
|
|
|
|
|
|
uploaded_file.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
if st.session_state["UPLOADED_FILE_CHANGED"] == True:
|
|
|
|
|
|
st.session_state["UPLOADED_FILE_CHANGED"] = False
|
2024-02-11 08:06:50 +00:00
|
|
|
|
|
|
|
|
|
|
img = Image.open(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
|
png_file_path = os.path.join(temp_dir, 'image.png')
|
|
|
|
|
|
img.save(png_file_path, 'PNG')
|
|
|
|
|
|
|
2024-04-17 09:12:07 +00:00
|
|
|
|
with st.container(height=300):
|
|
|
|
|
|
img_base64 = get_image_base64(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown(f"""
|
|
|
|
|
|
<style>
|
|
|
|
|
|
.centered-container {{
|
|
|
|
|
|
text-align: center;
|
|
|
|
|
|
}}
|
|
|
|
|
|
.centered-image {{
|
|
|
|
|
|
display: block;
|
|
|
|
|
|
margin-left: auto;
|
|
|
|
|
|
margin-right: auto;
|
|
|
|
|
|
max-height: 350px;
|
|
|
|
|
|
max-width: 100%;
|
|
|
|
|
|
}}
|
|
|
|
|
|
</style>
|
|
|
|
|
|
<div class="centered-container">
|
|
|
|
|
|
<img src="data:image/png;base64,{img_base64}" class="centered-image" alt="Input image">
|
|
|
|
|
|
</div>
|
|
|
|
|
|
""", unsafe_allow_html=True)
|
2024-02-11 08:06:50 +00:00
|
|
|
|
st.markdown(f"""
|
|
|
|
|
|
<style>
|
|
|
|
|
|
.centered-container {{
|
|
|
|
|
|
text-align: center;
|
|
|
|
|
|
}}
|
|
|
|
|
|
</style>
|
|
|
|
|
|
<div class="centered-container">
|
|
|
|
|
|
<p style="color:gray;">Input image ({img.height}✖️{img.width})</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
st.write("")
|
|
|
|
|
|
|
|
|
|
|
|
with st.spinner("Predicting..."):
|
2024-06-04 14:23:56 +00:00
|
|
|
|
if inf_mode == "Formula recognition":
|
2024-04-21 00:05:14 +08:00
|
|
|
|
TexTeller_result = latex_recognition(
|
|
|
|
|
|
texteller,
|
|
|
|
|
|
tokenizer,
|
|
|
|
|
|
[png_file_path],
|
|
|
|
|
|
accelerator=accelerator,
|
|
|
|
|
|
num_beams=num_beams
|
|
|
|
|
|
)[0]
|
|
|
|
|
|
katex_res = to_katex(TexTeller_result)
|
|
|
|
|
|
else:
|
2024-05-27 17:03:48 +00:00
|
|
|
|
katex_res = mix_inference(png_file_path, infer_config, latex_det_model, lang_ocr_models, latex_rec_models, accelerator, num_beams)
|
2024-04-21 00:05:14 +08:00
|
|
|
|
|
2024-04-06 07:27:27 +00:00
|
|
|
|
st.success('Completed!', icon="✅")
|
|
|
|
|
|
st.markdown(suc_gif_html, unsafe_allow_html=True)
|
2024-04-17 09:12:07 +00:00
|
|
|
|
st.text_area(":blue[*** 𝑃r𝑒d𝑖c𝑡e𝑑 𝑓o𝑟m𝑢l𝑎 ***]", katex_res, height=150)
|
2024-04-21 00:05:14 +08:00
|
|
|
|
|
2024-06-04 14:23:56 +00:00
|
|
|
|
if inf_mode == "Formula recognition":
|
2024-04-21 00:05:14 +08:00
|
|
|
|
st.latex(katex_res)
|
2024-06-04 14:23:56 +00:00
|
|
|
|
elif inf_mode == "Paragraph recognition":
|
|
|
|
|
|
mixed_res = re.split(r'(\$\$.*?\$\$)', katex_res)
|
2024-05-27 17:03:48 +00:00
|
|
|
|
for text in mixed_res:
|
2024-06-04 14:23:56 +00:00
|
|
|
|
if text.startswith('$$') and text.endswith('$$'):
|
|
|
|
|
|
st.latex(text[2:-2])
|
2024-05-27 17:03:48 +00:00
|
|
|
|
else:
|
|
|
|
|
|
st.markdown(text)
|
2024-03-18 15:48:04 +00:00
|
|
|
|
|
2024-04-17 09:12:07 +00:00
|
|
|
|
st.write("")
|
|
|
|
|
|
st.write("")
|
|
|
|
|
|
|
|
|
|
|
|
with st.expander(":star2: :gray[Tips for better results]"):
|
|
|
|
|
|
st.markdown('''
|
|
|
|
|
|
* :mag_right: Use a clear and high-resolution image.
|
|
|
|
|
|
* :scissors: Crop images as accurately as possible.
|
|
|
|
|
|
* :jigsaw: Split large multi line formulas into smaller ones.
|
|
|
|
|
|
* :page_facing_up: Use images with **white background and black text** as much as possible.
|
|
|
|
|
|
* :book: Use a font with good readability.
|
|
|
|
|
|
''')
|
2024-03-18 15:48:04 +00:00
|
|
|
|
shutil.rmtree(temp_dir)
|
2024-02-11 08:06:50 +00:00
|
|
|
|
|
2024-04-17 09:12:07 +00:00
|
|
|
|
paste_result.image_data = None
|
|
|
|
|
|
|
2024-06-04 14:23:56 +00:00
|
|
|
|
################################ </page> ################################
|