Skip to content

Commit 45862a6

Browse files
committed
Add flask
1 parent 51b6897 commit 45862a6

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

app.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
from flask import Flask
3+
from flask import request
4+
5+
6+
from simplelatexocr.models import Latex_OCR
7+
REQUEST_ID_HEADER = 'x-fc-request-id'
8+
9+
detect_path="models/best.onnx"
10+
encoder_path = "models/encoder.onnx"
11+
decoder_path = "models/decoder.onnx"
12+
tokenizer_json = "models/tokenizer.json"
13+
14+
model = Latex_OCR(
15+
detect_path=detect_path,
16+
encoder_path=encoder_path,
17+
decoder_path=decoder_path,
18+
tokenizer_json=tokenizer_json,
19+
)
20+
21+
app = Flask(__name__)
22+
23+
@app.route('/', methods=['GET', 'POST'])
24+
def upload_image():
25+
if request.method == 'POST' and 'image' in request.files:
26+
image = request.files['image']
27+
res = model.predict(image)
28+
print("formula: ", res["formula"])
29+
print("confidence: ", res["confidence"])
30+
print("elapse: ", res["elapse"])
31+
print(res)
32+
return json.dumps(res)
33+
else:
34+
return json.dumps({"error": "info erro"})
35+
36+
if __name__ == '__main__':
37+
38+
app.run(host='0.0.0.0',port=9000,debug=False)
39+

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ numpy
44
opencv-python
55
Pillow>=9.2.0
66
PyYAML
7-
transformers
7+
transformers
8+
flask

test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import json
2+
import requests
3+
4+
5+
image_path = "tests/test_files/5.png"
6+
url = "http://127.0.0.1:9000/"
7+
files = {'image': open(image_path, 'rb')}
8+
response = requests.post(url, files=files)
9+
10+
# print result
11+
print(response.text)
12+
data=json.loads(response.text)
13+
print("formula:\n{}".format(data['formula']))
14+
print('confidence:\n{}'.format(data['confidence']))
15+
print('elapse:\n{}'.format(data['elapse']))

0 commit comments

Comments
 (0)