|
| 1 | +"""Copyright 2019 REV |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +""" |
| 15 | + |
| 16 | +import time |
| 17 | +from rev_ai import apiclient, JobStatus |
| 18 | +from rev_ai.models.asynchronous.translation_job_status import TranslationJobStatus |
| 19 | +from rev_ai.models.asynchronous.translation_language_options import TranslationLanguageOptions |
| 20 | +from rev_ai.models.asynchronous.translation_options import TranslationOptions |
| 21 | +from rev_ai.models.nlp_model import NlpModel |
| 22 | + |
| 23 | +# String containing your access token |
| 24 | +access_token = "<your_access_token>" |
| 25 | + |
| 26 | +# Create your api client |
| 27 | +client = apiclient.RevAiAPIClient(access_token) |
| 28 | + |
| 29 | +# Submitting a job through a local file. |
| 30 | +# |
| 31 | +# job = client.submit_job_local_file("your_local_file_path", |
| 32 | +# delete_after_seconds=2592000, |
| 33 | +# language='en', |
| 34 | +# translation_config=TranslationOptions( |
| 35 | +# target_languages=[ |
| 36 | +# TranslationLanguageOptions("es", NlpModel.PREMIUM) |
| 37 | +# ] |
| 38 | +# )) |
| 39 | + |
| 40 | + |
| 41 | +# Submitting a job with a link to the file you want transcribed and translated |
| 42 | +url = "https://www.rev.ai/FTC_Sample_1.mp3" |
| 43 | +job = client.submit_job_url(media_url=url, |
| 44 | + delete_after_seconds=2592000, |
| 45 | + language='en', |
| 46 | + translation_config=TranslationOptions( |
| 47 | + target_languages=[ |
| 48 | + TranslationLanguageOptions("es", NlpModel.PREMIUM) |
| 49 | + ] |
| 50 | + )) |
| 51 | + |
| 52 | +print("Submitted Job") |
| 53 | +print("Job Status : {}".format(job.status)) |
| 54 | + |
| 55 | +# Checks if the job has been transcribed and summarized. Please note that this is not the recommended way |
| 56 | +# of getting job status in a real application. For recommended methods of getting job status |
| 57 | +# please see our documentation on setting a callback url here: |
| 58 | +# https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/ |
| 59 | + |
| 60 | +while job.status == JobStatus.IN_PROGRESS: |
| 61 | + time.sleep(5) |
| 62 | + # Obtains details of a job in json format |
| 63 | + job = client.get_job_details(job.id) |
| 64 | + print("Job Status : {}".format(job.status)) |
| 65 | + |
| 66 | +if job.status == JobStatus.FAILED: |
| 67 | + print("Job Failed : {}".format(job.failure_detail)) |
| 68 | + exit() |
| 69 | + |
| 70 | +if job.status == JobStatus.TRANSCRIBED: |
| 71 | + print("Translation Status : {}".format(job.translation.target_languages[0].status)) |
| 72 | + # Checks if the job has been summarized. Please note that this is not the recommended way |
| 73 | + # of getting job status in a real application. For recommended methods of getting job status |
| 74 | + # please see our documentation on setting a callback url here: |
| 75 | + # https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/ |
| 76 | + while job.translation.target_languages[0].status == TranslationJobStatus.IN_PROGRESS: |
| 77 | + time.sleep(5) |
| 78 | + # Obtains details of a job in json format |
| 79 | + job = client.get_job_details(job.id) |
| 80 | + print("Translation Status : {}".format(job.translation.target_languages[0].status)) |
| 81 | + |
| 82 | +if job.translation.target_languages[0].status == TranslationJobStatus.FAILED: |
| 83 | + print("Translation Failed : {}".format(job.translation.target_languages[0].failure)) |
| 84 | + exit() |
| 85 | + |
| 86 | +# obtain transcript translation as a string for the job. |
| 87 | +transcript_text = client.get_translated_transcript_text(job.id, 'es') |
| 88 | +print(transcript_text) |
| 89 | + |
| 90 | +# obtain transcript translation as a json object for the job. |
| 91 | +transcript_json = client.get_translated_transcript_json(job.id, 'es') |
| 92 | + |
| 93 | +# obtain transcript translation object for the job. |
| 94 | +transcript_obj = client.get_translated_transcript_object(job.id, 'es') |
| 95 | + |
| 96 | +# obtain translated captions for the job. |
| 97 | +captions = client.get_translated_captions(job.id, 'es') |
| 98 | + |
| 99 | +# Use the objects however you please |
| 100 | + |
| 101 | +# Once you are done with the job, you can delete it. |
| 102 | +# NOTE : This will PERMANENTLY DELETE all data related to a job. Exercise only |
| 103 | +# if you're sure you want to delete the job. |
| 104 | +# client.delete_job(job.id) |
| 105 | +print("Job Submission and Collection Finished.") |
0 commit comments