Spaces:
Runtime error
Runtime error
Commit
•
5dee6f4
1
Parent(s):
d8e0bc8
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,23 @@
|
|
1 |
-
import
|
|
|
2 |
from PIL import Image
|
3 |
import streamlit
|
4 |
from simple_latex_ocr.models import Latex_OCR
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
if __name__ == '__main__':
|
7 |
streamlit.set_page_config(page_title='Simple-LaTeX-OCR')
|
8 |
streamlit.title('Simple-LaTeX-OCR')
|
9 |
-
streamlit.markdown(
|
|
|
10 |
|
11 |
uploaded_file = streamlit.file_uploader(
|
12 |
'Upload an image an equation',
|
@@ -22,17 +33,15 @@ if __name__ == '__main__':
|
|
22 |
if streamlit.button('Convert'):
|
23 |
if uploaded_file is not None and image is not None:
|
24 |
with streamlit.spinner('Computing'):
|
25 |
-
|
26 |
-
response =model.predict(uploaded_file.getvalue())
|
27 |
if "formula" in response:
|
28 |
latex_code = response["formula"]
|
|
|
29 |
streamlit.code(latex_code, language='latex')
|
30 |
streamlit.markdown(f'$\\displaystyle {latex_code}$')
|
31 |
-
|
32 |
-
streamlit.markdown(
|
33 |
-
'<a href="https://www.latexlive.com/" target="_blank"><button style="color: black;">Online Editing</button></a>',
|
34 |
-
unsafe_allow_html=True)
|
35 |
else:
|
36 |
streamlit.error(response)
|
37 |
else:
|
38 |
-
streamlit.error('Please upload an image.')
|
|
|
1 |
+
import execjs
|
2 |
+
|
3 |
from PIL import Image
|
4 |
import streamlit
|
5 |
from simple_latex_ocr.models import Latex_OCR
|
6 |
+
|
7 |
+
js_code = """
|
8 |
+
function encodeToBase64(latex) {
|
9 |
+
var str = encodeURI(latex); // 进行URL编码
|
10 |
+
str = btoa(str); // 进行Base64编码
|
11 |
+
return str;
|
12 |
+
}
|
13 |
+
"""
|
14 |
+
ctx = execjs.compile(js_code)
|
15 |
+
model = Latex_OCR()
|
16 |
if __name__ == '__main__':
|
17 |
streamlit.set_page_config(page_title='Simple-LaTeX-OCR')
|
18 |
streamlit.title('Simple-LaTeX-OCR')
|
19 |
+
streamlit.markdown(
|
20 |
+
'Convert images of equations to corresponding LaTeX code.\n\nThis is based on the `Simple-LaTeX-OCR` module. Check it out (https://github.com/chaodreaming/Simple-LaTeX-OCR)')
|
21 |
|
22 |
uploaded_file = streamlit.file_uploader(
|
23 |
'Upload an image an equation',
|
|
|
33 |
if streamlit.button('Convert'):
|
34 |
if uploaded_file is not None and image is not None:
|
35 |
with streamlit.spinner('Computing'):
|
36 |
+
response = model.predict(uploaded_file.getvalue())
|
|
|
37 |
if "formula" in response:
|
38 |
latex_code = response["formula"]
|
39 |
+
latex_encode = ctx.call("encodeToBase64", latex_code)
|
40 |
streamlit.code(latex_code, language='latex')
|
41 |
streamlit.markdown(f'$\\displaystyle {latex_code}$')
|
42 |
+
link_html = f'<a href="https://www.latexlive.com/#{latex_encode}" target="_blank"><button style="color: black;">Online Editing</button></a>'
|
43 |
+
streamlit.markdown(link_html, unsafe_allow_html=True)
|
|
|
|
|
44 |
else:
|
45 |
streamlit.error(response)
|
46 |
else:
|
47 |
+
streamlit.error('Please upload an image.')
|