A simple interface that creates a very basic http server in python to connect between your python project like an Machine Learning model and JS clients or servers for demo purposes, DON'T USE IN PRODUCTION
- Put the 3 files any where in your project.
- Open
use.py
file - You will find a
TestModel
class, feel free to edit or remove it, It's just an example how you can use the script. - Write your own class and extend it from
BaseMLModel
and overriderun()
function with your logic like calling your model function
# your function
def myfunc(name):
return "Hello " + name
# your class
class TestModel(BaseMLModel):
# **kwargs contains all parameters you have sent from your client.
def run(self, **kwargs):
return myfunc(name=kwargs['name']) # call your function
- Just run
python server.py
and you will seeServer started http://localhost:8080
and you can stop it by interrupting the command. - You are done now.
- You can call the host main endpoint and it supports only 1 POST request with.
model_name
string parameter which contains the exact name of your classdata
in a JSON form.- Or just use the client function in the
MLInterface.js
file. in HTML
<script src="MLInterface.js"></script>
<script>
runModel('TestModel', {
name: "Mostafa"
}).then((result) => {
console.log(result)
})
</script>
- You can change your default port if you have a conflict from
server.py
file.
# ...
# basic server
HOSTNAME = "localhost"
PORT = 8080
# ...