Skip to content

Commit

Permalink
new machine learning api working
Browse files Browse the repository at this point in the history
  • Loading branch information
ebc5802 committed May 1, 2024
1 parent 339bb2a commit eb03cf9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 32 deletions.
Binary file added machine-learning-client/RestaurantReceipt2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
117 changes: 86 additions & 31 deletions machine-learning-client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
from dotenv import load_dotenv
from bson import ObjectId
from mindee import Client, PredictResponse, product # To access mindee OCR API

import logging

Expand Down Expand Up @@ -58,31 +59,37 @@ def pretdict_endpoint():
return jsonify({'error': 'Object_ID not found in request data'}), 400

Object_ID = ObjectId(request_data['Object_ID'])
logger.debug('OBJECT_ID MESSAGE:', Object_ID)
logging.debug('OBJECT_ID MESSAGE:', Object_ID)
#image = db.receipts.find_one({"_id": Object_ID})['image']

# Here, you would add the code to perform OCR on the image
# For now, let's assume you have a function called perform_ocr that does this

# Uncomment next line to perform OCR
data = perform_ocr(Object_ID)
logging.debug("data after ocr: %s", data) # debug
print("data after ocr: %s", data)
#data = json.load(open("response1.json", "r"))

# Connect to your collection (replace 'mycollection' with your collection name)
collection = db['receipts']

# Prepare the data to be inserted into the database
receipt = data['receipts'][0]
# line_items = data['document']['inference']['pages'][0]['prediction']['line_items']
receipt = data['document']['inference']['pages'][0]['prediction']
logging.debug('OCR Json Keys:', data['document']['inference']['pages'][0]['prediction'].keys()) # debug
print('OCR Json Keys:', data['document']['inference']['pages'][0]['prediction'].keys())
receipt_data = {
'receipt_name': receipt['merchant_name'],
'currency': receipt['currency'],
'items': [{'description': item['description'], 'amount': item['amount']} for item in receipt['items']],
'total': receipt['total'],
'tax': receipt['tax'],
'tip': receipt['tip'],
'subtotal': receipt['subtotal'],
'receipt_name': receipt['supplier_name']['raw_value'],
'currency': receipt['locale']['currency'],
'items': [{'description': item['description'], 'amount': item['total_amount'], 'quantity': item['quantity']} for item in receipt['line_items']],
'total': receipt['total_amount']['value'],
'tax': receipt['total_tax']['value'],
'tip': receipt['tip']['value'],
'subtotal': receipt['total_net']['value'],
}
logger.debug(receipt_data)
logging.debug(receipt_data)
print("receipt_data: %s", receipt_data)

# Update the document with given ObjectId
collection.update_one({'_id': Object_ID}, {'$set': receipt_data})
Expand All @@ -92,32 +99,80 @@ def pretdict_endpoint():
return jsonify({'_id': str(inserted_id)})

def perform_ocr(Object_ID):
logger.debug("starting perform_ocr function...") # debug
url = "https://ocr.asprise.com/api/v1/receipt"
logging.debug("starting perform_ocr function with mindee api...") # debug
url = "https://api.mindee.net/v1/products/mindee/expense_receipts/v5/predict"
api_key = os.getenv("OCR_API_KEY") # Get the API key from environment variable

# Fetch the image data from the database
image_data = db.receipts.find_one({"_id": Object_ID})['image']
file_path = f"receipt_{Object_ID}.jpg" # Set the file path to save the image
logger.debug("file path: %s", file_path) # debug
logging.debug("file path: %s", file_path) # debug

# Save the image data to a file
with open(file_path, "wb") as f:
f.write(image_data)
file_path = file_path.replace('\x00', '') # Remove any null bytes from the file path


# Get response (can only do this a couple times with the test API key)
res = requests.post(url,
data = {
'api_key': 'TEST',
'recognizer': 'auto',
'ref_no': 'ocr_python_123'
},

files = {
'file': open(file_path, "rb")
})

with open("response.json", "w") as f:
f.write(res.text)

return res.json()
# Parse the file with the Mindee API
with open(file_path, "rb") as myfile:
files = {"document": myfile}
headers = {"Authorization": "Token " + api_key}
response = requests.post(url, files=files, headers=headers)
print("Type of response: ", type(response))
print("response.text: %s", response.text)
return response.json()

# def perform_ocr(Object_ID):
# logging.debug("starting perform_ocr function with mindee api...") # debug
# # Init a new client
# api_key = os.getenv("OCR_API_KEY") # Get the API key from environment variable
# mindee_client = Client(api_key)

# # Fetch the image data from the database
# image_data = db.receipts.find_one({"_id": Object_ID})['image']
# file_path = f"receipt_{Object_ID}.jpg" # Set the file path to save the image
# logging.debug("file path: %s", file_path) # debug

# # Save the image data to a file
# with open(file_path, "wb") as f:
# f.write(image_data)

# # Load the file
# input_doc = mindee_client.source_from_path(file_path)

# # Parse the file with the Mindee API
# result: PredictResponse = mindee_client.parse(product.ReceiptV5, input_doc)
# logging.debug("result: %s", result.document) # debug

# # Return the result as a JSON response
# return jsonify(result.document)

# def perform_ocr_old_version(Object_ID):
# logging.debug("starting perform_ocr function...") # debug
# url = "https://ocr.asprise.com/api/v1/receipt"
# image_data = db.receipts.find_one({"_id": Object_ID})['image']
# file_path = f"receipt_{Object_ID}.jpg" # Set the file path to save the image
# logging.debug("file path: %s", file_path) # debug
# with open(file_path, "wb") as f:
# f.write(image_data)
# file_path = file_path.replace('\x00', '') # Remove any null bytes from the file path


# # Get response (can only do this a couple times with the test API key)
# res = requests.post(url,
# data = {
# 'api_key': 'TEST',
# 'recognizer': 'auto',
# 'ref_no': 'ocr_python_123'
# },

# files = {
# 'file': open(file_path, "rb")
# })

# with open("response.json", "w") as f:
# f.write(res.text)

# return res.json()

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5002) # Run the app
3 changes: 2 additions & 1 deletion machine-learning-client/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pylint
black
requests
Flask
python-dotenv
python-dotenv
mindee
Empty file.

0 comments on commit eb03cf9

Please sign in to comment.