Support headless voice download

This commit is contained in:
Kovid Goyal 2024-09-27 16:10:26 +05:30
parent 1a3be4662a
commit 6b6fcf6e5a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 26 additions and 9 deletions

View File

@ -162,6 +162,24 @@ class DownloadResources(QDialog):
super().reject() 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(): def develop():
from calibre.gui2 import Application from calibre.gui2 import Application
app = Application([]) app = Application([])

View File

@ -13,11 +13,11 @@ from itertools import count
from time import monotonic from time import monotonic
from qt.core import ( from qt.core import (
QApplication,
QAudio, QAudio,
QAudioFormat, QAudioFormat,
QAudioSink, QAudioSink,
QByteArray, QByteArray,
QDialog,
QIODevice, QIODevice,
QIODeviceBase, QIODeviceBase,
QMediaDevices, QMediaDevices,
@ -469,14 +469,13 @@ class Piper(TTSBackend):
if not download_even_if_exists: if not download_even_if_exists:
return model_path, config_path return model_path, config_path
os.makedirs(os.path.dirname(model_path), exist_ok=True) os.makedirs(os.path.dirname(model_path), exist_ok=True)
from calibre.gui2.tts.download import DownloadResources from calibre.gui2.tts.download import download_resources
d = DownloadResources(_('Downloading voice for Read aloud'), _('Downloading neural network for the {} voice').format(voice.human_name), { 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['model_url']: (model_path, _('Neural network data')),
voice.engine_data['config_url']: (config_path, _('Neural network metadata')), voice.engine_data['config_url']: (config_path, _('Neural network metadata')),
}, parent=widget_parent(self)) }, parent=widget_parent(self), headless=getattr(QApplication.instance(), 'headless', False)
if d.exec() == QDialog.DialogCode.Accepted: )
return model_path, config_path return (model_path, config_path) if ok else ('', '')
return '', ''
def download_voice(self, v: Voice) -> None: def download_voice(self, v: Voice) -> None:
if not v.name: if not v.name: