Skip to content

Commit

Permalink
complete implementation of google api
Browse files Browse the repository at this point in the history
  • Loading branch information
Maleehak committed Apr 16, 2024
1 parent 9ecd302 commit c0f801d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ public ResponseEntity<String> generateSummary(@RequestParam String video, @Reque
return ResponseEntity.ok(summary);
}


@GetMapping("/get-summary/v2")
public ResponseEntity<String> generateSummaryFromSubtitles(@RequestParam String video, @RequestParam int lines){
String summary = youtubeVideoSummaryService.generateSummaryFromSubtitles(video, lines);
return ResponseEntity.ok(summary);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.BriefMe.request;
package com.example.BriefMe.data.request;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.BriefMe.request;
package com.example.BriefMe.data.request;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,25 +20,30 @@ public class YoutubeVideoSummaryService {
AudioToTextConverter audioToTextConverter;

@Autowired
TextSummarizer textSummarizer;
@Qualifier("googleVertexAiTextSummarizer")
TextSummarizer googleVertexAiTextSummarizer;


@Autowired
@Qualifier("customTextSummarizer")
TextSummarizer customTextSummarizer;

@Autowired
VideoToTextConverter videoToTextConverter;

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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -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(),
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand Down Expand Up @@ -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();

Expand Down
9 changes: 9 additions & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c0f801d

Please sign in to comment.