-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from lukas-blecher/api
Add API
- Loading branch information
Showing
7 changed files
with
213 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Adapted from https://github.com/kingyiusuen/image-to-latex/blob/main/api/app.py | ||
|
||
from ctypes import resize | ||
from http import HTTPStatus | ||
from fastapi import FastAPI, File, UploadFile, Form | ||
from PIL import Image | ||
from io import BytesIO | ||
from pix2tex.cli import LatexOCR | ||
|
||
model = None | ||
app = FastAPI(title='pix2tex API') | ||
|
||
|
||
def read_imagefile(file) -> Image.Image: | ||
image = Image.open(BytesIO(file)) | ||
return image | ||
|
||
|
||
@app.on_event('startup') | ||
async def load_model(): | ||
global model | ||
if model is None: | ||
model = LatexOCR() | ||
|
||
|
||
@app.get('/') | ||
def root(): | ||
'''Health check.''' | ||
response = { | ||
'message': HTTPStatus.OK.phrase, | ||
'status-code': HTTPStatus.OK, | ||
'data': {}, | ||
} | ||
return response | ||
|
||
|
||
@app.post('/predict/') | ||
async def predict(file: UploadFile = File(...)): | ||
global model | ||
image = Image.open(file.file) | ||
return model(image) | ||
|
||
|
||
@app.post('/bytes/') | ||
async def predict_from_bytes(file: bytes = File(...)): #, size: str = Form(...) | ||
global model | ||
#size = tuple(int(a) for a in size.split(',')) | ||
image = Image.open(BytesIO(file)) | ||
return model(image, resize=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from multiprocessing import Process | ||
import subprocess | ||
import os | ||
|
||
|
||
def start_api(path='.'): | ||
subprocess.call(['uvicorn', 'app:app'], cwd=path) | ||
|
||
|
||
def start_frontend(path='.'): | ||
subprocess.call(['streamlit', 'run', 'streamlit.py'], cwd=path) | ||
|
||
|
||
if __name__ == '__main__': | ||
path = os.path.realpath(os.path.dirname(__file__)) | ||
api = Process(target=start_api, kwargs={'path': path}) | ||
api.start() | ||
frontend = Process(target=start_frontend, kwargs={'path': path}) | ||
frontend.start() | ||
api.join() | ||
frontend.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from msilib.schema import Icon | ||
import requests | ||
from PIL import Image | ||
import streamlit | ||
|
||
if __name__ == '__main__': | ||
streamlit.set_page_config(page_title='LaTeX-OCR') | ||
streamlit.title('LaTeX OCR') | ||
streamlit.markdown('Convert images of equations to corresponding LaTeX code.\n\nThis is based on the `pix2tex` module. Check it out [![github](https://img.shields.io/badge/LaTeX--OCR-visit-a?style=social&logo=github)](https://github.com/lukas-blecher/LaTeX-OCR)') | ||
|
||
uploaded_file = streamlit.file_uploader( | ||
'Upload an image an equation', | ||
type=['png', 'jpg'], | ||
) | ||
|
||
if uploaded_file is not None: | ||
image = Image.open(uploaded_file) | ||
streamlit.image(image) | ||
else: | ||
streamlit.text('\n') | ||
|
||
if streamlit.button('Convert'): | ||
if uploaded_file is not None and image is not None: | ||
with streamlit.spinner('Computing'): | ||
response = requests.post('http://127.0.0.1:8000/predict/', files={'file': uploaded_file.getvalue()}) | ||
if response.ok: | ||
latex_code = response.json() | ||
streamlit.code(latex_code, language='latex') | ||
streamlit.markdown(f'$\\displaystyle {latex_code}$') | ||
else: | ||
streamlit.error(response.text) | ||
else: | ||
streamlit.error('Please upload an image.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.