Skip to content
Snippets Groups Projects
Commit 5a6d4bc5 authored by Mia Wang's avatar Mia Wang
Browse files

flask send data

parent f08c1f73
No related branches found
No related tags found
No related merge requests found
from dataclasses import dataclass from dataclasses import dataclass
from flask import Flask, request, current_app, send_file, send_from_directory from flask import Flask, request, current_app, send_file, send_from_directory, redirect, url_for, session
from flask_cors import CORS, cross_origin from flask_cors import CORS, cross_origin
import os import os
...@@ -24,87 +24,86 @@ CORS(app) ...@@ -24,87 +24,86 @@ CORS(app)
@app.route('/home', methods=["GET", "POST"]) @app.route('/home', methods=["GET", "POST"])
# @cross_origin() # @cross_origin()
def get_form_data(): def get_form_data():
print('@@@ in Flask Home')
# form_data = request.get_json()
# form_data = request.files['ds_upload']
# print('@@@ form_data', form_data)
form_data = request.form
print('@@@ this is form data', form_data)
# required input
ds = form_data['select_dataset'] # pick dataset (MNIST, KMNIST, FashionMNIST, CIFAR10, CIFAR100)
IsLeNet = form_data["select_network"] # using LeNet or EasyNet or SimpleNet ->> default
auto_aug_learner = form_data["select_learner"] # augmentation methods to be excluded
print('@@@ required user input:', 'ds', ds, 'IsLeNet:', IsLeNet, 'auto_aug_leanrer:',auto_aug_learner)
# advanced input
if form_data['batch_size'] != 'undefined':
batch_size = form_data['batch_size'] # size of batch the inner NN is trained with
else:
batch_size = 1 # this is for demonstration purposes
if form_data['learning_rate'] != 'undefined':
learning_rate = form_data['learning_rate'] # fix learning rate
else:
learning_rate = 10-1
if form_data['toy_size'] != 'undefined':
toy_size = form_data['toy_size'] # total propeortion of training and test set we use
else:
toy_size = 1 # this is for demonstration purposes
if form_data['iterations'] != 'undefined':
iterations = form_data['iterations'] # total iterations, should be more than the number of policies
else:
iterations = 10
exclude_method = form_data['select_action']
print('@@@ advanced search: batch_size:', batch_size, 'learning_rate:', learning_rate, 'toy_size:', toy_size, 'iterations:', iterations, 'exclude_method', exclude_method)
if request.method == 'POST':
# default values print('@@@ in Flask Home')
max_epochs = 10 # max number of epochs that is run if early stopping is not hit
early_stop_num = 10 # max number of worse validation scores before early stopping is triggered form_data = request.form
num_policies = 5 # fix number of policies print('@@@ this is form data', form_data)
num_sub_policies = 5 # fix number of sub-policies in a policy
# required input
ds = form_data['select_dataset'] # pick dataset (MNIST, KMNIST, FashionMNIST, CIFAR10, CIFAR100)
# if user upload datasets and networks, save them in the database IsLeNet = form_data["select_network"] # using LeNet or EasyNet or SimpleNet ->> default
if ds == 'Other': auto_aug_learner = form_data["select_learner"] # augmentation methods to be excluded
ds_folder = request.files['ds_upload']
print('!!!ds_folder', ds_folder) print('@@@ required user input:', 'ds', ds, 'IsLeNet:', IsLeNet, 'auto_aug_leanrer:',auto_aug_learner)
ds_name_zip = ds_folder.filename # advanced input
ds_name = ds_name_zip.split('.')[0] if form_data['batch_size'] != 'undefined':
ds_folder.save('./datasets/'+ ds_name_zip) batch_size = form_data['batch_size'] # size of batch the inner NN is trained with
with zipfile.ZipFile('./datasets/'+ ds_name_zip, 'r') as zip_ref: else:
zip_ref.extractall('./datasets/upload_dataset/') batch_size = 1 # this is for demonstration purposes
if not current_app.debug: if form_data['learning_rate'] != 'undefined':
os.remove(f'./datasets/{ds_name_zip}') learning_rate = form_data['learning_rate'] # fix learning rate
else: else:
ds_name_zip = None learning_rate = 10-1
ds_name = None if form_data['toy_size'] != 'undefined':
toy_size = form_data['toy_size'] # total propeortion of training and test set we use
# test if uploaded dataset meets the criteria else:
for (dirpath, dirnames, filenames) in os.walk(f'./datasets/upload_dataset/{ds_name}/'): toy_size = 1 # this is for demonstration purposes
for dirname in dirnames: if form_data['iterations'] != 'undefined':
if dirname[0:6] != 'class_': iterations = form_data['iterations'] # total iterations, should be more than the number of policies
return None # neet to change render to a 'failed dataset webpage' else:
iterations = 10
# save the user uploaded network exclude_method = form_data['select_action']
if IsLeNet == 'Other': print('@@@ advanced search: batch_size:', batch_size, 'learning_rate:', learning_rate, 'toy_size:', toy_size, 'iterations:', iterations, 'exclude_method', exclude_method)
childnetwork = request.files['network_upload']
childnetwork.save('./child_networks/'+childnetwork.filename)
network_name = childnetwork.filename # default values
else: max_epochs = 10 # max number of epochs that is run if early stopping is not hit
network_name = None early_stop_num = 10 # max number of worse validation scores before early stopping is triggered
num_policies = 5 # fix number of policies
num_sub_policies = 5 # fix number of sub-policies in a policy
print("@@@ user input has all stored in the app")
data = {'ds': ds, 'ds_name': ds_name_zip, 'IsLeNet': IsLeNet, 'network_name': network_name, # if user upload datasets and networks, save them in the database
'auto_aug_learner':auto_aug_learner, 'batch_size': batch_size, 'learning_rate': learning_rate, if ds == 'Other':
'toy_size':toy_size, 'iterations':iterations, 'exclude_method': exclude_method, } ds_folder = request.files['ds_upload']
print('!!!ds_folder', ds_folder)
current_app.config['data'] = data ds_name_zip = ds_folder.filename
ds_name = ds_name_zip.split('.')[0]
print('@@@ all data sent', current_app.config['data']) ds_folder.save('./datasets/'+ ds_name_zip)
with zipfile.ZipFile('./datasets/'+ ds_name_zip, 'r') as zip_ref:
zip_ref.extractall('./datasets/upload_dataset/')
if not current_app.debug:
os.remove(f'./datasets/{ds_name_zip}')
else:
ds_name_zip = None
ds_name = None
# test if uploaded dataset meets the criteria
for (dirpath, dirnames, filenames) in os.walk(f'./datasets/upload_dataset/{ds_name}/'):
for dirname in dirnames:
if dirname[0:6] != 'class_':
return None # neet to change render to a 'failed dataset webpage'
# save the user uploaded network
if IsLeNet == 'Other':
childnetwork = request.files['network_upload']
childnetwork.save('./child_networks/'+childnetwork.filename)
network_name = childnetwork.filename
else:
network_name = None
print("@@@ user input has all stored in the app")
data = {'ds': ds, 'ds_name': ds_name_zip, 'IsLeNet': IsLeNet, 'network_name': network_name,
'auto_aug_learner':auto_aug_learner, 'batch_size': batch_size, 'learning_rate': learning_rate,
'toy_size':toy_size, 'iterations':iterations, 'exclude_method': exclude_method, }
current_app.config['data'] = data
print('@@@ all data sent', current_app.config['data'])
# try this if you want it might work, it might not # try this if you want it might work, it might not
# wapp_util.parse_users_learner_spec( # wapp_util.parse_users_learner_spec(
...@@ -114,8 +113,11 @@ def get_form_data(): ...@@ -114,8 +113,11 @@ def get_form_data():
# max_epochs, # max_epochs,
# **data, # **data,
# ) # )
else:
return {'data': 'all stored'} print('it is GET method')
data = current_app.config['data']
return data
# return redirect(url_for('confirm', data=data))
...@@ -123,9 +125,12 @@ def get_form_data(): ...@@ -123,9 +125,12 @@ def get_form_data():
# ======================================================================== # ========================================================================
@app.route('/confirm', methods=['POST', 'GET']) @app.route('/confirm', methods=['POST', 'GET'])
@cross_origin() @cross_origin()
def confirm(): def confirm():
print('inside confirm page') print('inside confirm page')
data = current_app.config['data'] data = current_app.config['data']
print("current_app.config['data']", current_app.config['data'])
# print("session.get('data')", session.get('data'))
# data = request.args.get('data')
return data return data
...@@ -145,11 +150,11 @@ def training(): ...@@ -145,11 +150,11 @@ def training():
# fake training # fake training
print('pretend it is training') print('pretend it is training')
time.sleep(3) time.sleep(1)
print('epoch: 1') print('epoch: 1')
time.sleep(3) time.sleep(1)
print('epoch: 2') print('epoch: 2')
time.sleep(3) time.sleep(1)
print('epoch: 3') print('epoch: 3')
print('it has finished training') print('it has finished training')
......
{ {
"files": { "files": {
"main.css": "/static/css/main.073c9b0a.css", "main.css": "/static/css/main.073c9b0a.css",
"main.js": "/static/js/main.a39dcf9a.js", "main.js": "/static/js/main.1009a38b.js",
"static/js/787.5006ed28.chunk.js": "/static/js/787.5006ed28.chunk.js", "static/js/787.5006ed28.chunk.js": "/static/js/787.5006ed28.chunk.js",
"index.html": "/index.html", "index.html": "/index.html",
"main.073c9b0a.css.map": "/static/css/main.073c9b0a.css.map", "main.073c9b0a.css.map": "/static/css/main.073c9b0a.css.map",
"main.a39dcf9a.js.map": "/static/js/main.a39dcf9a.js.map", "main.1009a38b.js.map": "/static/js/main.1009a38b.js.map",
"787.5006ed28.chunk.js.map": "/static/js/787.5006ed28.chunk.js.map" "787.5006ed28.chunk.js.map": "/static/js/787.5006ed28.chunk.js.map"
}, },
"entrypoints": [ "entrypoints": [
"static/css/main.073c9b0a.css", "static/css/main.073c9b0a.css",
"static/js/main.a39dcf9a.js" "static/js/main.1009a38b.js"
] ]
} }
\ No newline at end of file
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>MetaRL for AutoAugmentation</title><script defer="defer" src="/static/js/main.a39dcf9a.js"></script><link href="/static/css/main.073c9b0a.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html> <!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>MetaRL for AutoAugmentation</title><script defer="defer" src="/static/js/main.1009a38b.js"></script><link href="/static/css/main.073c9b0a.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
\ No newline at end of file \ No newline at end of file
...@@ -9,8 +9,9 @@ export default function Confirm() { ...@@ -9,8 +9,9 @@ export default function Confirm() {
const [dataset, setDataset] = useState() const [dataset, setDataset] = useState()
const [network, setNetwork] = useState() const [network, setNetwork] = useState()
// console.log('already in confirm react')
useEffect(() => { useEffect(() => {
const res = fetch('/confirm').then( const res = fetch('/home').then(
response => response.json() response => response.json()
).then(data => {setMyData(data); ).then(data => {setMyData(data);
if (data.ds == 'Other'){setDataset(data.ds_name)} else {setDataset(data.ds)}; if (data.ds == 'Other'){setDataset(data.ds_name)} else {setDataset(data.ds)};
......
...@@ -60,7 +60,11 @@ export default function Home() { ...@@ -60,7 +60,11 @@ export default function Home() {
method: 'POST', method: 'POST',
body: formData body: formData
}).then((response) => response.json()); }).then((response) => response.json());
// {console.log('before calling response');
// response.json();
// console.log('after calling response')});
console.log('before navigate to confirm')
navigate('/confirm', {replace:true}); navigate('/confirm', {replace:true});
// //
///////// testing ///////// testing
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment