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

REVAI-3855: examples #106

Merged
merged 17 commits into from
Dec 26, 2023
8 changes: 4 additions & 4 deletions examples/async_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""

import time
from rev_ai import apiclient
from rev_ai import apiclient, JobStatus
from rev_ai.models import CustomVocabulary

# String containing your access token
Expand Down Expand Up @@ -72,23 +72,23 @@
while True:
# Obtains details of a job in json format
job_details = client.get_job_details(job.id)
status = job_details.status.name
status = job_details.status

print("Job Status : {}".format(status))

# Checks if the job has been transcribed. Please note that this is not the recommended way
# of getting job status in a real application. For recommended methods of getting job status
# please see our documentation on setting a callback url here:
# https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/
if status == "IN_PROGRESS":
if status == JobStatus.IN_PROGRESS:
time.sleep(5)
continue

elif status == "FAILED":
print("Job Failed : {}".format(job_details.failure_detail))
break

if status == "TRANSCRIBED":
if status == JobStatus.TRANSCRIBED:
# Getting a list of current jobs connected with your account
# The optional parameters limits the length of the list.
# starting_after is a job id which causes the removal of
Expand Down
88 changes: 88 additions & 0 deletions examples/async_summarize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""Copyright 2019 REV

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import time
from rev_ai import apiclient, JobStatus
from rev_ai.models.asynchronous.summarization_job_status import SummarizationJobStatus
from rev_ai.models.asynchronous.summarization_options import SummarizationOptions
from rev_ai.models.nlp_model import NlpModel

# String containing your access token
access_token = "<your_access_token>"

# Create your api client
client = apiclient.RevAiAPIClient(access_token)

# Submitting a job through a local file.
#
# job = client.submit_job_local_file("your_local_file_path",
# delete_after_seconds=2592000,
# language='en',
# summarization_config=SummarizationOptions(
# model=NlpModel.PREMIUM
# ))


# Submitting a job with a link to the file you want transcribed
# Change url to your url, custom_vocabularies is optional like above
url = "https://www.rev.ai/FTC_Sample_1.mp3"
job = client.submit_job_url(media_url=url,
delete_after_seconds=2592000,
language='en',
summarization_config=SummarizationOptions(
model=NlpModel.PREMIUM
))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is odd formatting style (the integration instead of 4 spaces is much larger) are you trying to follow the existing format?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most likely linter was complaining about the line length. I'll double check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general - yes I was trying to follow existing formatting in other example files for consistency


print("Submitted Job")

while True:
# Obtains details of a job in json format
job_details = client.get_job_details(job.id)
status = job_details.status
summarization_status = job_details.summarization.status
print("Job Status : {}. Summarization status: {}.".format(status, summarization_status))

# Checks if the job has been transcribed and summarized. Please note that this is not the recommended way
# of getting job status in a real application. For recommended methods of getting job status
# please see our documentation on setting a callback url here:
# https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/
if status == JobStatus.IN_PROGRESS or summarization_status == SummarizationJobStatus.IN_PROGRESS:
time.sleep(5)
continue

if status == JobStatus.FAILED or summarization_status == SummarizationJobStatus.FAILED:
print("Job Failed : {}".format(job_details.failure_detail))
break

if status == JobStatus.TRANSCRIBED and summarization_status == SummarizationJobStatus.COMPLETED:
# obtain transcript text as a string for the job.
summary_text = client.get_transcript_summary_text(job.id)
print(summary_text)

# obtain transcript text as a json object for the job.
summary_json = client.get_transcript_summary_json(job.id)

# obtain transcript object for the job.
summary_obj = client.get_transcript_summary_object(job.id)

break

# Use the objects however you please

