Skip to content
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

better json support #2571

Open
ralyodio opened this issue Nov 18, 2024 · 2 comments
Open

better json support #2571

ralyodio opened this issue Nov 18, 2024 · 2 comments

Comments

@ralyodio
Copy link

Can we get a way to output json to STDOUT?

@KitaitiMakoto
Copy link
Contributor

KitaitiMakoto commented Nov 25, 2024

Whisper.cpp provides API for logging: whisper_log_set
Using it, you may customize logging format.

@KitaitiMakoto
Copy link
Contributor

KitaitiMakoto commented Nov 26, 2024

Also, you may set callback called on new segment: new_segment_callback

require "whisper"
require "json"

# Set log callback to convert log to JSON format
Whisper.log_set ->(level, text, _) {
  puts ({
    level: level,
    text:  text
  }).to_json
}, nil

whisper = Whisper::Context.new("../../models/ggml-base.bin")
params = Whisper::Params.new

# Helper function
def format_time(time_ms)
  sec, decimal_part = time_ms.divmod(1000)
  min, sec = sec.divmod(60)
  hour, min = min.divmod(60)
  "%02d:%02d:%02d.%03d" % [hour, min, sec, decimal_part]
end

# Set new segment callback to convert transcription result to JSON format
params.on_new_segment do |segment|
  puts ({
    start_time: format_time(segment.start_time),
    end_time:   format_time(segment.end_time),
    text:       segment.text
  }).to_json
end

whisper.transcribe("../../samples/jfk.wav", params)
{"level":2,"text":"whisper_init_from_file_with_params_no_state: loading model from '../../models/ggml-base.bin'\n"}
{"level":2,"text":"whisper_init_with_params_no_state: use gpu    = 1\n"}
{"level":2,"text":"whisper_init_with_params_no_state: flash attn = 0\n"}
  :
  :
(snip)
  :
  :
{"level":2,"text":"whisper_init_state: compute buffer (cross)  =    4.65 MB\n"}
{"level":2,"text":"whisper_init_state: compute buffer (decode) =   97.27 MB\n"}
{"start_time":"00:00:00.000","end_time":"00:00:07.600","text":" And so my fellow Americans, ask not what your country can do for you,"}
{"start_time":"00:00:07.600","end_time":"00:00:10.600","text":" ask what you can do for your country."}

I used Ruby because I'm not familiar with C/C++, though essentially we should be able to do the same thing in C/C++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants