Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add common usage pattern to SDK #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 12 additions & 50 deletions examples/speech_summarized.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,4 @@
from slashml import SpeechToText, TextSummarization
import time


def speech_to_text(uploaded_audio_url, service_provider, api_key):
# Initialize model
model = SpeechToText(api_key=api_key)

# Submit transcription request
job = model.transcribe(uploaded_audio_url, service_provider=service_provider)

assert job.status != "ERROR", f"{job}"
print(f"Job ID: {job.id}")

# check job status
response = model.status(job.id, service_provider=service_provider)

while response.status == "IN_PROGRESS":
time.sleep(30)
response = model.status(job.id, service_provider=service_provider)
print(f"Response = {response}. Retrying in 30 seconds")

return response


def summarize(text, service_provider, api_key):
# Initialize model
model = TextSummarization(api_key=api_key)

# Submit request
job = model.summarize(text=text, service_provider=service_provider)

assert job.status != "ERROR", f"{job}"
print(f"Job ID: {job.id}")

# Check job status
response = model.status(job.id, service_provider=service_provider)

# Keep checking job status until the task is complete
while response.status == "PENDING":
print(f"Response = {response}. Retrying in 30 seconds")
time.sleep(30)
response = model.status(job.id, service_provider=service_provider)

return response
from slashml import SpeechToText, TextSummarization, services


# Replace `API_KEY` with your SlasML API token. This example still runs without
Expand All @@ -57,13 +13,19 @@ def summarize(text, service_provider, api_key):
"https://slashml.s3.ca-central-1.amazonaws.com/fda70f6a-6057-4541-adf1-2cf4f4182929"
)

# Convert audio to text
response_speech_to_text = services.speech_to_text(
uploaded_url, service_provider_speech_to_text, API_KEY
)

response_speech_to_text = speech_to_text(uploaded_url, service_provider_speech_to_text, API_KEY)

# Extract text from API response
transcribed_text = response_speech_to_text.transcription_data.transcription
print (f"Transcribed Text = {transcribed_text}")
print(f"Transcribed Text = {transcribed_text}")

response_summarize = summarize(transcribed_text, service_provider_summarize, API_KEY)
# Summarize text
response_summarize = services.summarize_text(
transcribed_text, service_provider_summarize, API_KEY
)
summary = response_summarize.summarization_data

print (f"Summarized Text = {summary}")
print(f"Summarized Text = {summary}")
34 changes: 2 additions & 32 deletions examples/speech_to_text.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
from slashml import SpeechToText
import time


def speech_to_text(audio_filepath, service_provider, api_key):
# Initialize model
model = SpeechToText(api_key=api_key)

# Upload audio
uploaded_file = model.upload_audio(audio_filepath)
print(f"file uploaded: {uploaded_file}")

# Submit transcription request
job = model.transcribe(
uploaded_file["upload_url"], service_provider=service_provider
)

assert job.status != "ERROR", f"{job}"
print(f"Job ID: {job.id}")

# check job status
response = model.status(job.id, service_provider=service_provider)

while response.status == "IN_PROGRESS":
print(f"Response = {response}. Retrying in 30 seconds")
time.sleep(30)
response = model.status(job.id, service_provider=service_provider)

return response

from slashml import SpeechToText, services

# Replace `API_KEY` with your SlasML API token. This example still runs without
# the API token but usage will be limited
Expand All @@ -39,8 +10,7 @@ def speech_to_text(audio_filepath, service_provider, api_key):
print(f"Available providers: {SpeechToText.ServiceProvider.choices()}")
print(f"Selected provider: {service_provider}")


response = speech_to_text(
response = services.speech_to_text(
audio_filepath=audio_filepath, service_provider=service_provider, api_key=API_KEY
)
print(f"{response}\n\nTranscription = {response.transcription_data.transcription}")
30 changes: 4 additions & 26 deletions examples/summarize.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
from slashml import TextSummarization
import time

def summarize(text, service_provider, api_key):
# Initialize model
model = TextSummarization(api_key=api_key)

# Submit request
job = model.summarize(text=text, service_provider=service_provider)

assert job.status != "ERROR", f"{job}"
print(f"Job ID: {job.id}")

# Check job status
response = model.status(job.id, service_provider=service_provider)

# Keep checking job status until the task is complete
while response.status == "PENDING":
print(f"Response = {response}. Retrying in 30 seconds")
time.sleep(30)
response = model.status(job.id, service_provider=service_provider)

return response

from slashml import TextSummarization, services

# Replace `API_KEY` with your SlasML API token. This example still runs without
# the API token but usage will be limited
API_KEY = "b503d137d229fdd4085f59e9ea06ac95d2706182"
API_KEY = None
service_provider = TextSummarization.ServiceProvider.OPENAI
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."""

# Find all the service providers that we support by running the choices() method
print(f"Available providers: {TextSummarization.ServiceProvider.choices()}")
print(f"Selected provider: {service_provider}")

response = summarize(text=input_text, service_provider=service_provider, api_key=API_KEY)
# Summarize text
response = services.summarize_text(text=input_text, service_provider=service_provider, api_key=API_KEY)
print(f"{response}\n\nSummary = {response.summarization_data}")
51 changes: 51 additions & 0 deletions slashml/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from slashml import TextSummarization, SpeechToText
import time


def summarize_text(text, service_provider, api_key):
# Initialize model
model = TextSummarization(api_key=api_key)

# Submit request
job = model.summarize(text=text, service_provider=service_provider)

assert job.status != "ERROR", f"{job}"
print(f"Job ID: {job.id}")

# Check job status
response = model.status(job.id, service_provider=service_provider)

# Keep checking job status until the task is complete
while response.status == "IN_PROGRESS":
print(f"Response = {response}. Retrying in 30 seconds")
time.sleep(30)
response = model.status(job.id, service_provider=service_provider)

return response


def speech_to_text(audio_filepath, service_provider, api_key):
# Initialize model
model = SpeechToText(api_key=api_key)

# Upload audio
uploaded_file = model.upload_audio(audio_filepath)
print(f"file uploaded: {uploaded_file}")

# Submit transcription request
job = model.transcribe(
uploaded_file["upload_url"], service_provider=service_provider
)

assert job.status != "ERROR", f"{job}"
print(f"Job ID: {job.id}")

# check job status
response = model.status(job.id, service_provider=service_provider)

while response.status == "IN_PROGRESS":
print(f"Response = {response}. Retrying in 30 seconds")
time.sleep(30)
response = model.status(job.id, service_provider=service_provider)

return response
1 change: 1 addition & 0 deletions slashml/text_summarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ def summarize(self, text: str, service_provider: ServiceProvider):

def status(self, job_id: str, service_provider: ServiceProvider):
return getTaskStatus(self._base_url, self._headers, job_id, service_provider)