-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathpix.py
39 lines (31 loc) · 981 Bytes
/
mathpix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
import os
import base64
import requests
import json
#
# Common module for calling Mathpix OCR service from Python.
#
# N.B.: Set your credentials in environment variables APP_ID and APP_KEY,
# either once via setenv or on the command line as in
# APP_ID=my-id APP_KEY=my-key python3 simple.py
#
default_headers = {
'app_id': os.getenv('APP_ID'),
'app_key': os.getenv('APP_KEY'),
'Content-type': 'application/json'
}
service = 'https://api.mathpix.com/v3/latex'
#
# Return the base64 encoding of an image with the given filename.
#
def image_uri(filename):
image_data = open(filename, "rb").read()
return "data:image/jpg;base64," + base64.b64encode(image_data).decode()
#
# Call the Mathpix service with the given arguments, headers, and timeout.
#
def latex(args, headers=default_headers, timeout=100):
r = requests.post(service,
data=json.dumps(args), headers=headers, timeout=timeout)
return json.loads(r.text)