added worker validation bot
This commit is contained in:
@@ -2,6 +2,7 @@ import os
|
||||
from flask import Flask
|
||||
from src.api.routes.sops import sops_bp
|
||||
from src.api.routes.questions import qs_b
|
||||
from src.api.routes.chatbot import bot
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
@@ -9,6 +10,7 @@ def create_app():
|
||||
# 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')
|
||||
app.register_blueprint(bot,url_prefix='/api/v1/bot')
|
||||
|
||||
# Set up the upload folder configuration inside the src directory
|
||||
UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../uploads')
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import os
|
||||
from flask import Blueprint, request, jsonify, current_app
|
||||
from werkzeug.utils import secure_filename
|
||||
from src.services.chatbot import Chatbot
|
||||
from src.utils.utils import delete_all_files_in_directory
|
||||
from src.utils.document_loader import load_document
|
||||
|
||||
|
||||
# Initialize the Blueprint
|
||||
bot = Blueprint('chatbot', __name__)
|
||||
|
||||
# Allowed file extensions
|
||||
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
|
||||
|
||||
@bot.route('/validate_worker_document', methods=['POST'])
|
||||
def validate_worker_document():
|
||||
try:
|
||||
# Retrieve form data
|
||||
question = request.form.get('question')
|
||||
file = request.files.get('document')
|
||||
|
||||
if not question or not file:
|
||||
return jsonify({"error": "Missing data", "message": "Question or document not provided."}), 400
|
||||
|
||||
if file.filename == '':
|
||||
return jsonify({"error": "No selected file", "message": "No file was selected for upload."}), 400
|
||||
|
||||
if file and allowed_file(file.filename):
|
||||
# Secure the file name and save it
|
||||
filename = secure_filename(file.filename)
|
||||
upload_folder = current_app.config['UPLOAD_FOLDER']
|
||||
file_path = os.path.join(upload_folder, filename)
|
||||
file.save(file_path)
|
||||
|
||||
# Load the document for processing
|
||||
docs = load_document(file_path)
|
||||
|
||||
# Instantiate the chatbot service
|
||||
chatbot = Chatbot()
|
||||
|
||||
# Validate the worker's response using the provided document
|
||||
validation_result = chatbot.validate_worker(question, docs)
|
||||
|
||||
if not validation_result:
|
||||
return jsonify({"error": "Validation failed", "message": "Validation process failed."}), 400
|
||||
|
||||
# Clean up uploaded file (optional)
|
||||
os.remove(file_path)
|
||||
|
||||
# Return the validation result
|
||||
return jsonify({"result": validation_result["result"]}), 200
|
||||
|
||||
else:
|
||||
return jsonify({"error": "Invalid file", "message": "File format not supported."}), 400
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": "Internal Server Error", "message": str(e)}), 500
|
||||
Reference in New Issue
Block a user