-
Notifications
You must be signed in to change notification settings - Fork 123
feat telegram: add voice message support for telegram with pluggable #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
soufianebouaddis
wants to merge
4
commits into
jobrunr:main
Choose a base branch
from
soufianebouaddis:feature/add-voice-message-support-for-telegram
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d1514f
feat telegram: add voice message support for telegram with pluggable …
soufianebouaddis 21404e0
Merge main into feature/add-voice-message-support-for-telegram and re…
soufianebouaddis 16edc98
Move OpenAiSpeechToTextService.java to base/src/test/java/ai/javaclaw…
soufianebouaddis 6b3d09b
Clean .gitignore
soufianebouaddis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
base/src/main/java/ai/javaclaw/speech/SpeechToTextException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package ai.javaclaw.speech; | ||
|
|
||
| public class SpeechToTextException extends RuntimeException { | ||
|
|
||
| public SpeechToTextException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public SpeechToTextException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
base/src/main/java/ai/javaclaw/speech/SpeechToTextService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package ai.javaclaw.speech; | ||
|
|
||
| import java.io.InputStream; | ||
|
|
||
| public interface SpeechToTextService { | ||
|
|
||
| String transcribe(InputStream audioStream); | ||
| } |
121 changes: 121 additions & 0 deletions
121
base/src/main/java/ai/javaclaw/speech/WhisperCppSpeechToTextService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package ai.javaclaw.speech; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @Service | ||
| @ConditionalOnProperty(name = "speech.provider", havingValue = "whisper-cpp") | ||
| public class WhisperCppSpeechToTextService implements SpeechToTextService { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(WhisperCppSpeechToTextService.class); | ||
|
|
||
| private final String modelPath; | ||
|
|
||
| public WhisperCppSpeechToTextService( | ||
| @Value("${speech.whisper-cpp.model-path}") String modelPath) { | ||
| this.modelPath = modelPath; | ||
| } | ||
|
|
||
| @Override | ||
| public String transcribe(InputStream audioStream) { | ||
| LOGGER.info("Transcribing audio via whisper-cpp (model: {})", modelPath); | ||
|
|
||
| Path oggFile = null; | ||
| Path wavFile = null; | ||
| Path outputFile = null; | ||
|
|
||
| try { | ||
| oggFile = Files.createTempFile("whisper-input-", ".ogg"); | ||
| Files.write(oggFile, audioStream.readAllBytes()); | ||
|
|
||
| wavFile = Files.createTempFile("whisper-input-", ".wav"); | ||
| convertOggToWav(oggFile, wavFile); | ||
|
|
||
| outputFile = Files.createTempFile("whisper-output-", ".txt"); | ||
| Files.deleteIfExists(outputFile); | ||
|
|
||
| ProcessBuilder pb = new ProcessBuilder( | ||
| "whisper-cli", | ||
| "-m", modelPath, | ||
| "-f", wavFile.toString(), | ||
| "-otxt", | ||
| "-of", outputFile.toString().replace(".txt", ""), | ||
| "--no-prints" | ||
| ); | ||
| pb.redirectErrorStream(true); | ||
|
|
||
| Process process = pb.start(); | ||
| boolean finished = process.waitFor(60, TimeUnit.SECONDS); | ||
|
|
||
| if (!finished) { | ||
| process.destroyForcibly(); | ||
| throw new SpeechToTextException("whisper-cli timed out after 60 seconds"); | ||
| } | ||
|
|
||
| if (process.exitValue() != 0) { | ||
| String error = new String(process.getInputStream().readAllBytes()); | ||
| throw new SpeechToTextException("whisper-cli exited with code " + process.exitValue() + ": " + error); | ||
| } | ||
|
|
||
| if (!Files.exists(outputFile)) { | ||
| throw new SpeechToTextException("whisper-cli did not produce output file"); | ||
| } | ||
|
|
||
| String text = Files.readString(outputFile).trim(); | ||
| if (text.isBlank()) { | ||
| throw new SpeechToTextException("whisper-cli returned empty transcription"); | ||
| } | ||
|
|
||
| LOGGER.info("whisper-cpp transcription completed successfully"); | ||
| return text; | ||
|
|
||
| } catch (IOException | InterruptedException e) { | ||
| if (e instanceof InterruptedException) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| throw new SpeechToTextException("Failed to run whisper-cli", e); | ||
| } finally { | ||
| deleteSilently(oggFile); | ||
| deleteSilently(wavFile); | ||
| deleteSilently(outputFile); | ||
| } | ||
| } | ||
|
|
||
| private void convertOggToWav(Path oggFile, Path wavFile) throws IOException, InterruptedException { | ||
| ProcessBuilder pb = new ProcessBuilder( | ||
| "ffmpeg", "-y", "-i", oggFile.toString(), "-ar", "16000", "-ac", "1", wavFile.toString() | ||
| ); | ||
| pb.redirectErrorStream(true); | ||
|
|
||
| Process process = pb.start(); | ||
| boolean finished = process.waitFor(30, TimeUnit.SECONDS); | ||
|
|
||
| if (!finished) { | ||
| process.destroyForcibly(); | ||
| throw new SpeechToTextException("ffmpeg conversion timed out"); | ||
| } | ||
|
|
||
| if (process.exitValue() != 0) { | ||
| String error = new String(process.getInputStream().readAllBytes()); | ||
| throw new SpeechToTextException("ffmpeg conversion failed: " + error); | ||
| } | ||
| } | ||
|
|
||
| private void deleteSilently(Path path) { | ||
| if (path != null) { | ||
| try { | ||
| Files.deleteIfExists(path); | ||
| } catch (IOException ignored) { | ||
| } | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
base/src/test/java/ai/javaclaw/speech/MockSpeechToTextService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package ai.javaclaw.speech; | ||
|
|
||
| import java.io.InputStream; | ||
|
|
||
| public class MockSpeechToTextService implements SpeechToTextService { | ||
|
auloin marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| public String transcribe(InputStream audioStream) { | ||
| return "[voice message]"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
plugins/telegram/src/main/java/ai/javaclaw/channels/telegram/TelegramVoiceDownloader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ai.javaclaw.channels.telegram; | ||
|
|
||
| import org.telegram.telegrambots.meta.api.methods.GetFile; | ||
| import org.telegram.telegrambots.meta.api.objects.message.Message; | ||
| import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
| import org.telegram.telegrambots.meta.generics.TelegramClient; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.net.URI; | ||
|
|
||
| class TelegramVoiceDownloader { | ||
|
|
||
| private final TelegramClient telegramClient; | ||
| private final String botToken; | ||
|
|
||
| TelegramVoiceDownloader(TelegramClient telegramClient, String botToken) { | ||
| this.telegramClient = telegramClient; | ||
| this.botToken = botToken; | ||
| } | ||
|
|
||
| InputStream download(Message message) throws TelegramApiException, IOException { | ||
| String fileId = message.getVoice().getFileId(); | ||
| GetFile getFile = new GetFile(fileId); | ||
| String filePath = telegramClient.execute(getFile).getFilePath(); | ||
| String fileUrl = "https://api.telegram.org/file/bot" + botToken + "/" + filePath; | ||
| return URI.create(fileUrl).toURL().openStream(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been looking for a java library that does speech to text and I found vosk: https://github.com/alphacep/vosk-api. If it works, what do you think of making it the default @soufianebouaddis? We could also drop this implementation which requires having both ffmpeg and whisper-cli.