|
| 1 | +package ai.rev; |
| 2 | + |
| 3 | +import ai.rev.topicextraction.models.TopicExtractionJobOptions; |
| 4 | +import ai.rev.topicextraction.models.TopicExtractionJob; |
| 5 | +import ai.rev.topicextraction.models.TopicExtractionJobStatus; |
| 6 | +import ai.rev.topicextraction.models.TopicExtractionResult; |
| 7 | +import ai.rev.topicextraction.TopicExtractionClient; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +public class TopicExtraction { |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + // Assign your access token to a String |
| 16 | + String accessToken = "your_access_token"; |
| 17 | + |
| 18 | + // Initialize the TopicExtractionClient with your access token |
| 19 | + TopicExtractionClient topicExtractionClient = new TopicExtractionClient(accessToken); |
| 20 | + |
| 21 | + // Create TopicExtractionJobOptions for your submission |
| 22 | + TopicExtractionJobOptions jobOptions = new TopicExtractionJobOptions(); |
| 23 | + String textSubmission = "An orange is a fruit of various citrus species in the family Rutaceae " + |
| 24 | + "it primarily refers to Citrus sinensis which is also called sweet orange, " + |
| 25 | + "to distinguish it from the related Citrus aurantium, referred to as bitter orange. " + |
| 26 | + "The sweet orange reproduces asexually. varieties of sweet orange arise through mutations. " + |
| 27 | + "The orange is a hybrid between pomelo (Citrus maxima) and mandarin (Citrus reticulata). " + |
| 28 | + "The chloroplast genome, and therefore the maternal line, is that of pomelo. " + |
| 29 | + "The sweet orange has had its full genome sequenced."; |
| 30 | + |
| 31 | + |
| 32 | + TopicExtractionJob submittedJob; |
| 33 | + try { |
| 34 | + submittedJob = topicExtractionClient.submitJobText(textSubmission, jobOptions); |
| 35 | + } catch (IOException e) { |
| 36 | + throw new RuntimeException("Failed to submit topic extraction job " + e.getMessage()); |
| 37 | + } |
| 38 | + |
| 39 | + String jobId = submittedJob.getJobId(); |
| 40 | + System.out.println("Job Id: " + jobId); |
| 41 | + System.out.println("Job Status: " + submittedJob.getJobStatus()); |
| 42 | + System.out.println("Created On: " + submittedJob.getCreatedOn()); |
| 43 | + |
| 44 | + // Waits 5 seconds between each status check to see if job is complete |
| 45 | + boolean isProcessingComplete = false; |
| 46 | + while (!isProcessingComplete) { |
| 47 | + TopicExtractionJob retrievedJob; |
| 48 | + try { |
| 49 | + retrievedJob = topicExtractionClient.getJobDetails(jobId); |
| 50 | + } catch (IOException e) { |
| 51 | + throw new RuntimeException( |
| 52 | + "Failed to retrieve topic extraction job info [" |
| 53 | + + jobId |
| 54 | + + "] " |
| 55 | + + e.getMessage()); |
| 56 | + } |
| 57 | + |
| 58 | + TopicExtractionJobStatus status = retrievedJob.getJobStatus(); |
| 59 | + if (status == TopicExtractionJobStatus.COMPLETED |
| 60 | + || status == TopicExtractionJobStatus.FAILED) { |
| 61 | + System.out.println( |
| 62 | + "Processing for " |
| 63 | + + jobId |
| 64 | + + ": " |
| 65 | + + status); |
| 66 | + isProcessingComplete = true; |
| 67 | + } else { |
| 68 | + try { |
| 69 | + Thread.sleep(5000); |
| 70 | + } catch (InterruptedException e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + TopicExtractionResult result = |
| 78 | + topicExtractionClient.getResultObject(jobId); |
| 79 | + } catch (IOException e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + * The job can now be deleted. Deleting the job will remove ALL information |
| 85 | + * about the job from the Rev AI servers. Subsequent requests to Rev AI that |
| 86 | + * use the deleted jobs Id will return 404's. |
| 87 | + */ |
| 88 | + // apiClient.deleteJob(jobId); |
| 89 | + } |
| 90 | +} |
0 commit comments