Skip to content

Commit 3dff527

Browse files
authored
Merge pull request #48 from slashml/model-deployment
deploy model example
2 parents a070479 + bf0d301 commit 3dff527

File tree

13 files changed

+213
-4
lines changed

13 files changed

+213
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
All notable changes to `slashml-python-client` aka `slashml` will be documented in this file.
33
This project adheres to [Semantic Versioning](https://semver.org/).
44

5+
## 0.1.4 - 2023-06-09
6+
7+
### Added
8+
- Model deployment
9+
510
## 0.1.3 - 2023-05-13
611

712
### Added

DEVELOPMENT.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Contributing to this library
2+
3+
4+
In order to contribute you need to install a few things
5+
6+
7+
1. First make sure you have python3 installed. You can check by running the following command in your terminal
8+
9+
```
10+
python3 -m pip install --upgrade pip
11+
```
12+
13+
2. Install the build packages
14+
```
15+
python3 -m pip install --upgrade build
16+
```
17+
18+
3. Then run the following command at the root of the project to build the package
19+
```
20+
SLASHML_DIR='.' python3 -m build
21+
```
22+
23+
The `SLASHML_DIR` will pick the root dir and build the package inside the dist folder in the root.
24+
25+
26+
4. Pushing the package to pypi
27+
28+
```
29+
python3 -m pip install --upgrade twine
30+
python3 -m pip install --upgrade twine
31+
python3 -m twine upload --repository testpypi dist/*
32+
```
33+
34+
Make sure dist only has the newly build package, otherwise the command will try to deploy all of the packages.
35+
36+
The above command will prompt you for a username and a password. You will need developer access to the pypi repo to be able to push the package. Contact @eff-kay for this.
37+
38+
39+
5. Test the package by
40+
41+
```
42+
python -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple slashml
43+
```
44+
45+
This command will pull the latest from test.pypi.org and the extra-index-url will pull the dependencies from pypi.org. This is because the slashml package depends on other packages that are not on test.pypi.org.
46+
47+
48+

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,56 @@ print(f"Summary = {response.summarization_data}")
6565
```
6666

6767

68+
#### Deploy your own Model
69+
<!-- write a code snippet in the minimum number of lines -->
70+
71+
Note: this examples requires the `transformers` and `torch` packages to be installed. You can install them with `pip install transformers torch`
72+
73+
```
74+
pip install transformers torch
75+
```
76+
77+
```python
78+
from slashml import ModelDeployment
79+
import time
80+
81+
# you might have to install transfomers and torch
82+
from transformers import pipeline
83+
84+
def train_model():
85+
# Bring in model from huggingface
86+
return pipeline('fill-mask', model='bert-base-uncased')
87+
88+
my_model = train_model()
89+
90+
# Replace `API_KEY` with your SlasML API token.
91+
API_KEY = "YOUR_API_KEY"
92+
93+
model = ModelDeployment(api_key=API_KEY)
94+
95+
# deploy model
96+
response = model.deploy(model_name='my_model_3', model=my_model)
97+
98+
# wait for it to be deployed
99+
time.sleep(2)
100+
status = model.status(model_version_id=response.id)
101+
102+
while status.status != 'READY':
103+
print(f'status: {status.status}')
104+
print('trying again in 5 seconds')
105+
time.sleep(5)
106+
status = model.status(model_version_id=response.id)
107+
108+
if status.status == 'FAILED':
109+
raise Exception('Model deployment failed')
110+
111+
# submit prediction
112+
input_text = 'Steve jobs is the [MASK] of Apple.'
113+
prediction = model.predict(model_version_id=response.id, model_input=input_text)
114+
print(prediction)
115+
```
116+
117+
68118
### View the list of service providers available
69119
```python
70120
from slashml import TextToSpeech
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from slashml import ModelDeployment
2+
import time
3+
4+
# you might have to install transfomers and torch
5+
from transformers import pipeline
6+
7+
def train_model():
8+
# Bring in model from huggingface
9+
return pipeline('fill-mask', model='bert-base-uncased')
10+
11+
my_model = train_model()
12+
13+
# Replace `API_KEY` with your SlasML API token.
14+
model = ModelDeployment(api_key=None)
15+
16+
# deploy model
17+
response = model.deploy(model_name='my_model_3', model=my_model)
18+
19+
# wait for it to be deployed
20+
time.sleep(2)
21+
status = model.status(model_version_id=response.id)
22+
23+
while status.status != 'READY':
24+
print(f'status: {status.status}')
25+
print('trying again in 5 seconds')
26+
time.sleep(5)
27+
status = model.status(model_version_id=response.id)
28+
29+
if status.status == 'FAILED':
30+
raise Exception('Model deployment failed')
31+
32+
# submit prediction
33+
input_text = 'Steve jobs is the [MASK] of Apple.'
34+
prediction = model.predict(model_version_id=response.id, model_input=input_text)
35+
print(prediction)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
transformers==4.30.0
2+
torch==2.0.1
3+
slashml==0.1.4

examples/speech_to_text_sync.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Replace `API_KEY` with your SlasML API token. This example still runs without
44
# the API token but usage will be limited
55
API_KEY = "YOUR_API_KEY"
6-
service_provider = SpeechToText.ServiceProvider.REV
6+
7+
service_provider = SpeechToText.ServiceProvider.WHISPER
78
audio_filepath = "test.mp3"
89

910
# Find all the service providers that we support by running the choices() method

examples/summarize_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Replace `API_KEY` with your SlasML API token. This example still runs without
55
# the API token but usage will be limited
6-
API_KEY = "YOUR_API_KEY"
6+
API_KEY = None
77
service_provider = TextSummarization.ServiceProvider.OPENAI
88

99
input_text = """A good writer doesn't just think, and then write down what he thought, as a sort of transcript. A good writer will almost always discover new things in the process of writing. And there is, as far as I know, no substitute for this kind of discovery. Talking about your ideas with other people is a good way to develop them. But even after doing this, you'll find you still discover new things when you sit down to write. There is a kind of thinking that can only be done by writing."""

examples/text_to_speech_sync.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Replace `API_KEY` with your SlasML API token. This example still runs without
44
# the API token but usage will be limited
55
API_KEY = "YOUR_API_KEY"
6+
67
service_provider = TextToSpeech.ServiceProvider.AWS
78
input_text = "To be or not to be, that is the question!"
89

@@ -15,4 +16,4 @@
1516
# Submit request
1617
job = model.execute(text=input_text, service_provider=service_provider)
1718

18-
print (f"\n\n\n You can access the audio file here: {job.audio_url}")
19+
print(f"\n\n\n You can access the audio file here: {job.audio_url}")

requires-build.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build==0.10.0
2+
twine==4.0.2
3+

requires-install.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
requests==2.28.1
22
addict==2.4.0
3+
truss==0.4.8

0 commit comments

Comments
 (0)