Skip to content

Commit 06d2fb3

Browse files
authored
Merge pull request #27 from slashml/azeem-library-refactor
refactor library and examples
2 parents 31222ed + e6a3917 commit 06d2fb3

File tree

12 files changed

+265
-356
lines changed

12 files changed

+265
-356
lines changed

examples/speech_summarized.py

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,69 @@
1-
2-
from slashml import SpeechToText, Summarization
1+
from slashml import SpeechToText, TextSummarization
32
import time
43

5-
# Initialize your model
6-
model = SpeechToText()
74

8-
service_provider = model.ServiceProvider.AWS
5+
def speech_to_text(uploaded_audio_url, service_provider, api_key):
6+
# Initialize model
7+
model = SpeechToText(api_key=api_key)
8+
9+
# Submit transcription request
10+
job = model.transcribe(uploaded_audio_url, service_provider=service_provider)
11+
12+
assert job.status != "ERROR", f"{job}"
13+
print(f"Job ID: {job.id}")
14+
15+
# check job status
16+
response = model.status(job.id, service_provider=service_provider)
17+
18+
while response.status == "IN_PROGRESS":
19+
time.sleep(30)
20+
response = model.status(job.id, service_provider=service_provider)
21+
print(f"Response = {response}. Retrying in 30 seconds")
22+
23+
return response
24+
925

10-
# long audio file already uploaded
11-
upload_url = 'https://slashml.s3.ca-central-1.amazonaws.com/fda70f6a-6057-4541-adf1-2cf4f4182929'
26+
def summarize(text, service_provider, api_key):
27+
# Initialize model
28+
model = TextSummarization(api_key=api_key)
1229

13-
# # choose a service provider and transcribe
14-
print('transcribing file')
15-
job = model.transcribe(
16-
upload_url,
17-
service_provider=service_provider
18-
)
30+
# Submit request
31+
job = model.summarize(text=text, service_provider=service_provider)
1932

20-
assert job.status != 'ERROR', f'{job}'
33+
assert job.status != "ERROR", f"{job}"
34+
print(f"Job ID: {job.id}")
2135

22-
print('transcription job id', job.id)
36+
# Check job status
37+
response = model.status(job.id, service_provider=service_provider)
2338

24-
# check the status
25-
response = model.status(job.id, service_provider=service_provider)
39+
# Keep checking job status until the task is complete
40+
while response.status == "PENDING":
41+
print(f"Response = {response}. Retrying in 30 seconds")
42+
time.sleep(30)
43+
response = model.status(job.id, service_provider=service_provider)
2644

27-
while response.status != 'COMPLETED':
28-
print(f'response got', response , 'trying again')
29-
time.sleep(30)
30-
response = model.status(job.id, service_provider = service_provider)
45+
return response
3146

32-
text_to_summarize = response.transcription_data.transcription
3347

34-
print('summarizing text', text_to_summarize)
48+
# Replace `API_KEY` with your SlasML API token. This example still runs without
49+
# the API token but usage will be limited
50+
API_KEY = "b503d137d229fdd4085f59e9ea06ac95d2706182"
3551

36-
model:Summarization = Summarization()
37-
service_provider = model.ServiceProvider.OPENAI
52+
service_provider_speech_to_text = SpeechToText.ServiceProvider.AWS
53+
service_provider_summarize = TextSummarization.ServiceProvider.OPENAI
3854

39-
print('summarizing text')
40-
# choose a service provider and transcribe
41-
job = model.summarize(
42-
text=text_to_summarize,
43-
service_provider=service_provider
44-
)
55+
# 10 minute audio file already uploaded
56+
uploaded_url = (
57+
"https://slashml.s3.ca-central-1.amazonaws.com/fda70f6a-6057-4541-adf1-2cf4f4182929"
58+
)
4559

46-
assert job.status != 'ERROR', f'{job}'
4760

48-
print('summarization job id', job.id)
61+
response_speech_to_text = speech_to_text(uploaded_url, service_provider_speech_to_text, API_KEY)
4962

50-
# check the status
51-
response = model.status(job.id, service_provider=service_provider)
63+
transcribed_text = response_speech_to_text.transcription_data.transcription
64+
print (f"Transcribed Text = {transcribed_text}")
5265

53-
while response.status != 'COMPLETED':
54-
print(f'response got', response , 'trying again')
55-
time.sleep(30)
56-
response = model.status(job.id, service_provider = service_provider)
66+
response_summarize = summarize(transcribed_text, service_provider_summarize, API_KEY)
67+
summary = response_summarize.summarization_data
5768

58-
print(response.summarization_data)
69+
print (f"Summarized Text = {summary}")

examples/speech_to_text.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
1-
from slashml.slashml import SpeechToText
1+
from slashml import SpeechToText
22
import time
33

4-
# Initialize your model
5-
model = SpeechToText()
6-
# model = SpeechToText()
74

8-
service_provider = model.ServiceProvider.ASSEMBLY
5+
def speech_to_text(audio_filepath, service_provider, api_key):
6+
# Initialize model
7+
model = SpeechToText(api_key=api_key)
98

10-
uploaded_file = model.upload_audio("test.mp3")
11-
print(uploaded_file)
9+
# Upload audio
10+
uploaded_file = model.upload_audio(audio_filepath)
11+
print(f"file uploaded: {uploaded_file}")
1212

