Skip to content
Snippets Groups Projects
Select Git revision
  • 7fa7ef6e4dd7d2ff3e29cb3651762e59cfa4c61d
  • master default protected
  • gitkeep
  • dev protected
  • Issue/2464-invalidateMeta
  • Issue/2309-docs
  • Issue/2462-removeTraces
  • Hotfix/2459-EncodingPath
  • Hotfix/2452-linkedDeletion
  • Issue/1792-newMetadataStructure
  • Hotfix/2371-fixGitLabinRCV
  • Fix/xxxx-activateGitlab
  • Issue/2349-gitlabHttps
  • Issue/2287-guestRole
  • Issue/2102-gitLabResTypeRCV
  • Hotfix/2254-fixContentLenghtCalculation
  • Fix/xxxx-resourceVisibility
  • Issue/1951-quotaImplementation
  • Issue/2162-fixFolderResponse
  • Issue/2158-emailServicedesk
  • Hotfix/2141-fileUploadErrors
  • v3.3.4
  • v3.3.3
  • v3.3.2
  • v3.3.1
  • v3.3.0
  • v3.2.3
  • v3.2.2
  • v3.2.1
  • v3.2.0
  • v3.1.2
  • v3.1.1
  • v3.1.0
  • v3.0.6
  • v3.0.5
  • v3.0.4
  • v3.0.3
  • v3.0.2
  • v3.0.1
  • v3.0.0
  • v2.8.2
41 results

Blob.csproj

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    app.py 3.55 KiB
    from flask import Flask, render_template, request, jsonify, send_from_directory
    import os
    from werkzeug.utils import secure_filename
    import numpy as np
    from tensorflow.keras.models import load_model
    import datetime
    from pytz import timezone
    
    app = Flask(__name__)
    
    # Define the folder for predictions
    PREDICTIONS_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'predictions')
    UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'inputs')
    app.config['PREDICTIONS_FOLDER'] = PREDICTIONS_FOLDER
    app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
    
    # Ensure the predictions folder exists
    os.makedirs(PREDICTIONS_FOLDER, exist_ok=True)
    os.makedirs(UPLOAD_FOLDER, exist_ok=True)
    
    # Load your model
    model = load_model('models/model.h5')
    
    # Set Current Date and Time for naming the files
    now = datetime.datetime.now(timezone('Europe/Berlin'))
    current_time_label = now.strftime("%H%M")
    current_date_label = now.strftime("%Y%m%d_")
    TIMESTAMP = current_date_label+current_time_label
    
    # Flag to indicate whether predictions are available
    predictions_available = False
    
    @app.route('/')
    def index():
        global predictions_available
        return render_template('index.html', predictions_available=predictions_available)
    
    @app.route('/predict', methods=['POST'])
    def predict():
    
        global predictions_available
        
        from matplotlib.pyplot import imsave
        
        # Get input data from the request
        inputfile = request.files['input_file']
        input_data = np.load(inputfile.stream)
        randomint = np.random.randint(0, input_data.shape[0])
        input_filename = secure_filename(inputfile.filename)
        np.save(os.path.join(UPLOAD_FOLDER, f"{os.path.splitext(input_filename)[0]}" + "_" + TIMESTAMP + ".npy"), input_data)
        input_figname = f"{os.path.splitext(input_filename)[0]}" + "_" + TIMESTAMP + ".png"
        imsave(os.path.join(UPLOAD_FOLDER, input_figname), input_data[randomint, :, :, 0], cmap='gray')
    
        # Perform prediction
        TotalData_mean = -595.6387043163428
        TotalData_std = 627.3217112391814
        input_data = (input_data - TotalData_mean) / TotalData_std
        predictions = model.predict(input_data)
        predictions = predictions*TotalData_std + TotalData_mean
    
        # Save predictions as an npy file
        output_filename = f"{os.path.splitext(input_filename)[0]}_pred" + "_" + TIMESTAMP + ".npy"
        output_figname = f"{os.path.splitext(input_filename)[0]}_pred" + "_" + TIMESTAMP + ".png"
        np.save(os.path.join(PREDICTIONS_FOLDER, output_filename), predictions)
        imsave(os.path.join(PREDICTIONS_FOLDER, output_figname), predictions[randomint, :, :, 0], cmap='gray')
    
        # Set the flag to indicate predictions are available
        predictions_available = True
    
        # Redirect to the home page after prediction
        return render_template('index.html', 
                               predictions_available=predictions_available, 
                               download_filename=output_filename, 
                               input_fig=input_figname,
                               output_fig=output_figname)
    
    @app.route('/download/<filename>')
    def download_file(filename):
        file_path = os.path.join(PREDICTIONS_FOLDER, filename)
        print(f"Trying to access file: {file_path}")
        return send_from_directory(PREDICTIONS_FOLDER, filename, as_attachment=True)
    
    @app.route('/inputs/<filename>')
    def input_image(filename):
        return send_from_directory(UPLOAD_FOLDER, filename, as_attachment=True)
    
    @app.route('/predictions/<filename>')
    def predictions_image(filename):
        return send_from_directory(PREDICTIONS_FOLDER, filename, as_attachment=True)
    
    
    if __name__ == '__main__':
        app.run(debug=True)