test / app.py
Prathush21's picture
Update app.py
0aa24e5 verified
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)