Spaces:
Runtime error
Runtime error
import os | |
from pathlib import Path | |
import boto3 | |
import click | |
from click_help_colors import HelpColorsGroup | |
from dotenv import load_dotenv | |
load_dotenv() | |
def main() -> None: | |
"""TTS Service CLI""" | |
def serve(share: bool) -> None: | |
"""Start the TTS Service""" | |
from tts_service.app import app | |
app.launch(share=share) | |
def service() -> None: | |
"""Manages the deployed service.""" | |
def upload_voices(bucket: str, prefix: str, delete: bool, dry_run: bool, directory: Path) -> None: | |
"""Upload voices to the service""" | |
s3 = boto3.client("s3") | |
prefix = prefix.strip("/") | |
names = set() | |
for path in directory.glob("*.pth"): | |
names.add(path.name) | |
with path.open("rb") as file: | |
if dry_run: | |
click.echo(f"Would upload {path.name} to {bucket}/{prefix}") | |
else: | |
s3.put_object(Bucket=bucket, Key=f"{prefix}/{path.name}", Body=file) | |
# s3.upload_fileobj(file, bucket, f"{prefix}/{path.name}") | |
if not names: | |
raise click.ClickException(f"no voices found in directory {directory}") | |
deleted = 0 | |
if delete: | |
paginator = s3.get_paginator("list_objects_v2") | |
for page in paginator.paginate(Bucket=bucket, Prefix=prefix): | |
for obj in page["Contents"]: | |
key = obj["Key"] | |
if key.split("/")[-1] not in names: | |
if dry_run: | |
click.echo(f"Would delete {key}") | |
else: | |
s3.delete_object(Bucket=bucket, Key=key) | |
deleted += 1 | |
deleted_message = f", {deleted} deleted" if delete else "" | |
if not dry_run: | |
click.echo(f"{bucket}/{prefix}: {len(names)} voices uploaded{deleted_message}") | |
if __name__ == "__main__": | |
main() | |