It works for some requests so i know the code works, but when it is a longer scanned pdf for example, it times out.
from flask import Flask, request, jsonify
import os
import requests
app = Flask(__name__)
*Auth Token Removed*
@app.route('/')
def index():
return 'Hello World!'
@app.route('/process_ocr', methods=['POST'])
def process_ocr():
access_token = get_oauth_token()
if not access_token:
return jsonify({'message': 'Failed to authenticate with Zoho OAuth'}), 500
# Hardcoded image link for testing
image_link = "*Zoho Workdrive Link removed*?directDownload=true"
# Download the image from the given link
image_response = requests.get(image_link)
if image_response.status_code != 200:
return jsonify({'message': 'Failed to download image', 'status': image_response.status_code}), 500
# Prepare the OCR API call
ocr_headers = {
'Authorization': f'Zoho-oauthtoken {access_token}'
}
ocr_files = {
'image': ('image.pdf', image_response.content, 'application/pdf'),
'language': (None, 'eng,spa')
}
try:
ocr_response = requests.post(
headers=ocr_headers,
files=ocr_files,
timeout=200 # Set timeout to 60 seconds
)
except requests.exceptions.Timeout:
return jsonify({'message': 'OCR request timed out, please try again later'}), 504
if ocr_response.status_code != 200:
print("Failed OCR request:", ocr_response.text)
return jsonify({'message': 'OCR request failed', 'status': ocr_response.status_code}), 500
ocr_data = ocr_response.json()
print("OCR Response:", ocr_data)
ocr_text = ocr_data.get('data', {}).get('text', '')
# Post the OCR text to the webhook
webhook_url = '*Webhook URL Removed*'
webhook_response = requests.post(webhook_url, json={'text': ocr_text})
return jsonify({
'message': 'OCR processing completed',
'status': webhook_response.status_code,
'OCR Text': ocr_text,
'OCR Data': ocr_data # Optionally include full OCR response for debugging
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.getenv('X_ZOHO_CATALYST_LISTEN_PORT', 9000)), debug=True)