diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..3f63eb9 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "ms-azuretools.vscode-azurefunctions", + "ms-python.python" + ] +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..9a24428 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Attach to Python Functions", + "type": "debugpy", + "request": "attach", + "connect": { + "host": "localhost", + "port": 9091 + }, + "preLaunchTask": "func: host start" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6a8c441 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "azureFunctions.deploySubpath": "Deployment\\Code", + "azureFunctions.scmDoBuildDuringDeployment": true, + "azureFunctions.pythonVenv": ".venv", + "azureFunctions.projectLanguage": "Python", + "azureFunctions.projectRuntime": "~4", + "debug.internalConsoleOptions": "neverOpen" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..ffd8940 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,33 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "func", + "label": "func: host start", + "command": "host start", + "problemMatcher": "$func-python-watch", + "isBackground": true, + "dependsOn": "pip install (functions)", + "options": { + "cwd": "${workspaceFolder}/Deployment\\Code" + } + }, + { + "label": "pip install (functions)", + "type": "shell", + "osx": { + "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt" + }, + "windows": { + "command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt" + }, + "linux": { + "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt" + }, + "problemMatcher": [], + "options": { + "cwd": "${workspaceFolder}/Deployment\\Code" + } + } + ] +} \ No newline at end of file diff --git a/Deployment/Code/.funcignore b/Deployment/Code/.funcignore new file mode 100644 index 0000000..b694934 --- /dev/null +++ b/Deployment/Code/.funcignore @@ -0,0 +1 @@ +.venv \ No newline at end of file diff --git a/Deployment/Code/RecognizeFile/__init__.py b/Deployment/Code/RecognizeFile/__init__.py index 6443da5..152d868 100644 --- a/Deployment/Code/RecognizeFile/__init__.py +++ b/Deployment/Code/RecognizeFile/__init__.py @@ -52,16 +52,16 @@ def main(req: func.HttpRequest) -> func.HttpResponse: and date_time and year_number and month_number and day_number: logging.info(f"All required fields are received: {msg}") else: - http_body_msg = f"Not all required fields are present in requet: {msg} " - return func.HttpResponse(body=http_body_msg, status_code=100) + http_body_msg = f"Not all required fields are present in request: {msg} " + return func.HttpResponse(body=http_body_msg, status_code=400) my_account_url = f"https://{storage_account}.blob.core.windows.net" - input_blob_url = f"{my_account_url}" + f"{file_path}" # preferred method + input_blob_url = f"{my_account_url}{file_path}" # preferred method ################################################################## # Establish Security Model # Use Managed Identity - # RG_MID_CLIENT_ID is the Azure Client ID of the Resource Group Managed Identity. Set in Fuctions App Settings + # RG_MID_CLIENT_ID is the Azure Client ID of the Resource Group Managed Identity. Set in Functions App Settings client_id = os.getenv("RG_MID_CLIENT_ID") my_credential = DefaultAzureCredential(managed_identity_client_id=client_id) @@ -69,19 +69,22 @@ def main(req: func.HttpRequest) -> func.HttpResponse: # Start working with form recognizer ################################################################## - # All set as environment varibales in Fuctions App Settings + # All set as environment variables in Functions App Settings fr_endpoint = os.getenv("AZURE_FORM_RECOGNIZER_ENDPOINT") apim_key = os.getenv("AZURE_FORM_RECOGNIZER_KEY") model_id = os.getenv("CUSTOM_BUILT_MODEL_ID") + if not (fr_endpoint and apim_key and model_id): + raise ValueError("Form Recognizer endpoint, key, or model ID not found in environment variables.") + document_analysis_client = DocumentAnalysisClient( - endpoint=fr_endpoint, credential=AzureKeyCredential(apim_key)) + endpoint=fr_endpoint, credential=AzureKeyCredential(apim_key)) poller = document_analysis_client.begin_analyze_document_from_url( - model=model_id, document_url=input_blob_url) + model_id=model_id, document_url=input_blob_url) analyzedResult = poller.result() # analyzedResult -AnalyzeResult Class - logging.info(f"form recognizer analyzed successfully.") + logging.info(f"Form recognizer analyzed successfully.") ################################################################## # End working with form recognizer @@ -159,6 +162,7 @@ def main(req: func.HttpRequest) -> func.HttpResponse: except Exception as ex_main: logging.error(f"Exception occurred: {ex_main}. ") + return func.HttpResponse(body=f"Error: {str(ex_main)}", status_code=500) else: logging.info(f"Processing Successful. Ready save data to Azure Data Lake Storage and then to send response to sender.") output_data_json = json.dumps(output_data) @@ -168,5 +172,4 @@ def main(req: func.HttpRequest) -> func.HttpResponse: output_blob_client = BlobClient(account_url=my_account_url,container_name=output_container, blob_name=output_file_path_no_container, credential=my_credential) output_blob_client.upload_blob(output_data_json, overwrite=True, blob_type="BlockBlob") # return output data to caller as http body - return func.HttpResponse(body=output_data_json, status_code=200) - + return func.HttpResponse(body=output_data_json, status_code=200) \ No newline at end of file diff --git a/Deployment/Code/requirements.txt b/Deployment/Code/requirements.txt index 4aa63f6..2eddfd2 100644 --- a/Deployment/Code/requirements.txt +++ b/Deployment/Code/requirements.txt @@ -7,7 +7,7 @@ azure-storage-blob azure-identity requests #azure-ai-formrecognizer -azure-ai-formrecognizer==3.2.0b3 +azure-ai-formrecognizer==3.3.0 PyPDF2==2.5.0