• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python How to send data frame to another neural net in flask route

koidula

New Coder
So, I have flask app with py-cicflowmeter. Purpose - detection of network attacks. According to my idea, two neural networks are used. The first is a fast binary LSTM classifier, and the second is a slow multiclass KNN. Both networks are pretrained and have 23 inputs each, with the same input data types and names.
flask-app.png

So, I can't overcome the errors that the JSON Decoder gives. Often, this indicates an unexpected value for the if-else construction. I tested the neural networks separately from each other and each of them works and returns data correctly after np.argmax
Python:
@app.route('/predict/', methods=['POST'])
#@login_required
def predict():
req = request.get_json()
df1 = pd.DataFrame(data=req["data"], columns=req["columns"] )
df2 = df1.copy()
cols = ['pkt_len_var', 'active_min', ' Fwd IAT Std',
        'active_mean','active_max', ' min_seg_size_forward',
        'flow_iat_std', 'bwd_iat_mean',
        ' PSH Flag Count', ' Flow IAT Mean', 'Total Length of Fwd Packets',
        ' Subflow Fwd Bytes', 'bwd_iat_min','active_std', ' Bwd IAT Max',
        'fwd_psh_flags', 'syn_flag_cnt', 'fwd_iat_mean', 'bwd_iat_std',
        'flow_iat_min', 'Init_Win_bytes_forward', ' Bwd Packet Length Min', 'fwd_iat_min']
feature = df1[cols]

# Making Prediction
pred_raw_prod_bin = model_bin.predict(feature, batch_size=256)
pred_prod_bin = np.argmax(pred_raw_prod_bin, axis=1)
label = pred_prod_bin[0]

if label == 0:
    df1['label'] == 'Benign'
else:
    feature = np.array(feature)
    pred_raw_prod_multi = model_multi.predict(feature, batch_size=256)
    pred_prod_multi = np.argmax(pred_raw_prod_multi, axis=1)
    label = pred_prod_multi[0]
    if label == 0:
        df1['label'] == 'Bot'
    if label == 1:
        df1['label'] == 'DDoS'
    if label == 2:
        df1['label'] == 'DoS Golden Eye'
    if label == 3:
        df1['label'] == 'DoS Hulk'
    if label == 4:
        df1['label'] == 'DoS Slow Http Test'
    if label == 5:
        df1['label'] == 'DoS Slowloris'
    if label == 6:
        df1['label'] == 'FTP Patator'
    if label == 7:
        df1['label'] == 'Heartbleed'
    if label == 8:
        df1['label'] == 'Infiltration'
    if label == 9:
        df1['label'] == 'Port Scan'
    if label == 10:
        df1['label'] == 'SSH Patator'
    if label == 11:
        df1['label'] == 'BruteForce'
    if label == 12:
        df1['label'] == 'SQL Injection'
    if label == 13:
        df1['label'] == 'XSS'

#if label == 0:
#    df1['label'] = 'Benign'
#if label == 1:
#    df1['label'] = 'Attack'

df1.rename(columns = {" Destination Port": "dst_port"},
    inplace = True)

result = df1.to_json(orient="records")   
sse.publish(result, type='greeting')
resp = {"label" :  df1['label'].values[0]}
print(resp)
return jsonify(resp)

Bash:
File "/home/miko/SIAST-Tech/venv/lib/python3.8/site-packages/flask/app.py", line 2095, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/miko/SIAST-Tech/venv/lib/python3.8/site-packages/flask/app.py", line 2080, in wsgi_app
    response = self.handle_exception(e)
  File "/home/miko/SIAST-Tech/venv/lib/python3.8/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/miko/SIAST-Tech/venv/lib/python3.8/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/miko/SIAST-Tech/venv/lib/python3.8/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/miko/SIAST-Tech/venv/lib/python3.8/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/miko/SIAST-Tech/app/routes.py", line 279, in predict
    df1['label'] == 'DoS Hulk'
  File "/home/miko/SIAST-Tech/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 3024, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/miko/SIAST-Tech/venv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: 'label'
Exception in thread AsyncSniffer:
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 946, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.10/dist-packages/scapy-2.4.5-py3.10.egg/scapy/sendrecv.py", line 1210, in _run
    session.on_packet_received(p)
  File "/usr/local/lib/python3.10/dist-packages/cicflowmeter-0.1.6-py3.10.egg/cicflowmeter/flow_session.py", line 108, in on_packet_received
  File "/usr/local/lib/python3.10/dist-packages/cicflowmeter-0.1.6-py3.10.egg/cicflowmeter/flow_session.py", line 142, in garbage_collect
  File "/usr/lib/python3/dist-packages/requests/models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
 
Top