added sops generation for personal assessment

This commit is contained in:
2024-09-05 02:59:01 +00:00
parent 6f781cb90a
commit 67d30fbc6a
6 changed files with 368 additions and 71 deletions
+111 -32
View File
@@ -2,7 +2,9 @@ import os
from flask import Blueprint, request, jsonify, current_app
from werkzeug.utils import secure_filename
from src.services.sop_generator import SopGenerator
from src.services.sop_generator import (SopGenerator,SopGeneratorDocument,
SopPersonalAssessment)
from src.utils.utils import delete_all_files_in_directory
from src.utils.document_loader import load_document
import json
@@ -59,8 +61,8 @@ def get_roles():
@sops_bp.route('/generate_sops_from_doc', methods=['POST'])
def generate_sops():
@sops_bp.route('/generate_questions_from_doc', methods=['POST'])
def generate_questions_from_sop():
# Check if the POST request has the file part
if 'document' not in request.files:
return jsonify({"error": "No file part", "message": "Please upload a file with the key 'document'."}), 400
@@ -101,7 +103,8 @@ def generate_sops():
return jsonify({"error": "Document cannot extract SOPs", "message": status_check["message"]}), 400
# Generate SOPs based on the roles provided
sops = sop_generator.generate_sops_from_doc(roles, docs)
sop_generator = SopGeneratorDocument()
sops = sop_generator.generate_sops_from_doc(docs)
# Cleanup: Delete all files in the upload directory after processing
delete_all_files_in_directory(upload_folder)
@@ -117,44 +120,120 @@ def generate_sops():
@sops_bp.route('/generate_sops_from_info', methods=['POST'])
def generate_sops_from_info():
@sops_bp.route('/generate_sops_from_doc', methods=['POST'])
def generate_sops():
# Check if the POST request has the file part
if 'document' not in request.files:
return jsonify({"error": "No file part", "message": "Please upload a file with the key 'document'."}), 400
print("Running................")
file = request.files['document']
# If the user does not select a file, the browser may also submit an empty part without a filename
if file.filename == '':
return jsonify({"error": "No selected file", "message": "A file was not selected for upload. Please select a valid file."}), 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
upload_folder = current_app.config['UPLOAD_FOLDER']
file_path = os.path.join(upload_folder, filename)
# Save the file to the upload folder
file.save(file_path)
try:
# Use the utility function to generate docs from the file
docs = load_document(file_path)
# Generate SOPs based on the roles provided
sop_generator = SopGeneratorDocument()
sops = sop_generator.extract_sops_from_doc(docs)
# Cleanup: Delete all files in the upload directory after processing
delete_all_files_in_directory(upload_folder)
if not sops:
return jsonify({"error":"Error in generating sops"})
return jsonify({"sops": sops, "message": "SOPs successfully generated for the roles from the document."}), 200
except Exception as e:
# Cleanup: Delete all files in the upload directory if an error occurs
delete_all_files_in_directory(upload_folder)
return jsonify({"error": "Processing error", "message": f"An error occurred while processing the document: {str(e)}"}), 500
return jsonify({"error": "File type not allowed", "message": "The uploaded file type is not allowed. Please upload a PDF, DOC, or DOCX file."}), 400
@sops_bp.route('/personal_assessment/generate_sops_from_questionnaire', methods=['POST'])
def generate_sops_from_questionnaire():
"""
Generate SOPs based on role information provided in the request body.
Generate SOPs based on the questionnaire data provided in the request body.
The request body is expected to contain plain-text information for vision, roles, responsibilities, and project details.
"""
try:
# Get role information from the request body
roles_info = request.json.get('roles_info')
if not roles_info:
return jsonify({"error": "No role information provided", "message": "Please provide role information in the 'roles_info' field."}), 400
# Generate SOPs based on the provided role information
sops_response = sop_generator.generate_sops_from_info(roles=roles_info)
return jsonify({"sops": sops_response, "message": "SOPs successfully generated based on the provided role information."}), 200
# Get the questionnaire data from the request body
questionnaire_data = request.json
# Validate the required fields in the questionnaire data
if not questionnaire_data.get('vision') or not questionnaire_data.get('roles') or not questionnaire_data.get('responsibilities'):
return jsonify({
"error": "Missing required fields",
"message": "Please provide 'vision', 'roles', and 'responsibilities' in the request body."
}), 400
# Step 1: Call the function from the sop_generator
sop_generator = SopPersonalAssessment()
sops_response = sop_generator.extract_sops_from_questionnaire(questionnaire_data)
# Step 2: Return the SOPs if the extraction is successful
if not sops_response:
return jsonify({
"error": "SOP generation failed",
"message": "Failed to generate SOPs based on the provided questionnaire data."
}), 500
return jsonify({
"sops": sops_response,
"message": "SOPs successfully generated based on the provided questionnaire data."
}), 200
except Exception as e:
return jsonify({"error": "Processing error", "message": f"An error occurred while generating SOPs: {str(e)}"}), 500
return jsonify({
"error": "Processing error",
"message": f"An error occurred while generating SOPs: {str(e)}"
}), 500
@sops_bp.route('/generate_sops_by_role_and_area', methods=['POST'])
def generate_sops_by_role_and_area():
@sops_bp.route('/personal_assessment/generate_sops_by_roles_and_areas', methods=['POST'])
def generate_sops_by_roles_and_areas():
"""
Generate SOPs based on the roles, SOP types (will, shall, must), and areas provided in the request body.
"""
try:
# Get role and area from the request body
role = request.json.get('role')
area = request.json.get('area')
if not role or not area:
return jsonify({"error": "Missing parameters", "message": "Both 'role' and 'area' fields are required."}), 400
# Generate SOPs based on the provided role and area
sops_response = sop_generator.generate_sops_by_role_and_area(role=role, area=area)
return jsonify({"sops": sops_response, "message": f"SOPs successfully generated for role '{role}' in area '{area}'."}), 200
# Get the roles data from the request body
roles = request.json.get('roles', None)
sop_generator = SopPersonalAssessment()
# Validate the presence of roles data
if not roles or not isinstance(roles, list):
return jsonify({"error": "Invalid input", "message": "The 'roles' field should be a non-empty list."}), 400
# Generate SOPs for all roles at once
sops_response = sop_generator.generate_sops_by_role_and_area(roles=roles)
return jsonify({
"sops": sops_response,
"message": "SOPs successfully generated for all provided roles."
}), 200
except Exception as e:
return jsonify({"error": "Processing error", "message": f"An error occurred while generating SOPs: {str(e)}"}), 500
return jsonify({
"error": "Processing error",
"message": f"An error occurred while generating SOPs: {str(e)}"
}), 500
@sops_bp.route('/executive/generate_sops_from_questionnaire', methods=['POST'])