-
-
Notifications
You must be signed in to change notification settings - Fork 328
Add TTS support #481
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
grgr
wants to merge
10
commits into
crmne:main
Choose a base branch
from
grgr:feat/add-tts-support
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
Add TTS support #481
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e874aed
Updated Crack and Mail in gemfiles/*.gemfile.lock
grgr 529950d
Add TTS Support for Openai
grgr e9118f6
adding gemini tts support
grgr 682e94e
Support images in Gemini responses
crmne f32aea6
Added provider_class to Model::Info and Model's ActiveRecord integration
crmne d5d4072
Test on Rails 8.1
crmne b568a21
Added Rails 8.1 Appraisal gemfile
crmne 0c5b237
Removing gemfile locks for appraisal
crmne d227c0d
Revert unintentional removal of an action
grgr 95cd414
Merge branch 'main' into feat/add-tts-support
grgr 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| --- | ||
| layout: default | ||
| title: Text to Speech | ||
| nav_order: 7 | ||
| description: Convert text to speech | ||
| redirect_from: | ||
| - /guides/audio-transcription | ||
| - /guides/transcription | ||
| --- | ||
|
|
||
| # {{ page.title }} | ||
| {: .d-inline-block .no_toc } | ||
|
|
||
| v1.9.0+ | ||
| {: .label .label-green } | ||
|
|
||
| {{ page.description }} | ||
| {: .fs-6 .fw-300 } | ||
|
|
||
| ## Table of contents | ||
| {: .no_toc .text-delta } | ||
|
|
||
| 1. TOC | ||
| {:toc} | ||
|
|
||
| --- | ||
|
|
||
| After reading this guide, you will know: | ||
|
|
||
| * How to generate speech from text. | ||
| * How to save audio files. | ||
| * How to select different voices. | ||
| * How to access raw audio data. | ||
| * Specifics of language support. | ||
|
|
||
| ## Basic Text to Speech | ||
|
|
||
| Generate audio with the global `RubyLLM.tts` method: | ||
|
|
||
| ```ruby | ||
| audio = RubyLLM.tts("Hello, welcome to RubyLLM!") | ||
|
|
||
| ``` | ||
|
|
||
| ## Save Audio File | ||
| You can save the generated audio to a file. | ||
| If you are using OpenAI, the audio will be saved as an MP3 file. | ||
|
|
||
| ```ruby | ||
| audio = RubyLLM.tts("This is a text to speech example.", provider: :openai, model: "gpt-4o-mini-tts") | ||
| audio.save("example.mp3") | ||
| ``` | ||
|
|
||
| If you are using Gemini, the audio will be saved as a raw PCM file. | ||
|
|
||
| ```ruby | ||
| audio = RubyLLM.tts("This is a text to speech example.", provider: :gemini, model: "gemini-2.5-flash-preview-tts") | ||
| audio.save("example.pcm") | ||
| ``` | ||
|
|
||
| You can convert it to MP3 using ffmpeg: | ||
|
|
||
| ```bash | ||
| ffmpeg -f s16le -ar 24000 -ac 1 -i example.pcm example.mp3 | ||
| ``` | ||
|
|
||
| ### Select Voice | ||
| You can specify different voices. Supported voices for OpenAI | ||
| are alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, and verse. | ||
|
|
||
| For Gemini have a look at the [gemini voices](https://ai.google.dev/gemini-api/docs/speech-generation#voices). | ||
|
|
||
| ```ruby | ||
| # Using a specific voice | ||
| voice = "ash" | ||
| audio = RubyLLM.tts("Hello, this is a #{voice}`s voice.", voice: voice) | ||
| ``` | ||
|
|
||
| ### Access Audio Data | ||
| You can access the raw audio data: | ||
|
|
||
| ```ruby | ||
| audio = RubyLLM.tts("Accessing raw audio data.") | ||
| audio.data # => binary audio data (MP3 for OpenAI, PCM for Gemini) | ||
| ``` | ||
|
|
||
| ### Language Support | ||
| OpenAi and Gemini gather language support automatically based on the text provided. | ||
| Previously, you could specify the language manually in Gemini. | ||
|
|
||
| ## Next Steps | ||
|
|
||
| * [Chatting with AI Models]({% link _core_features/chat.md %}): Learn about conversational AI. | ||
| * [Image Generation]({% link _core_features/image-generation.md %}): Generate images from text. | ||
| * [Error Handling]({% link _advanced/error-handling.md %}): Master handling API errors. | ||
|
|
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyLLM | ||
| module Providers | ||
| class Gemini | ||
| # Speech generation methods for the Gemini API integration | ||
| module Speech | ||
| module_function | ||
|
|
||
| def speech_url | ||
| "models/#{@model}:generateContent" | ||
| end | ||
|
|
||
| def render_speech_payload(input, model:, voice:) | ||
| @model = model | ||
| { | ||
| contents: [{ | ||
| role: 'user', | ||
| parts: [{ text: input }] | ||
| }], | ||
| generationConfig: { | ||
| responseModalities: ['AUDIO'], | ||
| speechConfig: { | ||
| voiceConfig: { | ||
| prebuiltVoiceConfig: { | ||
| voiceName: voice | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| model: model | ||
| } | ||
| end | ||
|
|
||
| def parse_speech_response(response, model:) | ||
| base64_audio = response.body['candidates'][0]['content']['parts'][0]['inlineData']['data'] | ||
| pcm_data = Base64.decode64(base64_audio) | ||
|
|
||
| RubyLLM::Speech.new( | ||
| model: model, | ||
| data: pcm_data | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyLLM | ||
| module Providers | ||
| class OpenAI | ||
| # Speech generation methods for the OpenAI API integration | ||
| module Speech | ||
| module_function | ||
|
|
||
| def speech_url | ||
| 'audio/speech' | ||
| end | ||
|
|
||
| def render_speech_payload(input, model:, voice:) | ||
| { | ||
| model: model, | ||
| input: input, | ||
| voice: voice | ||
| } | ||
| end | ||
|
|
||
| def parse_speech_response(response, model:) | ||
| data = response.body | ||
| RubyLLM::Speech.new( | ||
| model: model, | ||
| data: data | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
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,32 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyLLM | ||
| # Represents a generated image from an AI model. | ||
| class Speech | ||
| attr_reader :model, :data | ||
|
|
||
| def initialize(data:, model: nil) | ||
| @model = model | ||
| @data = data | ||
| end | ||
|
|
||
| def save(path) | ||
| File.binwrite(File.expand_path(path), data) | ||
| path | ||
| end | ||
|
|
||
| def self.tts(input, # rubocop:disable Metrics/ParameterLists | ||
| model: nil, | ||
| provider: nil, | ||
| assume_model_exists: false, | ||
| voice: 'alloy', | ||
| context: nil) | ||
| config = context&.config || RubyLLM.config | ||
| model ||= config.default_audio_model | ||
| model, provider_instance = Models.resolve(model, provider: provider, assume_exists: assume_model_exists, | ||
| config: config) | ||
|
|
||
| provider_instance.tts(input, model: model.id, voice:) | ||
| end | ||
| end | ||
| end |
89 changes: 89 additions & 0 deletions
89
..._basic_functionality_gemini_gemini-2_5-flash-preview-tts_can_generate_audio_from_text.yml
Large diffs are not rendered by default.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
...settes/speech_basic_functionality_openai_gpt-4o-mini-tts_can_generate_audio_from_text.yml
Large diffs are not rendered by default.
Oops, something went wrong.
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,54 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
| require 'tempfile' | ||
|
|
||
| def save_and_verify_audio(audio) | ||
| # Create a temp file to save to | ||
| temp_file = Tempfile.new(['audio', '.mp3']) | ||
| temp_path = temp_file.path | ||
| temp_file.close | ||
|
|
||
| begin | ||
| saved_path = audio.save(temp_path) | ||
| expect(saved_path).to eq(temp_path) | ||
| expect(File.exist?(temp_path)).to be true | ||
|
|
||
| file_size = File.size(temp_path) | ||
| expect(file_size).to be > 1000 # Any real audio should be larger than 1KB | ||
| ensure | ||
| # Clean up | ||
| File.delete(temp_path) | ||
| end | ||
| end | ||
|
|
||
| RSpec.describe RubyLLM::Speech do | ||
| include_context 'with configured RubyLLM' | ||
|
|
||
| describe 'basic functionality' do | ||
| SPEECH_MODELS.each do |config| | ||
| provider = config[:provider] | ||
| model = config[:model] | ||
|
|
||
| it "#{provider}/#{model} can generate audio from text" do | ||
| voice = provider == :gemini ? 'Sadachbia' : 'alloy' | ||
| audio = RubyLLM.tts( | ||
| 'Hello, welcome!', | ||
| model: model, | ||
| provider: provider, | ||
| voice: voice | ||
| ) | ||
|
|
||
| expect(audio.model).to eq(model) | ||
|
|
||
| save_and_verify_audio audio | ||
| end | ||
| end | ||
|
|
||
| it 'validates model existence' do | ||
| expect do | ||
| RubyLLM.tts('Hello, welcome!', model: 'invalid-audio-model') | ||
| end.to raise_error(RubyLLM::ModelNotFoundError) | ||
| end | ||
| end | ||
| end |
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
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.
Uh oh!
There was an error while loading. Please reload this page.