From c7b9fb5f54d69b28d7324e6aade2a4f9815390e4 Mon Sep 17 00:00:00 2001 From: Maleehak Date: Thu, 11 Apr 2024 16:22:05 +0500 Subject: [PATCH] configure google service account --- .gitignore | 3 ++- .../com/example/BriefMe/BriefMeApplication.java | 2 +- .../service/impl/AudioToTextConverterImpl.java | 17 +++++++++++++++-- .../src/main/resources/application.properties | 2 ++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 41f5431..c7074a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .mvn/wrapper backend/target/ -.idea/ \ No newline at end of file +.idea/ +google-api-credentials.json \ No newline at end of file diff --git a/backend/src/main/java/com/example/BriefMe/BriefMeApplication.java b/backend/src/main/java/com/example/BriefMe/BriefMeApplication.java index d9cbc47..45cedc5 100644 --- a/backend/src/main/java/com/example/BriefMe/BriefMeApplication.java +++ b/backend/src/main/java/com/example/BriefMe/BriefMeApplication.java @@ -15,7 +15,7 @@ @Slf4j public class BriefMeApplication { - public static void main(String[] args) throws IOException { + public static void main(String[] args){ SpringApplication.run(BriefMeApplication.class, args); } } diff --git a/backend/src/main/java/com/example/BriefMe/service/impl/AudioToTextConverterImpl.java b/backend/src/main/java/com/example/BriefMe/service/impl/AudioToTextConverterImpl.java index dac2211..83987f0 100644 --- a/backend/src/main/java/com/example/BriefMe/service/impl/AudioToTextConverterImpl.java +++ b/backend/src/main/java/com/example/BriefMe/service/impl/AudioToTextConverterImpl.java @@ -2,24 +2,37 @@ import com.example.BriefMe.service.client.AudioToTextConverter; import com.example.BriefMe.util.SpeechRecognitionTask; +import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.speech.v1.SpeechClient; +import com.google.cloud.speech.v1.SpeechSettings; +import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Slf4j @Component public class AudioToTextConverterImpl implements AudioToTextConverter { + @Value("${google.api.credentials.location}") + private String credentialsPath; + public String covertAudioToText(String audioFile){ - try (SpeechClient speechClient = SpeechClient.create()) { + try{ + CredentialsProvider credentialsProvider = FixedCredentialsProvider.create( + ServiceAccountCredentials.fromStream(new FileInputStream(credentialsPath))); + + SpeechSettings settings = SpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider).build(); + SpeechClient speechClient = SpeechClient.create(settings); log.info("Loading the audio file into memory..."); diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 8920d27..efb0621 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1 +1,3 @@ spring.application.name=BriefMe +google.api.credentials.location=google-api-credentials.json +