Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit af4e768

Browse files
committed
new version of wrapper
1 parent 6016b33 commit af4e768

31 files changed

+1508
-779
lines changed

README.rst

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,71 @@
11
mljar-api-python
22
================
33

4-
This is a python wrapper over MLJAR API.
4+
A simple python wrapper over mljar API. It allows MLJAR users to create
5+
Machine Learning models with few lines of code:
6+
7+
.. code:: python
8+
9+
import mljar
10+
11+
model = Mljar(project='My awesome project', experiment='First experiment')
12+
model.fit(X,y)
13+
14+
model.predict(X)
15+
16+
That's all folks! Yeah, I know, this makes Machine Learning super easy!
17+
You can use this code for following Machine Learning tasks: \* Binary
18+
classification (your target has only two unique values) \* Regression
19+
(your target value is continuous) \* More is coming soon!
20+
21+
How to install
22+
--------------
23+
24+
You can install mljar with **pip**:
25+
26+
::
27+
28+
pip install -U mljar
29+
30+
or from source code:
31+
32+
::
33+
34+
python setup.py install
35+
36+
How to use it
37+
-------------
38+
39+
1. Create an account at mljar.com and login.
40+
2. Please go to your users settings (top, right corner).
41+
3. Get your token, for example 'exampleexampleexample'.
42+
4. Set environment variable ``MLJAR_TOKEN`` with your token value:
43+
44+
::
45+
46+
export MLJAR_TOKEN=exampleexampleexample
47+
48+
5. That's all, you are ready to use MLJAR in your python code!
49+
50+
What's going on?
51+
----------------
52+
53+
- This wrapper allows you to search through different Machine Learning
54+
algorithms and tune each of the algorithm.
55+
- By searching and tuning ML algorithm to your data you will get very
56+
accurate model.
57+
- By calling method ``fit`` from ``Mljar class`` you create new project
58+
and start experiment with models training. All your results will be
59+
accessible from your mljar.com account - this makes Machine Learning
60+
super easy and keeps all your models and results in beautiful order.
61+
So, you will never miss anything.
62+
- All computations are done in MLJAR Cloud, they are executed in
63+
parallel. So after callgin ``fit`` method you can switch your
64+
computer off and MLJAR will do the job for you!
65+
- I think this is really amazing! What do you think? Please let us know
66+
at ``contact@mljar.com``.
67+
68+
Examples
69+
--------
70+
71+
Coming soon!

mljar/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
API_VERSION = 'v1'
3+
MLJAR_ENDPOINT = 'http://0.0.0.0:3000/api'
4+
5+
from mljar import Mljar

mljar/client.py

Lines changed: 0 additions & 111 deletions
This file was deleted.

mljar/client/__init__.py

Whitespace-only changes.