# Once you are done with the job, you can delete it.
# NOTE : This will PERMANENTLY DELETE all data related to a job. Exercise only
# if you're sure you want to delete the job.
# client.delete_job(job.id)
print("Job Submission and Collection Finished.")
95 changes: 95 additions & 0 deletions examples/async_translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""Copyright 2019 REV

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import time
from rev_ai import apiclient, JobStatus
from rev_ai.models.asynchronous.translation_job_status import TranslationJobStatus
from rev_ai.models.asynchronous.translation_language_options import TranslationLanguageOptions
from rev_ai.models.asynchronous.translation_options import TranslationOptions
from rev_ai.models.nlp_model import NlpModel

# String containing your access token
access_token = "<your_access_token>"

# Create your api client
client = apiclient.RevAiAPIClient(access_token)

# Submitting a job through a local file.
#
# job = client.submit_job_local_file("your_local_file_path",
# delete_after_seconds=2592000,
# language='en',
# translation_config=TranslationOptions(
# target_languages=[
# TranslationLanguageOptions("es", NlpModel.PREMIUM)
# ]
# ))


# Submitting a job with a link to the file you want transcribed and translated
url = "https://www.rev.ai/FTC_Sample_1.mp3"
job = client.submit_job_url(media_url=url,
delete_after_seconds=2592000,
language='en',
translation_config=TranslationOptions(
target_languages=[
TranslationLanguageOptions("es", NlpModel.PREMIUM)
]
))

print("Submitted Job")

while True:
# Obtains details of a job in json format
job_details = client.get_job_details(job.id)
status = job_details.status
translation_status = job_details.translation.target_languages[0].status
print("Job Status : {}. Translation status: {}.".format(status, translation_status))

# Checks if the job has been transcribed and translated. Please note that this is not the recommended way
# of getting job status in a real application. For recommended methods of getting job status
# please see our documentation on setting a callback url here:
# https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/
if status == JobStatus.IN_PROGRESS or translation_status == TranslationJobStatus.IN_PROGRESS:
time.sleep(5)
continue

elif status == JobStatus.FAILED or translation_status == TranslationJobStatus.FAILED:
print("Job Failed : {}".format(job_details.failure_detail))
break

if status == JobStatus.TRANSCRIBED and translation_status == TranslationJobStatus.COMPLETED:
# obtain transcript text as a string for the job.
transcript_text = client.get_translated_transcript_text(job.id, 'es')
print(transcript_text)

# obtain transcript text as a json object for the job.
transcript_json = client.get_translated_transcript_json(job.id, 'es')

# obtain transcript object for the job.
transcript_obj = client.get_translated_transcript_object(job.id, 'es')

# obtain captions for the job.
captions = client.get_translated_captions(job.id, 'es')

break

# Use the objects however you please

# Once you are done with the job, you can delete it.
# NOTE : This will PERMANENTLY DELETE all data related to a job. Exercise only
# if you're sure you want to delete the job.
# client.delete_job(job.id)
print("Job Submission and Collection Finished.")
13 changes: 6 additions & 7 deletions examples/language_identification_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"""

import time
from rev_ai import language_identification_client

from rev_ai import language_identification_client, JobStatus

from rev_ai.models.customer_url_data import CustomerUrlData

Expand All @@ -40,23 +39,23 @@
while True:
# Obtains details of a job in json format
job_details = client.get_job_details(job.id)
status = job_details.status.name
status = job_details.status

print("Job Status : {}".format(status))

# Checks if the job has been completed. Please note that this is not the recommended way
# of getting job status in a real application. For recommended methods of getting job status
# please see our documentation on callback_urls here:
# https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/
if status == "IN_PROGRESS":
time.sleep(2)
if status == JobStatus.IN_PROGRESS:
time.sleep(5)
continue

elif status == "FAILED":
elif status == JobStatus.FAILED:
print("Job Failed : {}".format(job_details.failure_detail))
break

if status == "COMPLETED":
if status == JobStatus.COMPLETED:
# Getting a list of current language identification jobs connected with your account
# The optional parameters limits the length of the list.
# starting_after is a job id which causes the removal of
Expand Down
10 changes: 5 additions & 5 deletions examples/sentiment_analysis_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""

import time
from rev_ai import sentiment_analysis_client, apiclient
from rev_ai import sentiment_analysis_client, apiclient, JobStatus
from rev_ai.models import SentimentValue


Expand Down Expand Up @@ -59,23 +59,23 @@
while True:
# Obtains details of a job in json format
job_details = client.get_job_details(job.id)
status = job_details.status.name
status = job_details.status

print("Job Status : {}".format(status))

# Checks if the job has been completed. Please note that this is not the recommended way
# of getting job status in a real application. For recommended methods of getting job status
# please see our documentation on callback_urls here:
# https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/
if status == "IN_PROGRESS":
if status == JobStatus.IN_PROGRESS:
time.sleep(2)
continue

elif status == "FAILED":
elif status == JobStatus.FAILED:
print("Job Failed : {}".format(job_details.failure_detail))
break

if status == "COMPLETED":
if status == JobStatus.COMPLETED:
# Getting a list of current sentiment analysis jobs connected with your account
# The optional parameters limits the length of the list.
# starting_after is a job id which causes the removal of
Expand Down
11 changes: 5 additions & 6 deletions examples/topic_extraction_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"""

