File size: 1,868 Bytes
bd3e8b6
 
0aa24e5
bd3e8b6
 
3265c70
bd3e8b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60fa672
 
 
 
 
bd3e8b6
 
 
60fa672
bd3e8b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, jsonify, request, send_file, render_template
from flask_cors import CORS
from lrp_pipeline_2 import lrp_main
from utils import create_folders, delete_folders, create_zip_file
from cam_pipeline import cam_main
import os

app = Flask(__name__)
CORS(app)


@app.route("/api/upload", methods=["GET"])
def get_data():
    data = {"message": "Hello from Flask backend!"}
    return jsonify(data)


@app.route("/api/upload", methods=["POST"])
def submit_data():
    # first clear all the existing files in uploads, heatmaps, segmentations, tables, cell_descriptors folders
    folder_names = [
        "uploads",
        "heatmaps",
        "segmentations",
        "tables",
        "cell_descriptors",
    ]
    delete_folders(folder_names)
    create_folders(folder_names)

    # Ensure the uploads directory exists
    uploads_dir = "uploads"
    if not os.path.exists(uploads_dir):
        os.makedirs(uploads_dir)

    # then upload the submitted file(s)
    file = list(dict(request.files).values())[0]
    print(file)
    file.save(os.path.join(uploads_dir, file.filename))  # Save to 'uploads' directory

    # Process data here
    return jsonify({"message": "Data received successfully!"})


@app.route("/api/inputform", methods=["POST"])
def submit_form():
    data = dict(request.json)  # format of data: {'model': 'VGGNet', 'xaiMethod': 'LRP'}
    print(data)
    if "LRP" in data["xaiMethod"]:
        # pixel_ratio = data['pixelRatio']
        return lrp_main(float(data["magval"]))  # pixel_ratio
    elif "GradCAM++" in data["xaiMethod"]:
        # pixel_ratio = data['pixelRatio']
        return cam_main(float(data["magval"]))  # pixel_ratio


@app.route("/api/zip", methods=["GET"])
def get_csv():
    create_zip_file()
    return send_file("outputs.zip", as_attachment=True)


if __name__ == "__main__":
    app.run(debug=True)