mljar/client/base.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import os
2+
import json, requests
3+
4+
from .. import API_VERSION, MLJAR_ENDPOINT
5+
from ..exceptions import MljarException, TokenException, DataReadException, BadRequestException
6+
from ..exceptions import JSONReadException, NotFoundException, AuthenticationException
7+
8+
9+
from ..log import logger
10+
11+
class MljarHttpClient(object):
12+
'''
13+
Mljar Client for HTTP Requests.
14+
'''
15+
16+
def __init__(self):
17+
self.TOKEN = os.environ.get('MLJAR_TOKEN', None)
18+
if not self.TOKEN:
19+
raise TokenException('Please define environment variable MLJAR_TOKEN. \
20+
You can get you MLJAR token by login to mljar.com account. \
21+
It is available in your settings.')
22+
23+
self.base_url = '/'.join([MLJAR_ENDPOINT, API_VERSION])
24+
25+
def request(self, method, url, data=None, with_header=True, url_outside_mljar=False, parse_json=True):
26+
"""
27+
Execute the request using requests library.
28+
"""
29+
if url_outside_mljar:
30+
request_url = url
31+
else:
32+
request_url = self.base_url + url
33+
logger.debug("Starting request to url: {} with data: {}".format(request_url, data))
34+
35+
headers = {'Authorization': 'Token '+self.TOKEN }
36+
if with_header:
37+
response = requests.request(method, request_url, headers=headers, data=data)
38+
else:
39+
response = requests.request(method, request_url, data=data)
40+
41+
if parse_json:
42+
try:
43+
if response.status_code != 204:
44+
logger.debug("Response content: {}, headers: {}".format(response.json(), response.headers))
45+
except Exception as e:
46+
logger.error("Request failed: {} {}".format(response.content, str(e)))
47+
self._check_response_status(response)
48+
return response
49+
50+
def _check_response_status(self, response):
51+
"""
52+
Check if response is successful else raise Exception.
53+
"""
54+
if not (200 <= response.status_code < 300):
55+
try:
56+
message = response.json()["errors"]
57+
except Exception:
58+
message = None
59+
logger.debug("Error received : status_code: {}, message: {}".format(response.status_code,
60+
message or response.content))
61+
62+
if response.status_code == 401:
63+
raise AuthenticationException()
64+
elif response.status_code == 404:
65+
raise NotFoundException()
66+
elif response.status_code == 400:
67+
raise BadRequestException()
68+
else:
69+
response.raise_for_status()
70+
71+
72+
'''
73+
def _get_data(self, response):
74+
if response is None:
75+
return None
76+
77+
try:
78+
data = response.json()
79+
except ValueError as e:
80+
raise JSONReadException('Get data failed, %s' % str(e) )
81+
82+
if not response.ok:
83+
msg = [data[m] for m in ("id", "message") if m in data][1]
84+
raise DataReadException(msg)
85+
86+
return data
87+
'''
88+
89+
'''
90+
91+
self._urls = {
92+
'project': '/'.join([self.API_ENDPOINT, API_VERSION, 'projects']),
93+
'dataset': '/'.join([self.API_ENDPOINT, API_VERSION, 'datasets']),
94+
'experiment': '/'.join([self.API_ENDPOINT, API_VERSION, 'experiments']),
95+
'result': '/'.join([self.API_ENDPOINT, API_VERSION, 'results/']),
96+
'predict': '/'.join([self.API_ENDPOINT, API_VERSION, 'predict/']),
97+
'predictions': '/'.join([self.API_ENDPOINT, API_VERSION, 'predictions']), # it is not a bug, we don't need here '/'
98+
'download_prediction': '/'.join([self.API_ENDPOINT, API_VERSION, 'download/prediction/']),
99+
's3policy': '/'.join([self.API_ENDPOINT, API_VERSION, 's3policy/']),
100+
'accept_column_usage': '/'.join([self.API_ENDPOINT, API_VERSION, 'accept_column_usage/']),
101+
}
102+
103+
104+
def _make_request(self, url_name = '', custom_url = '', request_type = 'get', url_additional = '', input_json = {}, with_header = True):
105+
try:
106+
response = None
107+
headers = {'Authorization': 'Token '+self.TOKEN } #'Content-Type': 'application/json'
108+
my_url = ''
109+
if url_name in self._urls:
110+
if my_url == '':
111+
raise Exception('Wrong URL address')
112+
if url_additional != '':
113+
my_url += url_additional
114+
115+
#print 'request', my_url, request_type, input_json, with_header, headers
116+
117+
if request_type == 'get':
118+
response = requests.get(my_url, headers=headers)
119+
elif request_type == 'post':
120+
if with_header:
121+
response = requests.post(my_url, data=input_json, headers=headers)
122+
else:
123+
response = requests.post(my_url, data=input_json)
124+
elif request_type == 'put':
125+
if with_header:
126+
print my_url, 'with header', input_json
127+
response = requests.put(my_url, data=input_json, headers=headers)
128+
else:
129+
response = requests.put(my_url, data=input_json)
130+
131+
except MljarException as e:
132+
print 'There was an error during API call, %s' % str(e)
133+
finally:
134+
return response
135+
'''

0 commit comments

Comments
 (0)