added questions generator

This commit is contained in:
OwusuBlessing
2024-09-10 02:51:31 +01:00
parent 286ff0e61e
commit 348c871abc
9 changed files with 271 additions and 39 deletions
+2
View File
@@ -1,12 +1,14 @@
import os
from flask import Flask
from src.api.routes.sops import sops_bp
from src.api.routes.questions import qs_b
def create_app():
app = Flask(__name__)
# Register the blueprint with the desired prefix
app.register_blueprint(sops_bp, url_prefix='/api/v1/sop')
app.register_blueprint(qs_b,url_prefix='/api/v1/qs')
# Set up the upload folder configuration inside the src directory
UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../uploads')
+67
View File
@@ -0,0 +1,67 @@
import os
from flask import Blueprint, request, jsonify, current_app
from werkzeug.utils import secure_filename
from src.services.sop_generator import (SopPersonalAssessment,SopGeneratorExecutive)
from src.services.sop_document_parser import DocumentParser
from src.services.questions_generator import QuestionsGenerator
from src.utils.utils import delete_all_files_in_directory
from src.utils.document_loader import load_document
import json
# Initialize the Blueprint
qs_b = Blueprint('questions', __name__)
# Initialize SopGenerator
ALLOWED_EXTENSIONS = {'pdf', 'doc', 'docx'}
def allowed_file(filename):
"""Check if the file has an allowed extension."""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@qs_b.route('/generate_questions_from_sop', methods=['POST'])
def generate_questions_from_sop():
# Check if the request contains data
if not request.is_json:
return jsonify({"error": "Invalid input", "message": "Input data must be in JSON format."}), 400
# Parse the incoming JSON data
input_data = request.get_json()
# Validate required fields
required_fields = ['sops', 'assessment_type', 'frequency_type', 'duration']
for field in required_fields:
if field not in input_data:
return jsonify({"error": "Missing data", "message": f"'{field}' is required."}), 400
try:
# Extract fields from input_data
sops = input_data['sops']
assessment_type = input_data['assessment_type']
frequency_type = input_data['frequency_type']
duration = input_data['duration']
# Prepare the data for the generator
generator_input = {
"sops": json.dumps(sops), # Convert SOPs to JSON string
"assessment_type": assessment_type,
"frequency_type": frequency_type,
"duration": duration
}
# Call the generator to create questions
generator = QuestionsGenerator()
questions_response = generator.generate_questions(generator_input)
# Check if the response is valid
if not questions_response:
return jsonify({"error": "Question generation failed", "message": "Could not generate questions from the provided data."}), 500
# Return the generated questions
return jsonify({"questions": questions_response}), 200
except Exception as e:
return jsonify({"error": "Internal Server Error", "message": str(e)}), 500