|
| 1 | +"""Copyright 2022 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 sentiment_analysis_client, apiclient |
| 18 | +from rev_ai.models import SentimentValue |
| 19 | + |
| 20 | + |
| 21 | +# String containing your access token |
| 22 | +access_token = "<your_access_token>" |
| 23 | + |
| 24 | +# Create your api client |
| 25 | +client = sentiment_analysis_client.SentimentAnalysisClient(access_token) |
| 26 | + |
| 27 | +# Submit a job with whatever text you want by changing this input |
| 28 | +text = "An umbrella or parasol is a folding canopy supported by wooden or metal ribs that is \ |
| 29 | + usually mounted on a wooden, metal, or plastic pole. It is designed to protect a person \ |
| 30 | + against rain or sunlight. The term umbrella is traditionally used when protecting oneself from \ |
| 31 | + rain, with parasol used when protecting oneself from sunlight, though the terms continue to be \ |
| 32 | + used interchangeably. Often the difference is the material used for the canopy; some parasols \ |
| 33 | + are not waterproof, and some umbrellas are transparent. Umbrella canopies may be made of \ |
| 34 | + fabric or flexible plastic. There are also combinations of parasol and umbrella that are \ |
| 35 | + called en-tout-cas (French for 'in any case')." |
| 36 | +job = client.submit_job_from_text(text, |
| 37 | + metadata=None, |
| 38 | + callback_url=None, |
| 39 | + delete_after_seconds=None, |
| 40 | + language=None, |
| 41 | + notification_config=None) |
| 42 | + |
| 43 | +# If you'd like to submit the transcript of an existing transcription job you can do so by |
| 44 | +# uncommenting the lines below |
| 45 | +# |
| 46 | +# async_job_id = "your_job_id" |
| 47 | +# async_api_client = apiclient.RevAiAPIClient(access_token) |
| 48 | +# transcript = async_api_client.get_transcript_object(async_job_id) |
| 49 | +# transcript_json = transcript |
| 50 | +# job = client.submit_job_from_transcript(transcript_json, |
| 51 | +# metadata=None, |
| 52 | +# callback_url=None, |
| 53 | +# delete_after_seconds=None, |
| 54 | +# language=None, |
| 55 | +# notification_config=None) |
| 56 | + |
| 57 | +print("Submitted Job") |
| 58 | + |
| 59 | +while True: |
| 60 | + # Obtains details of a job in json format |
| 61 | + job_details = client.get_job_details(job.id) |
| 62 | + status = job_details.status.name |
| 63 | + |
| 64 | + print("Job Status : {}".format(status)) |
| 65 | + |
| 66 | + # Checks if the job has been completed. Please note that this is not the recommended way |
| 67 | + # of getting job status in a real application. For recommended methods of getting job status |
| 68 | + # please see our documentation on callback_urls here: |
| 69 | + # https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/ |
| 70 | + if status == "IN_PROGRESS": |
| 71 | + time.sleep(2) |
| 72 | + continue |
| 73 | + |
| 74 | + elif status == "FAILED": |
| 75 | + print("Job Failed : {}".format(job_details.failure_detail)) |
| 76 | + break |
| 77 | + |
| 78 | + if status == "COMPLETED": |
| 79 | + # Getting a list of current sentiment analysis jobs connected with your account |
| 80 | + # The optional parameters limits the length of the list. |
| 81 | + # starting_after is a job id which causes the removal of |
| 82 | + # all jobs from the list which were created before that job |
| 83 | + list_of_jobs = client.get_list_of_jobs(limit=None, starting_after=None) |
| 84 | + |
| 85 | + # obtain a list of topics and their scores for the job |
| 86 | + result = client.get_result_object(job.id, filter_for=None) |
| 87 | + remove_none_elements = lambda dictionary: {k: v for k, v in dictionary.items() if v} |
| 88 | + print([remove_none_elements(message.__dict__) for message in result.messages]) |
| 89 | + |
| 90 | + break |
| 91 | + |
| 92 | +# Use the objects however you please |
| 93 | +# Once you are done with the job, you can delete it. |
| 94 | +# NOTE : This will PERMANENTLY DELETE all data related to a job. Exercise only |
| 95 | +# if you're sure you want to delete the job. |
| 96 | +# |
| 97 | +# client.delete_job(job.id) |
| 98 | + |
| 99 | +print("Job Submission and Collection Finished.") |
0 commit comments