import time
from rev_ai import topic_extraction_client, apiclient

from rev_ai import topic_extraction_client, apiclient, JobStatus

# String containing your access token
access_token = "<your_access_token>"
Expand Down Expand Up @@ -56,23 +55,23 @@
while True:
# Obtains details of a job in json format
job_details = client.get_job_details(job.id)
status = job_details.status.name
status = job_details.status

print("Job Status : {}".format(status))

# Checks if the job has been completed. Please note that this is not the recommended way
# of getting job status in a real application. For recommended methods of getting job status
# please see our documentation on setting a callback url here:
# https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/
if status == "IN_PROGRESS":
if status == JobStatus.IN_PROGRESS:
time.sleep(2)
continue

elif status == "FAILED":
elif status == JobStatus.FAILED:
print("Job Failed : {}".format(job_details.failure_detail))
break

if status == "COMPLETED":
if status == JobStatus.COMPLETED:
# Getting a list of current topic extraction jobs connected with your account
# The optional parameters limits the length of the list.
# starting_after is a job id which causes the removal of
Expand Down
20 changes: 19 additions & 1 deletion src/rev_ai/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,24 @@ def get_transcript_summary_text(self, id_):
)
return response.text

def get_transcript_summary_object(self, id_):
"""Get the transcript summary of a specific job as python object.

:param id_: id of job to be requested
:returns: transcript data as json
:raises: HTTPError
"""
if not id_:
raise ValueError('id_ must be provided')

response = self._make_http_request(
"GET",
urljoin(self.base_url, 'jobs/{}/transcript/summary'.format(id_)),
headers={'Accept': 'application/json'}
)

return Summary.from_json(response.json())

def get_transcript_summary_json(self, id_):
"""Get the transcript summary of a specific job as json.

Expand All @@ -552,7 +570,7 @@ def get_transcript_summary_json(self, id_):
headers={'Accept': 'application/json'}
)

return Summary.from_json(response.json())
return response.json()

def get_transcript_summary_json_as_stream(self, id_):
"""Get the transcript summary of a specific job as streamed json.
Expand Down
16 changes: 12 additions & 4 deletions src/rev_ai/models/asynchronous/job_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@


class JobStatus(Enum):
IN_PROGRESS = 1
TRANSCRIBED = 2
FAILED = 3
COMPLETED = 4
IN_PROGRESS = 'in_progress'
TRANSCRIBED = 'transcribed'
FAILED = 'failed'
COMPLETED = 'completed'

@classmethod
def from_string(cls, status):
return cls[status.upper()]

def __eq__(self, other):
if isinstance(other, JobStatus):
return self.value == other.value
elif isinstance(other, str):
return self.name == other
else:
return False
Loading
Loading