added apis for executives sops

This commit is contained in:
2024-09-06 01:48:32 +00:00
parent 67d30fbc6a
commit 690f80bc6e
6 changed files with 571 additions and 375 deletions
+108 -37
View File
@@ -2,8 +2,8 @@ import os
from flask import Blueprint, request, jsonify, current_app
from werkzeug.utils import secure_filename
from src.services.sop_generator import (SopGenerator,SopGeneratorDocument,
SopPersonalAssessment)
from src.services.sop_generator import (SopGeneratorDocument,
SopPersonalAssessment,SopGeneratorExecutive)
from src.utils.utils import delete_all_files_in_directory
from src.utils.document_loader import load_document
@@ -12,7 +12,7 @@ import json
sops_bp = Blueprint('sops', __name__)
# Initialize SopGenerator
sop_generator = SopGenerator()
ALLOWED_EXTENSIONS = {'pdf', 'doc', 'docx'}
@@ -120,7 +120,7 @@ def generate_questions_from_sop():
@sops_bp.route('/generate_sops_from_doc', methods=['POST'])
@sops_bp.route('/personal_assessment/generate_sops_from_doc', methods=['POST'])
def generate_sops():
# Check if the POST request has the file part
if 'document' not in request.files:
@@ -167,7 +167,7 @@ def generate_sops():
@sops_bp.route('/personal_assessment/generate_sops_from_questionnaire', methods=['POST'])
def generate_sops_from_questionnaire():
def generate_sops_from_questionnaire_per():
"""
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.
@@ -236,31 +236,28 @@ def generate_sops_by_roles_and_areas():
}), 500
@sops_bp.route('/executive/generate_sops_from_questionnaire', methods=['POST'])
def generate_executive_sops_from_questionnaire():
try:
# Get data from the request body
data = request.json
# Generate SOPs based on the questionnaire answers
sops_response = sop_generator.generate_executive_sops_from_questionnaire(data)
return jsonify({"sops": sops_response, "message": "SOPs successfully generated from the questionnaire."}), 200
except Exception as e:
return jsonify({"error": "Processing error", "message": f"An error occurred while generating SOPs: {str(e)}"}), 500
@sops_bp.route('/executive/generate_sops_from_doc', methods=['POST'])
@sops_bp.route('/executive/generate_sop_mission_from_vision', methods=['POST'])
def generate_executive_sops_from_doc():
"""
Generate SOPs for executives based on a document containing vision and mission.
"""
# Check if the POST request has the file part
# Check if the POST request has the file part and roles
if 'document' not in request.files:
return jsonify({"error": "No file part", "message": "Please upload a file with the key 'document'."}), 400
if 'executives' not in request.form:
return jsonify({"error": "No roles provided", "message": "Please provide roles as a JSON array."}), 400
try:
executives = request.form.get('executives')
# Manually load roles from the string to JSON
executives = json.loads(executives)
except json.JSONDecodeError:
return jsonify({"error": "Invalid JSON", "message": "The roles must be a valid JSON array."}), 400
except ValueError as e:
return jsonify({"error": "Invalid roles format", "message": str(e)}), 400
file = request.files['document']
@@ -280,29 +277,32 @@ def generate_executive_sops_from_doc():
# Use the utility function to generate docs from the file
docs = load_document(file_path)
# Use LLM to extract Vision and Mission sections from the document
vision_section, mission_section = sop_generator.extract_vision_and_mission(docs)
sop_doc = SopGeneratorDocument()
vision_mission = sop_doc.extract_vision_mission(docs)
if not vision_mission:
return jsonify({"error": "Vision and Mission generation error ", "message": "Error in generating mssion and viso."}), 400
if not vision_section or not mission_section:
# Check if both vision and mission are empty
if not vision_mission.get('vision') and not vision_mission.get('mission'):
# Cleanup: Delete all files in the upload directory if parsing fails
delete_all_files_in_directory(upload_folder)
return jsonify({"error": "Missing Vision and Mission", "message": "The document does not contain or properly define the company's vision and mission."}), 400
return jsonify({"error": "Missing Vision and Mission", "message": "The document does not contain mission and vision."}), 400
# Organize extracted data
extracted_data = {
"role": "Executive",
"organization vision": [vision_section],
"organization strategic goals": [mission_section]
}
print(f"Vision and mission: {vision_mission}")
# Generate SOPs based on the extracted vision and goals
sops_response = sop_generator.generate_executive_sops_from_questionnaire(extracted_data)
ex_sop = SopGeneratorExecutive()
result = {}
for exe in executives:
sops = ex_sop.extract_sops_from_executive_vision_goals_doc(vision_mission, exe)
result[exe] = sops
# Cleanup: Delete all files in the upload directory after processing
delete_all_files_in_directory(upload_folder)
return jsonify({"sops": sops_response, "message": "SOPs successfully generated from the document."}), 200
return jsonify({"sops": result, "message": "SOPs successfully generated from the document."}), 200
except Exception as e:
# Cleanup: Delete all files in the upload directory if an error occurs
@@ -310,3 +310,74 @@ def generate_executive_sops_from_doc():
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('/executive/generate_sop_managers_doc', methods=['POST'])
def generate_sop_managers_doc():
if 'document' not in request.files:
return jsonify({"error": "No file part", "message": "Please upload a file with the key 'document'."}), 400
file = request.files['document']
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)
file.save(file_path)
try:
docs = load_document(file_path)
sop_generator = SopGeneratorExecutive()
result = sop_generator.generate_sops_for_department_managers(docs)
delete_all_files_in_directory(upload_folder)
if not result:
return jsonify({"error": "Processing error", "message": "Failed to generate SOPs for department managers."}), 500
return jsonify({"sops": result.dict(), "message": "SOPs successfully generated for department managers."}), 200
except Exception as e:
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('/executive/generate_sops_from_questionnaire', methods=['POST'])
def generate_sops_from_questionnaire():
try:
data = request.json
questionnaire_data = data.get('questionnaire')
executives = data.get('executives', [])
managers = data.get('managers', [])
departments = data.get('departments', [])
if not questionnaire_data or not executives or not managers or not departments:
return jsonify({"error": "Missing data", "message": "Please provide questionnaire data, executives, managers, and departments."}), 400
sop_generator = SopGeneratorExecutive()
result = sop_generator.generate_sops_from_questionnaire(questionnaire_data, executives, managers, departments)
if not result:
return jsonify({"error": "Processing error", "message": "Failed to generate SOPs from questionnaire."}), 500
# Convert Pydantic models to dictionaries
serializable_result = {
"executive_sops": {exec: sops.dict() for exec, sops in result["executive_sops"].items()},
"department_sops": result["department_sops"].dict()
}
return jsonify({"sops": serializable_result, "message": "SOPs successfully generated from questionnaire."}), 200
except Exception as e:
return jsonify({"error": "Processing error", "message": f"An error occurred while processing the request: {str(e)}"}), 500