Spaces:
Running
Running
Upload 5 files
Browse files- .gitattributes +0 -1
- .gitignore +158 -0
- app.py +115 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -25,7 +25,6 @@
|
|
| 25 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
*.wasm filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 25 |
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 28 |
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
*.wasm filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Created by .ignore support plugin (hsz.mobi)
|
| 2 |
+
### Python template
|
| 3 |
+
# Byte-compiled / optimized / DLL files
|
| 4 |
+
__pycache__/
|
| 5 |
+
*.py[cod]
|
| 6 |
+
*$py.class
|
| 7 |
+
.pytest_cache
|
| 8 |
+
|
| 9 |
+
# C extensions
|
| 10 |
+
*.so
|
| 11 |
+
|
| 12 |
+
# Distribution / packaging
|
| 13 |
+
.Python
|
| 14 |
+
build/
|
| 15 |
+
develop-eggs/
|
| 16 |
+
dist/
|
| 17 |
+
downloads/
|
| 18 |
+
eggs/
|
| 19 |
+
.eggs/
|
| 20 |
+
lib/
|
| 21 |
+
lib64/
|
| 22 |
+
parts/
|
| 23 |
+
sdist/
|
| 24 |
+
var/
|
| 25 |
+
wheels/
|
| 26 |
+
pip-wheel-metadata/
|
| 27 |
+
share/python-wheels/
|
| 28 |
+
*.egg-info/
|
| 29 |
+
.installed.cfg
|
| 30 |
+
*.egg
|
| 31 |
+
MANIFEST
|
| 32 |
+
|
| 33 |
+
# PyInstaller
|
| 34 |
+
# Usually these files are written by a python script from a template
|
| 35 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 36 |
+
# *.manifest
|
| 37 |
+
# *.spec
|
| 38 |
+
*.res
|
| 39 |
+
|
| 40 |
+
# Installer logs
|
| 41 |
+
pip-log.txt
|
| 42 |
+
pip-delete-this-directory.txt
|
| 43 |
+
|
| 44 |
+
# Unit test / coverage reports
|
| 45 |
+
htmlcov/
|
| 46 |
+
.tox/
|
| 47 |
+
.nox/
|
| 48 |
+
.coverage
|
| 49 |
+
.coverage.*
|
| 50 |
+
.cache
|
| 51 |
+
nosetests.xml
|
| 52 |
+
coverage.xml
|
| 53 |
+
*.cover
|
| 54 |
+
*.py,cover
|
| 55 |
+
.hypothesis/
|
| 56 |
+
.pytest_cache/
|
| 57 |
+
|
| 58 |
+
# Translations
|
| 59 |
+
*.mo
|
| 60 |
+
*.pot
|
| 61 |
+
|
| 62 |
+
# Django stuff:
|
| 63 |
+
*.log
|
| 64 |
+
local_settings.py
|
| 65 |
+
db.sqlite3
|
| 66 |
+
db.sqlite3-journal
|
| 67 |
+
|
| 68 |
+
# Flask stuff:
|
| 69 |
+
instance/
|
| 70 |
+
.webassets-cache
|
| 71 |
+
|
| 72 |
+
# Scrapy stuff:
|
| 73 |
+
.scrapy
|
| 74 |
+
|
| 75 |
+
# Sphinx documentation
|
| 76 |
+
docs/_build/
|
| 77 |
+
|
| 78 |
+
# PyBuilder
|
| 79 |
+
target/
|
| 80 |
+
|
| 81 |
+
# Jupyter Notebook
|
| 82 |
+
.ipynb_checkpoints
|
| 83 |
+
|
| 84 |
+
# IPython
|
| 85 |
+
profile_default/
|
| 86 |
+
ipython_config.py
|
| 87 |
+
|
| 88 |
+
# pyenv
|
| 89 |
+
.python-version
|
| 90 |
+
|
| 91 |
+
# pipenv
|
| 92 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 93 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 94 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 95 |
+
# install all needed dependencies.
|
| 96 |
+
#Pipfile.lock
|
| 97 |
+
|
| 98 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
| 99 |
+
__pypackages__/
|
| 100 |
+
|
| 101 |
+
# Celery stuff
|
| 102 |
+
celerybeat-schedule
|
| 103 |
+
celerybeat.pid
|
| 104 |
+
|
| 105 |
+
# SageMath parsed files
|
| 106 |
+
*.sage.py
|
| 107 |
+
|
| 108 |
+
# Environments
|
| 109 |
+
.env
|
| 110 |
+
.venv
|
| 111 |
+
env/
|
| 112 |
+
venv/
|
| 113 |
+
ENV/
|
| 114 |
+
env.bak/
|
| 115 |
+
venv.bak/
|
| 116 |
+
|
| 117 |
+
# Spyder project settings
|
| 118 |
+
.spyderproject
|
| 119 |
+
.spyproject
|
| 120 |
+
|
| 121 |
+
# Rope project settings
|
| 122 |
+
.ropeproject
|
| 123 |
+
|
| 124 |
+
# mkdocs documentation
|
| 125 |
+
/site
|
| 126 |
+
|
| 127 |
+
# mypy
|
| 128 |
+
.mypy_cache/
|
| 129 |
+
.dmypy.json
|
| 130 |
+
dmypy.json
|
| 131 |
+
|
| 132 |
+
# Pyre type checker
|
| 133 |
+
.pyre/
|
| 134 |
+
|
| 135 |
+
#idea
|
| 136 |
+
.vs
|
| 137 |
+
.vscode
|
| 138 |
+
.idea
|
| 139 |
+
/images
|
| 140 |
+
/models
|
| 141 |
+
|
| 142 |
+
#models
|
| 143 |
+
*.onnx
|
| 144 |
+
|
| 145 |
+
*.ttf
|
| 146 |
+
*.ttc
|
| 147 |
+
|
| 148 |
+
long1.jpg
|
| 149 |
+
|
| 150 |
+
*.bin
|
| 151 |
+
*.mapping
|
| 152 |
+
*.xml
|
| 153 |
+
|
| 154 |
+
*.pdiparams
|
| 155 |
+
*.pdiparams.info
|
| 156 |
+
*.pdmodel
|
| 157 |
+
|
| 158 |
+
.DS_Store
|
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- encoding: utf-8 -*-
|
| 2 |
+
# @Author: SWHL
|
| 3 |
+
# @Contact: [email protected]
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from rapid_latex_ocr import LatexOCR
|
| 10 |
+
from streamlit_cropper import st_cropper
|
| 11 |
+
from streamlit_image_select import image_select
|
| 12 |
+
|
| 13 |
+
st.set_option("deprecation.showfileUploaderEncoding", False)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class RecEquation:
|
| 17 |
+
def __init__(self, model_dir: str):
|
| 18 |
+
model_dir = Path(model_dir)
|
| 19 |
+
|
| 20 |
+
image_resizer_path = model_dir / "image_resizer.onnx"
|
| 21 |
+
encoder_path = model_dir / "encoder.onnx"
|
| 22 |
+
decoder_path = model_dir / "decoder.onnx"
|
| 23 |
+
tokenizer_json = model_dir / "tokenizer.json"
|
| 24 |
+
self.model = LatexOCR(
|
| 25 |
+
image_resizer_path=str(image_resizer_path),
|
| 26 |
+
encoder_path=str(encoder_path),
|
| 27 |
+
decoder_path=str(decoder_path),
|
| 28 |
+
tokenizer_json=str(tokenizer_json),
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def __call__(self, img: np.ndarray):
|
| 32 |
+
result, elapse = self.model(img)
|
| 33 |
+
return result, elapse
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
st.markdown(
|
| 38 |
+
"<h1 style='text-align: center;'><a href='https://github.com/RapidAI/RapidLatexOCR' style='text-decoration: none'>Rapid Latex OCR</a></h1>",
|
| 39 |
+
unsafe_allow_html=True,
|
| 40 |
+
)
|
| 41 |
+
st.markdown(
|
| 42 |
+
"""
|
| 43 |
+
<p align="left">
|
| 44 |
+
<a href=""><img src="https://img.shields.io/badge/Python->=3.6,<3.12-aff.svg"></a>
|
| 45 |
+
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
|
| 46 |
+
<a href="https://pepy.tech/project/rapid_latex_ocr"><img src="https://static.pepy.tech/personalized-badge/rapid_latex_ocr?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads"></a>
|
| 47 |
+
<a href="https://pypi.org/project/rapid_latex_ocr/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid_latex_ocr"></a>
|
| 48 |
+
<a href="https://semver.org/"><img alt="SemVer2.0" src="https://img.shields.io/badge/SemVer-2.0-brightgreen"></a>
|
| 49 |
+
<a href="https://github.com/psf/black"><img src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
|
| 50 |
+
</p>
|
| 51 |
+
""",
|
| 52 |
+
unsafe_allow_html=True,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Upload an image and set some options for demo purposes
|
| 56 |
+
img_file = st.sidebar.file_uploader(label="Upload a file", type=["png", "jpg"])
|
| 57 |
+
realtime_update = st.sidebar.checkbox(label="Update in Real Time", value=False)
|
| 58 |
+
box_color = st.sidebar.color_picker(label="Box Color", value="#0000FF")
|
| 59 |
+
aspect_choice = st.sidebar.radio(
|
| 60 |
+
label="Aspect Ratio", options=["Free", "1:1", "16:9", "4:3", "2:3"]
|
| 61 |
+
)
|
| 62 |
+
aspect_dict = {
|
| 63 |
+
"Free": None,
|
| 64 |
+
"1:1": (1, 1),
|
| 65 |
+
"16:9": (16, 9),
|
| 66 |
+
"4:3": (4, 3),
|
| 67 |
+
"2:3": (2, 3),
|
| 68 |
+
}
|
| 69 |
+
aspect_ratio = aspect_dict[aspect_choice]
|
| 70 |
+
with st.sidebar.container():
|
| 71 |
+
img = image_select(
|
| 72 |
+
label="Examples(click to select):",
|
| 73 |
+
images=[
|
| 74 |
+
"images/equation.png",
|
| 75 |
+
"images/eq_2.png",
|
| 76 |
+
"images/eq_3.png",
|
| 77 |
+
"images/eq_4.png",
|
| 78 |
+
],
|
| 79 |
+
key="equation_default",
|
| 80 |
+
use_container_width=False,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
rec_eq_sys = RecEquation(model_dir="models")
|
| 84 |
+
|
| 85 |
+
select_img_container = st.container()
|
| 86 |
+
|
| 87 |
+
st.markdown("#### Select image:")
|
| 88 |
+
img_empty = st.empty()
|
| 89 |
+
|
| 90 |
+
img_empty.image(img, use_column_width=False)
|
| 91 |
+
rec_res, elapse = rec_eq_sys(img)
|
| 92 |
+
|
| 93 |
+
if img_file:
|
| 94 |
+
img = Image.open(img_file)
|
| 95 |
+
|
| 96 |
+
# Get a cropped image from the frontend
|
| 97 |
+
with select_img_container:
|
| 98 |
+
if not realtime_update:
|
| 99 |
+
select_img_container.markdown("#### Double click to save crop")
|
| 100 |
+
|
| 101 |
+
img = st_cropper(
|
| 102 |
+
img,
|
| 103 |
+
realtime_update=realtime_update,
|
| 104 |
+
box_color=box_color,
|
| 105 |
+
aspect_ratio=aspect_ratio,
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
img_empty.image(img, use_column_width=False)
|
| 109 |
+
rec_res, elapse = rec_eq_sys(np.array(img))
|
| 110 |
+
|
| 111 |
+
st.markdown(f"#### Rec result (cost: {elapse:.4f}s):")
|
| 112 |
+
st.latex(rec_res)
|
| 113 |
+
|
| 114 |
+
st.markdown("#### Latex source code:")
|
| 115 |
+
st.code(rec_res)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair<5
|
| 2 |
+
Pillow
|
| 3 |
+
numpy
|
| 4 |
+
rapid_latex_ocr
|
| 5 |
+
streamlit_image_select
|
| 6 |
+
streamlit_cropper
|