13-
# # choose a service provider and transcribe
14-
job = model.transcribe(
15-
uploaded_file['upload_url'],
16-
service_provider=service_provider
13+
# Submit transcription request
14+
job = model.transcribe(
15+
uploaded_file["upload_url"], service_provider=service_provider
1716
)
1817

19-
assert job.status != 'ERROR', f'{job}'
20-
print('transcription job id', job.id)
18+
assert job.status != "ERROR", f"{job}"
19+
print(f"Job ID: {job.id}")
2120

22-
# check the status
23-
response = model.status(job.id, service_provider=service_provider)
21+
# check job status
22+
response = model.status(job.id, service_provider=service_provider)
2423

25-
while response.status != 'COMPLETED':
26-
time.sleep(30)
27-
response = model.status(job.id, service_provider = service_provider)
28-
print(f'response got', response , 'trying agagin')
24+
while response.status == "IN_PROGRESS":
25+
print(f"Response = {response}. Retrying in 30 seconds")
26+
time.sleep(30)
27+
response = model.status(job.id, service_provider=service_provider)
2928

30-
print(response.transcription_data.transcription)
29+
return response
30+
31+
32+
# Replace `API_KEY` with your SlasML API token. This example still runs without
33+
# the API token but usage will be limited
34+
API_KEY = None
35+
service_provider = SpeechToText.ServiceProvider.ASSEMBLY
36+
audio_filepath = "test.mp3"
37+
38+
# Find all the service providers that we support by running the choices() method
39+
print(f"Available providers: {SpeechToText.ServiceProvider.choices()}")
40+
print(f"Selected provider: {service_provider}")
41+
42+
43+
response = speech_to_text(
44+
audio_filepath=audio_filepath, service_provider=service_provider, api_key=API_KEY
45+
)
46+
print(f"{response}\n\nTranscription = {response.transcription_data.transcription}")

examples/summarize.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1-
2-
from slashml import Summarization
1+
from slashml import TextSummarization
32
import time
43

5-
# Initialize your model
6-
model = Summarization()
7-
# model = Summarization()
8-
service_provider = model.ServiceProvider.OPENAI
4+
def summarize(text, service_provider, api_key):
5+
# Initialize model
6+
model = TextSummarization(api_key=api_key)
7+
8+
# Submit request
9+
job = model.summarize(text=text, service_provider=service_provider)
10+
11+
assert job.status != "ERROR", f"{job}"
12+
print(f"Job ID: {job.id}")
913

10-
in_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.'''
14+
# Check job status
15+
response = model.status(job.id, service_provider=service_provider)
1116

12-
# # choose a service provider and transcribe
13-
job = model.summarize(
14-
text=in_text,
15-
service_provider=service_provider
16-
)
17+
# Keep checking job status until the task is complete
18+
while response.status == "PENDING":
19+
print(f"Response = {response}. Retrying in 30 seconds")
20+
time.sleep(30)
21+
response = model.status(job.id, service_provider=service_provider)
1722

18-
assert job.status != 'ERROR', f'{job}'
23+
return response
1924

20-
print('transcription job id', job.id)
2125

22-
# check the status
23-
response = model.status(job.id, service_provider = service_provider)
26+
# Replace `API_KEY` with your SlasML API token. This example still runs without
27+
# the API token but usage will be limited
28+
API_KEY = "b503d137d229fdd4085f59e9ea06ac95d2706182"
29+
service_provider = TextSummarization.ServiceProvider.OPENAI
30+
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."""
2431

25-
while response.status != 'COMPLETED':
26-
time.sleep(30)
27-
response = model.status(job['id'], service_provider = service_provider)
28-
print(f'response got', response , 'trying again')
32+
# Find all the service providers that we support by running the choices() method
33+
print(f"Available providers: {TextSummarization.ServiceProvider.choices()}")
34+
print(f"Selected provider: {service_provider}")
2935

30-
print(response)
36+
response = summarize(text=input_text, service_provider=service_provider, api_key=API_KEY)
37+
print(f"{response}\n\nSummary = {response.summarization_data}")

examples/text_to_speech.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
21
from slashml import TextToSpeech
32

4-
# Initialize your model
5-
model = TextToSpeech()
6-
# model = SpeechToText()
3+
# Replace `API_KEY` with your SlasML API token. This example still runs without
4+
# the API token but usage will be limited
5+
API_KEY = None
6+
service_provider = TextToSpeech.ServiceProvider.AWS
7+
input_text = "To be or not to be, that is the question!"
8+
9+
# Find all the service providers that we support by running the choices() method
10+
print(f"Available providers: {TextToSpeech.ServiceProvider.choices()}")
11+
print(f"Selected provider: {service_provider}")
712

8-
service_provider = model.ServiceProvider.AWS
13+
model = TextToSpeech(api_key=API_KEY)
914

10-
# # choose a service provider and transcribe
11-
job = model.speechify(
12-
text ='this is my text',
13-
service_provider=service_provider
14-
)
15+
# Submit request
16+
job = model.speechify(text=input_text, service_provider=service_provider)
17+
print(job)
1518

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

requires-install.txt

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

slashml/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
from .slashml import SpeechToText, Summarization # noqa: F401,E402
1+
from .text_summarization import TextSummarization # noqa: F401,E402
2+
from .speech_to_text import SpeechToText # noqa: F401,E402
23
from .text_to_speech import TextToSpeech # noqa: F401,E402
4+

slashml/json_to_dot.py

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

0 commit comments

Comments
 (0)