From c2710d180bbd5fe70ce08db045f7727d847bd7e0 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 24 Aug 2023 14:37:01 +0200 Subject: [PATCH] Begin moving cli to click --- verbanote_client/main.py | 66 +++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/verbanote_client/main.py b/verbanote_client/main.py index f7fc2db..952ceee 100644 --- a/verbanote_client/main.py +++ b/verbanote_client/main.py @@ -2,6 +2,8 @@ import requests import logging import time import sys +import click +from pathlib import Path args = sys.argv @@ -22,20 +24,57 @@ headers = { "Authorization": f"Bearer {bearer_token}", } -input_data = {"input": {"url": "https://0x0.st/H9PH.aac"}} -@click.command() -def cli(): - ... +@click.group() +@click.option("--endpoint", "-e", help="URL of runpod serverless endpoint.") +@click.option("--token", "-t", help="Access token for runpod instance.") +# TODO @click.version_option() +def cli(token): + """Verbanote + + Transcribes any audio file given using OpenAI's whisper AI + and pyannote for speaker detection. + """ + print(f"Token: {token}") + + +@cli.command() +@click.argument( + "audiofile", + type=click.Path(exists=True, dir_okay=False, readable=True, path_type=Path), +) +def start(audiofile): + """Start processing the given audiofile. + + Queues a job in the processing queue of the AI api. + """ + url = _upload_to_oxo(audiofile) + input_data = {"input": {"url": url}} + logging.info(f"Requesting new job for {audiofile}...") + response = requests.post(run_endpoint, json=input_data, headers=headers) + click.echo(f"Job {response} has been queued.") + + +@cli.command() +def health(): + logging.info("requesting health status...") + resp = requests.get(health_endpoint, headers=headers) + click.echo(resp) + + +def _upload_to_oxo(file: Path, url: str = "https://0x0.st", expires: int = 2) -> str: + resp = requests.post( + url=url, + files={"file": open(file, "rb"), "expires": str(expires)}, + ) + if not resp.ok: + raise requests.exceptions.HTTPError() + logging.info(f"Uploaded file {file} to {str(resp.content)}") + return str(resp.content) + def main(args: list[str]) -> None: - if len(args) <= 1: - sys.exit(0) - - if args[1] == "health": - logging.info("requesting health status...") - response = requests.get(health_endpoint, headers=headers) - elif args[1] == "status": + if args[1] == "status": if len(args) <= 2: logging.error("No job id to get status from supplied.") sys.exit(1) @@ -50,9 +89,6 @@ def main(args: list[str]) -> None: elif args[1] == "purge": logging.info("purging all jobs in queue...") response = requests.post(purge_endpoint, headers=headers) - else: - logging.info("requesting new job...") - response = requests.post(run_endpoint, json=input_data, headers=headers) json = response.json() @@ -71,4 +107,4 @@ def main(args: list[str]) -> None: if __name__ == "__main__": - main(args) + cli(auto_envvar_prefix='VERBANOTE')