GithubHelp home page GithubHelp logo

thoughtscript / python_api Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 294 KB

Some fun with Flask, Python, and Machine Learning

Python 75.86% Shell 1.38% TypeScript 16.81% HTML 0.90% CSS 5.05%
docker machine-learning mysql numpy angular flask docker-compose

python_api's Introduction

python_api

Run the following from the root dir:

docker-compose up

That should spin up each subservice. (NOTE: the backend service will continually restart until the mysql service has fully initialized.) Otherwise you can launch each service individually by:

  1. Commenting out everything in docker-compose.yml except for:

    services:
      mysql:
       image: 'bitnami/mysql:latest'
      ports:
        - '3306:3306'
      environment:
        - ALLOW_EMPTY_PASSWORD=yes
        - MYSQL_USER=example
        - MYSQL_PASSWORD=example
        - MYSQL_DATABASE=example
        - MYSQL_AUTHENTICATION_PLUGIN=mysql_native_password

    Then launching the MySQL subservice via docker-compose-up.

  2. From within /angular run npm i && npm i angular/cli -g, then ng serve -o to spin up the Angular frontend.

  3. From within /backend run:

    cd ml 
    python3 -m pip install --upgrade pip
    python3 -m pip install -r requirements.txt
    python3 ml-conjunction.py
    python3 ml-disjunction.py 
    python3 ml-implication.py
    python3 ml-negation.py
    python3 ml-nand.py

    To generate the Machine Learning models.

  4. Lastly, to start the backend service - from within /backend run:

    cd backend 
    python3 -m pip install --upgrade pip
    python3 -m pip install -r requirements.txt
    python3 main.py

Generally speaking, the necessary config, settings, and commands are available within docker-compose.yml, backend dockerfile, frontend dockerfile, angular.json, and main.py, respectively. So, please make recourse to those if you encounter problems.

Angular Frontend

Will be exposed on:

  • Docker: http://localhost:4201
  • Standalone: http://localhost:4200

API Endpoints

API endpoints are exposed through Docker or by running each subservice individually.

Test API

A test endpoint to verify that requests are getting through and the app is exposed correctly through Docker:

  • Docker http://localhost:5001/api/hello
  • Standalone http://localhost:5000/api/hello

Logic API

