Created
December 15, 2020 05:41
-
-
Save abhamra/ebb453e660ba635583c58d183d4c1362 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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=trial APP_KEY=34f1a4cea0eaca8540c95908b4dc84ab python3 simple.py | |
# | |
env = os.environ | |
default_headers = { | |
'app_id': env.get('APP_ID', 'mmemmo_episcopalacademy_org_907c7f_36cc62'), | |
'app_key': env.get('APP_KEY', '47ad618ad54e2ef9a15e'), | |
'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=30): | |
r = requests.post(service, | |
data=json.dumps(args), headers=headers, timeout=timeout | |
) | |
return json.loads(r.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment