-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (52 loc) · 2.19 KB
/
app.py
File metadata and controls
65 lines (52 loc) · 2.19 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, request, jsonify
from ocr.ocr_model import *
from ocr.google_drive import *
from PIL import Image
from ocr.prerocess import *
from ocr.agent import *
app = Flask(__name__)
@app.route('/upload', methods=['POST'])
def upload_invoice():
if 'file' not in request.files:
return jsonify({"error": "No file provided"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No file selected"}), 400
print(f"File received: {file.filename}")
file.save("uploaded_invoice.png")
print("File saved as 'uploaded_invoice.png'")
try:
from PIL import Image
img = Image.open("uploaded_invoice.png")
print("Image opened successfully")
j=extract_invoice_details(img)
jsonify({"message": "File uploaded successfully"})
agent = InvoiceAgent()
validated_details = agent.validate_invoice_details(j)
except Exception as e:
print(f"Error opening image: {e}")
return jsonify({"error": "Failed to process image"}), 500
return jsonify(j,"validated_details",validated_details), 200
@app.route('/monitor-folder', methods=['GET'])
def monitor_folder():
service = authenticate_drive()
folder_id = '1sHTeNQX_QTP-EooQkXiVV2VtZfttoX8f'
downloaded_files = load_downloaded_files()
jsonify({"message": "Monitoring folder triggered."}),200
try:
while True:
jj= monitor(service, folder_id, downloaded_files)
print(jj)
save_downloaded_files(downloaded_files)
time.sleep(60) # Poll every minute
jj= monitor(service, folder_id, downloaded_files)
agent = InvoiceAgent()
validated_details = agent.validate_invoice_details(jj)
save_downloaded_files(downloaded_files)
return jsonify(jj,"validated_details",validated_details),200
except KeyboardInterrupt:
print("Stopping monitoring.")
save_downloaded_files(downloaded_files)
return jsonify(jj,"validated_details",validated_details),200
if __name__ == "__main__":
app.run(debug=True)