From c0f801db7320d5b4d03d07ac284d751403bf722a Mon Sep 17 00:00:00 2001 From: Maleehak Date: Tue, 16 Apr 2024 22:38:59 +0500 Subject: [PATCH] complete implementation of google api --- .../example/BriefMe/BriefMeApplication.java | 1 - .../YoutubeVideoSummaryController.java | 7 ++++ .../{ => data}/request/VertexAIData.java | 2 +- .../request/VertexAIParameters.java | 2 +- .../service/YoutubeVideoSummaryService.java | 14 +++++--- ...java => GoogleVertexAiTextSummarizer.java} | 32 +++++++------------ .../impl/YoutubeVideoToTextConverterImpl.java | 7 ++-- .../src/main/resources/application.properties | 9 ++++++ 8 files changed, 45 insertions(+), 29 deletions(-) rename backend/src/main/java/com/example/BriefMe/{ => data}/request/VertexAIData.java (75%) rename backend/src/main/java/com/example/BriefMe/{ => data}/request/VertexAIParameters.java (83%) rename backend/src/main/java/com/example/BriefMe/service/impl/{PredictTextSummarizationSample.java => GoogleVertexAiTextSummarizer.java} (82%) diff --git a/backend/src/main/java/com/example/BriefMe/BriefMeApplication.java b/backend/src/main/java/com/example/BriefMe/BriefMeApplication.java index fdafa74..8af3ea3 100644 --- a/backend/src/main/java/com/example/BriefMe/BriefMeApplication.java +++ b/backend/src/main/java/com/example/BriefMe/BriefMeApplication.java @@ -1,6 +1,5 @@ package com.example.BriefMe; -import com.example.BriefMe.service.impl.PredictTextSummarizationSample; import java.io.IOException; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; diff --git a/backend/src/main/java/com/example/BriefMe/controllers/YoutubeVideoSummaryController.java b/backend/src/main/java/com/example/BriefMe/controllers/YoutubeVideoSummaryController.java index be6ecf5..108322f 100644 --- a/backend/src/main/java/com/example/BriefMe/controllers/YoutubeVideoSummaryController.java +++ b/backend/src/main/java/com/example/BriefMe/controllers/YoutubeVideoSummaryController.java @@ -21,4 +21,11 @@ public ResponseEntity generateSummary(@RequestParam String video, @Reque return ResponseEntity.ok(summary); } + + @GetMapping("/get-summary/v2") + public ResponseEntity generateSummaryFromSubtitles(@RequestParam String video, @RequestParam int lines){ + String summary = youtubeVideoSummaryService.generateSummaryFromSubtitles(video, lines); + return ResponseEntity.ok(summary); + } + } diff --git a/backend/src/main/java/com/example/BriefMe/request/VertexAIData.java b/backend/src/main/java/com/example/BriefMe/data/request/VertexAIData.java similarity index 75% rename from backend/src/main/java/com/example/BriefMe/request/VertexAIData.java rename to backend/src/main/java/com/example/BriefMe/data/request/VertexAIData.java index 4b17051..93917b3 100644 --- a/backend/src/main/java/com/example/BriefMe/request/VertexAIData.java +++ b/backend/src/main/java/com/example/BriefMe/data/request/VertexAIData.java @@ -1,4 +1,4 @@ -package com.example.BriefMe.request; +package com.example.BriefMe.data.request; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/backend/src/main/java/com/example/BriefMe/request/VertexAIParameters.java b/backend/src/main/java/com/example/BriefMe/data/request/VertexAIParameters.java similarity index 83% rename from backend/src/main/java/com/example/BriefMe/request/VertexAIParameters.java rename to backend/src/main/java/com/example/BriefMe/data/request/VertexAIParameters.java index 1b14cd6..aadbfdf 100644 --- a/backend/src/main/java/com/example/BriefMe/request/VertexAIParameters.java +++ b/backend/src/main/java/com/example/BriefMe/data/request/VertexAIParameters.java @@ -1,4 +1,4 @@ -package com.example.BriefMe.request; +package com.example.BriefMe.data.request; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/backend/src/main/java/com/example/BriefMe/service/YoutubeVideoSummaryService.java b/backend/src/main/java/com/example/BriefMe/service/YoutubeVideoSummaryService.java index 6941458..3da73ba 100644 --- a/backend/src/main/java/com/example/BriefMe/service/YoutubeVideoSummaryService.java +++ b/backend/src/main/java/com/example/BriefMe/service/YoutubeVideoSummaryService.java @@ -6,6 +6,7 @@ import com.example.BriefMe.service.client.VideoToTextConverter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Slf4j @@ -19,7 +20,13 @@ public class YoutubeVideoSummaryService { AudioToTextConverter audioToTextConverter; @Autowired - TextSummarizer textSummarizer; + @Qualifier("googleVertexAiTextSummarizer") + TextSummarizer googleVertexAiTextSummarizer; + + + @Autowired + @Qualifier("customTextSummarizer") + TextSummarizer customTextSummarizer; @Autowired VideoToTextConverter videoToTextConverter; @@ -27,17 +34,16 @@ public class YoutubeVideoSummaryService { public String generateSummary(String youtubeVideoUrl, int summarizeIn) { String audioFile = audioExtractor.extractAudio(youtubeVideoUrl); String text = audioToTextConverter.covertAudioToText(audioFile); - String summary = textSummarizer.generateSummary(text, summarizeIn); + String summary = customTextSummarizer.generateSummary(text, summarizeIn); log.info("Text summarization completed using custom text summarizer ..."); - return summary; } public String generateSummaryFromSubtitles(String youtubeVideoUrl, int summarizeIn) { String subtitlesFile = videoToTextConverter.fetchSubtitlesJsonFileFromVideo(youtubeVideoUrl); String subtitles = videoToTextConverter.readSubtitlesFromJsonFile(subtitlesFile); - String summary = textSummarizer.generateSummary(subtitles, summarizeIn); + String summary = googleVertexAiTextSummarizer.generateSummary(subtitles, summarizeIn); log.info("Text summarization completed using Goggle generative text summarizer..."); return summary; diff --git a/backend/src/main/java/com/example/BriefMe/service/impl/PredictTextSummarizationSample.java b/backend/src/main/java/com/example/BriefMe/service/impl/GoogleVertexAiTextSummarizer.java similarity index 82% rename from backend/src/main/java/com/example/BriefMe/service/impl/PredictTextSummarizationSample.java rename to backend/src/main/java/com/example/BriefMe/service/impl/GoogleVertexAiTextSummarizer.java index 5fde3c2..94058f0 100644 --- a/backend/src/main/java/com/example/BriefMe/service/impl/PredictTextSummarizationSample.java +++ b/backend/src/main/java/com/example/BriefMe/service/impl/GoogleVertexAiTextSummarizer.java @@ -1,16 +1,14 @@ package com.example.BriefMe.service.impl; import com.example.BriefMe.properties.VertexAIProperties; -import com.example.BriefMe.request.VertexAIParameters; -import com.example.BriefMe.request.VertexAIData; +import com.example.BriefMe.data.request.VertexAIParameters; +import com.example.BriefMe.data.request.VertexAIData; import com.example.BriefMe.service.client.TextSummarizer; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.IOException; -import javax.print.DocFlavor.STRING; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; +import org.springframework.stereotype.Component; import com.google.cloud.aiplatform.v1beta1.EndpointName; import com.google.cloud.aiplatform.v1beta1.PredictResponse; import com.google.cloud.aiplatform.v1beta1.PredictionServiceClient; @@ -20,12 +18,10 @@ import java.util.ArrayList; import java.util.List; - -@Service +@Component @Slf4j -public class PredictTextSummarizationSample implements TextSummarizer { +public class GoogleVertexAiTextSummarizer implements TextSummarizer { - //TODO: Set vaules in application.properties @Autowired VertexAIProperties vertexAIProperties; @Override @@ -41,8 +37,7 @@ public String generateSummary(String text, int numberOfLines) { .build(); // Initialize client - try (PredictionServiceClient predictionServiceClient = - PredictionServiceClient.create(predictionServiceSettings)) { + try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings)) { final EndpointName endpointName = EndpointName.ofProjectLocationPublisherModelName( vertexAIProperties.getProject(), @@ -61,23 +56,20 @@ public String generateSummary(String text, int numberOfLines) { JsonFormat.parser().merge(parameters, parameterValueBuilder); Value parameterValue = parameterValueBuilder.build(); - PredictResponse predictResponse = - predictionServiceClient.predict(endpointName, instances, parameterValue); - - //TODO: Fetch and return data - System.out.println("Predict Response"); - System.out.println(predictResponse); + PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, parameterValue); + return predictResponse.getPredictions(0).getStructValue().getFieldsOrThrow("content").getStringValue(); } - } catch (Exception e) { - throw new RuntimeException(e); + log.error("Unable to get predictions from Google Vertex AI. {}", e.getMessage()); + return "Nothing to summarize"; } - return "Nothing to summarize"; + } private String createPromptString(String text, int numberOfLines){ try{ String prompt = "Provide a short summary in "+ numberOfLines +" numeric bullet points:" + text; + log.info("Prompt: {}", prompt); VertexAIData vertexAIData = new VertexAIData(prompt); ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.writeValueAsString(vertexAIData); diff --git a/backend/src/main/java/com/example/BriefMe/service/impl/YoutubeVideoToTextConverterImpl.java b/backend/src/main/java/com/example/BriefMe/service/impl/YoutubeVideoToTextConverterImpl.java index b2c632e..3a12b3a 100644 --- a/backend/src/main/java/com/example/BriefMe/service/impl/YoutubeVideoToTextConverterImpl.java +++ b/backend/src/main/java/com/example/BriefMe/service/impl/YoutubeVideoToTextConverterImpl.java @@ -7,6 +7,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -31,7 +33,7 @@ public String fetchSubtitlesJsonFileFromVideo(String inputVideo) { try { String[] command = { - "yt-dlp", + "/opt/homebrew/bin/yt-dlp", "--skip-download", "--write-subs", "--write-auto-subs", @@ -105,7 +107,8 @@ public String readSubtitlesFromJsonFile(String filePath) { } } } - log.info("Extracted text: {} ", subtitles); + log.info("Subtitles extracted..."); + Files.deleteIfExists(Path.of(filePath)); return subtitles.toString(); diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index efb0621..97f13b2 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,3 +1,12 @@ spring.application.name=BriefMe google.api.credentials.location=google-api-credentials.json +google.vertex-ai.project=artful-lane-419217 +google.vertex-ai.location=us-central1 +google.vertex-ai.publisher=google +google.vertex-ai.model=text-bison@001 +google.vertex-ai.model.temperature=0.1 +google.vertex-ai.model.max-output-token=1024 +google.vertex-ai.model.top-p=0.9 +google.vertex-ai.model.top-k=40 +