Skip to content

Commit

Permalink
Support headless voice download
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Sep 27, 2024
1 parent 1a3be46 commit 6b6fcf6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/calibre/gui2/tts/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ def reject(self):
super().reject()


def download_resources(
title: str, message: str, urls: dict[str, tuple[str, str]], parent: QWidget | None = None, headless: bool = False
) -> bool:
if not headless:
d = DownloadResources(title, message, urls, parent=parent)
return d.exec() == QDialog.DialogCode.Accepted
from calibre import browser
print(title)
print(message)
for url, (path, name) in urls.items():
print(_('Downloading {}...').format(name))
br = browser()
data = br.open_novisit(url).read()
with open(path, 'wb') as f:
f.write(data)
return True


def develop():
from calibre.gui2 import Application
app = Application([])
Expand Down
17 changes: 8 additions & 9 deletions src/calibre/gui2/tts/piper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
from time import monotonic

from qt.core import (
QApplication,
QAudio,
QAudioFormat,
QAudioSink,
QByteArray,
QDialog,
QIODevice,
QIODeviceBase,
QMediaDevices,
Expand Down Expand Up @@ -469,14 +469,13 @@ def _download_voice(self, voice: Voice, download_even_if_exists: bool = False) -
if not download_even_if_exists:
return model_path, config_path
os.makedirs(os.path.dirname(model_path), exist_ok=True)
from calibre.gui2.tts.download import DownloadResources
d = DownloadResources(_('Downloading voice for Read aloud'), _('Downloading neural network for the {} voice').format(voice.human_name), {
voice.engine_data['model_url']: (model_path, _('Neural network data')),
voice.engine_data['config_url']: (config_path, _('Neural network metadata')),
}, parent=widget_parent(self))
if d.exec() == QDialog.DialogCode.Accepted:
return model_path, config_path
return '', ''
from calibre.gui2.tts.download import download_resources
ok = download_resources(_('Downloading voice for Read aloud'), _('Downloading neural network for the {} voice').format(voice.human_name), {
voice.engine_data['model_url']: (model_path, _('Neural network data')),
voice.engine_data['config_url']: (config_path, _('Neural network metadata')),
}, parent=widget_parent(self), headless=getattr(QApplication.instance(), 'headless', False)
)
return (model_path, config_path) if ok else ('', '')

def download_voice(self, v: Voice) -> None:
if not v.name:
Expand Down

0 comments on commit 6b6fcf6

Please sign in to comment.