Switch to unified view

a b/LSTM - Experiments/Flask_Server.ipynb
1
{"cells":[{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":20453,"status":"ok","timestamp":1693498260019,"user":{"displayName":"Francesco Bassino","userId":"12214195385968794219"},"user_tz":300},"id":"gEU0S3qtbFW0","outputId":"716f7de0-ec82-440a-b058-e8d6be64c457"},"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting flask-ngrok\n","  Downloading flask_ngrok-0.0.25-py3-none-any.whl (3.1 kB)\n","Requirement already satisfied: Flask>=0.8 in /usr/local/lib/python3.10/dist-packages (from flask-ngrok) (2.2.5)\n","Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from flask-ngrok) (2.31.0)\n","Requirement already satisfied: Werkzeug>=2.2.2 in /usr/local/lib/python3.10/dist-packages (from Flask>=0.8->flask-ngrok) (2.3.7)\n","Requirement already satisfied: Jinja2>=3.0 in /usr/local/lib/python3.10/dist-packages (from Flask>=0.8->flask-ngrok) (3.1.2)\n","Requirement already satisfied: itsdangerous>=2.0 in /usr/local/lib/python3.10/dist-packages (from Flask>=0.8->flask-ngrok) (2.1.2)\n","Requirement already satisfied: click>=8.0 in /usr/local/lib/python3.10/dist-packages (from Flask>=0.8->flask-ngrok) (8.1.7)\n","Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->flask-ngrok) (3.2.0)\n","Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->flask-ngrok) (3.4)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->flask-ngrok) (2.0.4)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->flask-ngrok) (2023.7.22)\n","Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from Jinja2>=3.0->Flask>=0.8->flask-ngrok) (2.1.3)\n","Installing collected packages: flask-ngrok\n","Successfully installed flask-ngrok-0.0.25\n","Collecting pyngrok\n","  Downloading pyngrok-6.0.0.tar.gz (681 kB)\n","\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m681.2/681.2 kB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25h  Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n","Requirement already satisfied: PyYAML in /usr/local/lib/python3.10/dist-packages (from pyngrok) (6.0.1)\n","Building wheels for collected packages: pyngrok\n","  Building wheel for pyngrok (setup.py) ... \u001b[?25l\u001b[?25hdone\n","  Created wheel for pyngrok: filename=pyngrok-6.0.0-py3-none-any.whl size=19867 sha256=faf501e1ba558af72d9a7e62617a4d13138fc6b11fd50177d5cff2a1d064aae4\n","  Stored in directory: /root/.cache/pip/wheels/5c/42/78/0c3d438d7f5730451a25f7ac6cbf4391759d22a67576ed7c2c\n","Successfully built pyngrok\n","Installing collected packages: pyngrok\n","Successfully installed pyngrok-6.0.0\n","Requirement already satisfied: flask in /usr/local/lib/python3.10/dist-packages (2.2.5)\n","Requirement already satisfied: Werkzeug>=2.2.2 in /usr/local/lib/python3.10/dist-packages (from flask) (2.3.7)\n","Requirement already satisfied: Jinja2>=3.0 in /usr/local/lib/python3.10/dist-packages (from flask) (3.1.2)\n","Requirement already satisfied: itsdangerous>=2.0 in /usr/local/lib/python3.10/dist-packages (from flask) (2.1.2)\n","Requirement already satisfied: click>=8.0 in /usr/local/lib/python3.10/dist-packages (from flask) (8.1.7)\n","Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from Jinja2>=3.0->flask) (2.1.3)\n"]}],"source":["!pip install flask-ngrok\n","!pip install pyngrok\n","!pip install flask"]},{"cell_type":"code","execution_count":2,"metadata":{"executionInfo":{"elapsed":15,"status":"ok","timestamp":1693498260020,"user":{"displayName":"Francesco Bassino","userId":"12214195385968794219"},"user_tz":300},"id":"JroAYe8raNVY"},"outputs":[],"source":["from flask import Flask, request, jsonify\n","from pyngrok import ngrok\n","from flask_ngrok import run_with_ngrok"]},{"cell_type":"code","execution_count":3,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":970,"status":"ok","timestamp":1693498260977,"user":{"displayName":"Francesco Bassino","userId":"12214195385968794219"},"user_tz":300},"id":"8TDnn6NQfVSj","outputId":"75e7a1f9-0644-4b52-d5b9-c0c9f01bf628"},"outputs":[{"output_type":"stream","name":"stdout","text":["Downloading ngrok ...\rDownloading ngrok: 0%\rDownloading ngrok: 1%\rDownloading ngrok: 2%\rDownloading ngrok: 3%\rDownloading ngrok: 4%\rDownloading ngrok: 5%\rDownloading ngrok: 6%\rDownloading ngrok: 7%\rDownloading ngrok: 8%\rDownloading ngrok: 9%\rDownloading ngrok: 10%\rDownloading ngrok: 11%\rDownloading ngrok: 12%\rDownloading ngrok: 13%\rDownloading ngrok: 14%\rDownloading ngrok: 15%\rDownloading ngrok: 16%\rDownloading ngrok: 17%\rDownloading ngrok: 18%\rDownloading ngrok: 19%\rDownloading ngrok: 20%\rDownloading ngrok: 21%\rDownloading ngrok: 22%\rDownloading ngrok: 23%\rDownloading ngrok: 24%\rDownloading ngrok: 25%\rDownloading ngrok: 26%\rDownloading ngrok: 27%\rDownloading ngrok: 28%\rDownloading ngrok: 29%\rDownloading ngrok: 30%\rDownloading ngrok: 31%\rDownloading ngrok: 32%\rDownloading ngrok: 33%\rDownloading ngrok: 34%\rDownloading ngrok: 35%\rDownloading ngrok: 36%\rDownloading ngrok: 37%\rDownloading ngrok: 38%\rDownloading ngrok: 39%\rDownloading ngrok: 40%\rDownloading ngrok: 41%\rDownloading ngrok: 42%\rDownloading ngrok: 43%\rDownloading ngrok: 44%\rDownloading ngrok: 45%\rDownloading ngrok: 46%\rDownloading ngrok: 47%\rDownloading ngrok: 48%\rDownloading ngrok: 49%\rDownloading ngrok: 50%\rDownloading ngrok: 51%\rDownloading ngrok: 52%\rDownloading ngrok: 53%\rDownloading ngrok: 54%\rDownloading ngrok: 55%\rDownloading ngrok: 56%\rDownloading ngrok: 57%\rDownloading ngrok: 58%\rDownloading ngrok: 59%\rDownloading ngrok: 60%\rDownloading ngrok: 61%\rDownloading ngrok: 62%\rDownloading ngrok: 63%\rDownloading ngrok: 64%\rDownloading ngrok: 65%\rDownloading ngrok: 66%\rDownloading ngrok: 67%\rDownloading ngrok: 68%\rDownloading ngrok: 69%\rDownloading ngrok: 70%\rDownloading ngrok: 71%\rDownloading ngrok: 72%\rDownloading ngrok: 73%\rDownloading ngrok: 74%\rDownloading ngrok: 75%\rDownloading ngrok: 76%\rDownloading ngrok: 77%\rDownloading ngrok: 78%\rDownloading ngrok: 79%\rDownloading ngrok: 80%\rDownloading ngrok: 81%\rDownloading ngrok: 82%\rDownloading ngrok: 83%\rDownloading ngrok: 84%\rDownloading ngrok: 85%\rDownloading ngrok: 86%\rDownloading ngrok: 87%\rDownloading ngrok: 88%\rDownloading ngrok: 89%\rDownloading ngrok: 90%\rDownloading ngrok: 91%\rDownloading ngrok: 92%\rDownloading ngrok: 93%\rDownloading ngrok: 94%\rDownloading ngrok: 95%\rDownloading ngrok: 96%\rDownloading ngrok: 97%\rDownloading ngrok: 98%\rDownloading ngrok: 99%\rDownloading ngrok: 100%\r                                                                                                    \rInstalling ngrok ... \r                                                                                                    \rAuthtoken saved to configuration file: /root/.ngrok2/ngrok.yml\n"]}],"source":["!ngrok config add-authtoken 2KkwwBb8LMpDEq5IpNsi7VF5O3T_4SJUvRLLnypoYNcsLxMYS"]},{"cell_type":"markdown","metadata":{"id":"wVbaASvyHlRV"},"source":["### Loading the model.\n","For now the process is:\n"," - Upload the model you want to use to the current environment\n"," - Run the function that recieves the raw data\n"," - Return the predictions"]},{"cell_type":"code","execution_count":4,"metadata":{"executionInfo":{"elapsed":3891,"status":"ok","timestamp":1693498264865,"user":{"displayName":"Francesco Bassino","userId":"12214195385968794219"},"user_tz":300},"id":"DI0qBZY1Hizn"},"outputs":[],"source":["from tensorflow.keras.models import load_model\n","from sklearn.preprocessing import MinMaxScaler\n","import numpy as np\n","from flask import Flask, request, jsonify\n"]},{"cell_type":"code","execution_count":5,"metadata":{"executionInfo":{"elapsed":4,"status":"ok","timestamp":1693498264866,"user":{"displayName":"Francesco Bassino","userId":"12214195385968794219"},"user_tz":300},"id":"VDRMTia7IJ2b"},"outputs":[],"source":["def is_float(num):\n","    try:\n","        float(num)\n","        return True\n","    except ValueError:\n","        return False"]},{"cell_type":"code","execution_count":6,"metadata":{"executionInfo":{"elapsed":4,"status":"ok","timestamp":1693498264866,"user":{"displayName":"Francesco Bassino","userId":"12214195385968794219"},"user_tz":300},"id":"UPNRM0z8IQdn"},"outputs":[],"source":["def preprocess_sequences(sequences):\n","    sequences = np.array(sequences)\n","    # Shape: (SeuenceSize, 51)\n","    scaler = MinMaxScaler()\n","    normalized_sequences = np.zeros_like(sequences)\n","    for i in range(sequences.shape[0]):\n","        for j in range(sequences.shape[1]):\n","            # Flatten the landmarks for each set within the sequence\n","            landmarks_flattened = np.reshape(sequences[i, j], (-1, 1))\n","            # Normalize tshe landmarks\n","            landmarks_normalized = scaler.fit_transform(landmarks_flattened)\n","            # Reshape the normalized landmarks back to the original shape\n","            normalized_landmarks = np.reshape(landmarks_normalized, sequences[i, j].shape)\n","            # Update the normalized landmarks in the sequences array\n","            normalized_sequences[i, j] = normalized_landmarks\n","    return normalized_sequences"]},{"cell_type":"code","execution_count":9,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"MD2AGSGZbXLH","executionInfo":{"status":"ok","timestamp":1693498543644,"user_tz":300,"elapsed":210078,"user":{"displayName":"Francesco Bassino","userId":"12214195385968794219"}},"outputId":"0d00ced4-d2c6-4954-ce0e-26155939942d"},"outputs":[{"output_type":"stream","name":"stdout","text":["Model: \"sequential_1\"\n","_________________________________________________________________\n"," Layer (type)                Output Shape              Param #   \n","=================================================================\n"," lstm_2 (LSTM)               (None, 10, 128)           91648     \n","                                                                 \n"," dropout_2 (Dropout)         (None, 10, 128)           0         \n","                                                                 \n"," lstm_3 (LSTM)               (None, 64)                49408     \n","                                                                 \n"," dropout_3 (Dropout)         (None, 64)                0         \n","                                                                 \n"," dense_1 (Dense)             (None, 1)                 65        \n","                                                                 \n","=================================================================\n","Total params: 141,121\n","Trainable params: 141,121\n","Non-trainable params: 0\n","_________________________________________________________________\n","None\n"," * Serving Flask app '__main__'\n"," * Debug mode: off\n"]},{"output_type":"stream","name":"stderr","text":["INFO:werkzeug:\u001b[31m\u001b[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\u001b[0m\n"," * Running on http://127.0.0.1:5000\n","INFO:werkzeug:\u001b[33mPress CTRL+C to quit\u001b[0m\n"]},{"output_type":"stream","name":"stdout","text":[" * Running on http://8017-35-245-244-77.ngrok-free.app\n"," * Traffic stats available on http://127.0.0.1:4040\n"]},{"output_type":"stream","name":"stderr","text":["INFO:werkzeug:127.0.0.1 - - [31/Aug/2023 16:13:07] \"\u001b[33mGET / HTTP/1.1\u001b[0m\" 404 -\n","INFO:werkzeug:127.0.0.1 - - [31/Aug/2023 16:13:07] \"\u001b[33mGET /favicon.ico HTTP/1.1\u001b[0m\" 404 -\n","INFO:werkzeug:127.0.0.1 - - [31/Aug/2023 16:13:39] \"\u001b[31m\u001b[1mGET /predict HTTP/1.1\u001b[0m\" 405 -\n"]}],"source":["from flask import Flask\n","from flask_ngrok import run_with_ngrok\n","\n","app = Flask(__name__)\n","run_with_ngrok(app)\n","\n","# Load the .h5 model\n","model_path = 'lstm_model_ex1.h5'\n","model = load_model(model_path)\n","print(model.summary())\n","@app.route('/predict', methods=['POST'])\n","def home():\n","    try:\n","        data = request.json\n","        received_array = np.array(data['array'])\n","        processed_array = preprocess_sequences(received_array)\n","        # Adding one dimension since is only one sequence\n","        single_sequence = np.expand_dims(processed_array, axis=0)\n","        # this should return (1, 1) shape\n","        sequence_prediction = model.predict(single_sequence)\n","        response = {'response_array': sequence_prediction.tolist()}\n","        print(sequence_prediction)\n","        return jsonify(response), 200\n","    except Exception as e:\n","        return jsonify({'error': str(e)}), 400\n","\n","app.run()"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"S9OqPXQ-etPh"},"outputs":[],"source":[]}],"metadata":{"colab":{"provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}