Generates Boolean, bivalent, classical, logic results from Deep Learning Linear Regression models (it doesn't use in-built programmatic, native, Boolean clauses or Automated Theorem Proving techniques).

Also, inferences are included that exceed the standard Boolean operations (Material Conditional/Implication, Conjunction, Disjunction, Negation - for example: NAND).

This mirrors research into how one might try to teach a computer logic rather than just stipulate how logic works like we do within programming languages. Read more here.

  1. Negation

    • Supply sequences of 0,0 (False) or 1,1 (True)
    • Will reject any invalid pair (e.g. - 1,0, 0,1)
    • Each result will contain two parts with each entry in the first mapped to the result in the second by same index:
      • 1,1 at index i will map to false at index i.
    • Docker: POST http://localhost:5001/api/logic/negation?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1
    • Standalone: POST http://localhost:5000/api/logic/negation?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1

    Response:

    [
        [
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                1
            ],
            [
                0,
                0
            ]
        ],
        [
            [
                false,
                true,
                false,
                true
            ]
        ]
    ]
  2. Conjunction

    • Supply sequences of 0,1 (left conjunct False, right conjunct True), 1,1 (left conjunct True, right conjunct True), 1,0 (left conjunct True, right conjunct False), 0,0 (left conjunct False, right conjunct False).
    • Each result will contain two parts with each entry in the first mapped to the result in the second by same index:
      • 1,1 at index i will map to true at index i.
    • Docker POST http://localhost:5001/api/logic/conjunction?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1
    • Standalone POST http://localhost:5000/api/logic/conjunction?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1

    Response:

    [
        [
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                0
            ],
            [
                0,
                1
            ],
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                0
            ],  
            [
                0,
                1
            ]
        ],
        [
            [
                true,
                false,
                false,
                false,
                true,
                false,
                false,
                false
            ]
        ]
    ]
  3. Implication

    • Supply sequences of 0,1 (antecedant False, consequent True), 1,1 (antecedant True, consequent True), 1,0 (antecedant True, consequent False), 0,0 (antecedant False, consequent False).
    • Each result will contain two parts with each entry in the first mapped to the result in the second by same index:
      • 1,0 at index i will map to false at index i.
    • Docker POST http://localhost:5001/api/logic/implication?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1
    • Standalone POST http://localhost:5000/api/logic/implication?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1

    Response:

    [
        [
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                0
            ],
            [
                0,
                1
            ],
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                0
            ],
            [
                0,
                1
            ]
        ],
        [
            [
                true,
                true,
                false,
                true,
                true,
                true,
                false,
                true
            ]
        ]
    ]
  4. Disjunction

    • Supply sequences of 0,1 (left conjunct False, right conjunct True), 1,1 (left conjunct True, right conjunct True), 1,0 (left conjunct True, right conjunct False), 0,0 (left conjunct False, right conjunct False).
    • Each result will contain two parts with each entry in the first mapped to the result in the second by same index:
      • 1,0 at index i will map to true at index i.
    • Docker POST http://localhost:5001/api/logic/disjunction?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1
    • Standalone POST http://localhost:5000/api/logic/disjunction?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1

    Response:

    [
        [
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                0
            ],
            [
                0,
                1
            ],
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                0
            ],
            [
                0,
                1
            ]
        ],
        [
            [
                true,
                false,
                true,
                true,
                true,
                false,
                true,
                true
            ]
        ]
    ]
  5. NAND

    • Supply sequences of 0,1 (left conjunct False, right conjunct True), 1,1 (left conjunct True, right conjunct True), 1,0 (left conjunct True, right conjunct False), 0,0 (left conjunct False, right conjunct False).
    • Each result will contain two parts with each entry in the first mapped to the result in the second by same index:
      • 1,0 at index i will map to true at index i.
    • Docker POST http://localhost:5001/api/logic/nand?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1
    • Standalone POST http://localhost:5000/api/logic/nand?test=1,1|0,0|1,0|0,1|1,1|0,0|1,0|0,1

    Response:

    [
        [
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                0
            ],
            [
                0,
                1
            ],
            [
                1,
                1
            ],
            [
                0,
                0
            ],
            [
                1,
                0
            ],
            [
                0,
                1
            ]
        ],
        [
            [
                false,
                true,
                true,
                true,
                false,
                true,
                true,
                true
            ]
        ]
    ]

DB API

Supports RESTful CRUD (CREATE, READ, UPDATE, DELETE) operations against a backing MySQL DB.

Check out the Example SQL Domain entry.

  1. Scan

    • Retrieve all prepopulated entries from the MySQL DB.
    • Docker GET http://localhost:5001/api/db/examples
    • Standalone GET http://localhost:5000/api/db/examples

    Response:

    ["{ id: 1, name: 'example_a' }","{ id: 2, name: 'example_b' }","{ id: 3, name: 'example_c' }","{ id: 4, name: 'example_d' }"]
  2. Get One

    • Retrieve one Example by ID.
    • Docker GET http://localhost:5001/api/db/example/1
    • Standalone GET http://localhost:5000/api/db/example/1

    Response:

    ["{ id: 1, name: 'example_a' }"]
  3. Create Example

    • Create an Example with form-data.
    • Docker POST http://localhost:5001/api/db/example
    • Standalone POST http://localhost:5000/api/db/example

    Request Body form-data:

    name created
    

    Response:

    [
        "{ id: 5, name: 'created' }"
    ]
  4. Update Example

    • Update an Example by ID
      • Docker PUT http://localhost:5001/api/db/example/1
      • Standalone PUT http://localhost:5000/api/db/example/1

    Request Body form-data:

    name updated    
    

    Response:

    [
        "{ id: 1, name: 'updated' }"
    ]
  5. Delete Example

    • Delete an Example by ID
      • Docker DELETE http://localhost:5001/api/db/example/1
      • Standalone DELETE http://localhost:5000/api/db/example/1

    Request Body form-data:

    name updated
    

    Response:

    [
        "{ id: 1, name: 'updated' }"
    ]

Resources and Links

  1. https://towardsdatascience.com/how-to-insert-dummy-data-into-databases-using-flask-sqlalchemy-9c59efab4527

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.