Begin moving cli to click

This commit is contained in:
Marty Oehme 2023-08-24 14:37:01 +02:00
parent 96634f547d
commit c2710d180b
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -2,6 +2,8 @@ import requests
import logging import logging
import time import time
import sys import sys
import click
from pathlib import Path
args = sys.argv args = sys.argv
@ -22,20 +24,57 @@ headers = {
"Authorization": f"Bearer {bearer_token}", "Authorization": f"Bearer {bearer_token}",
} }
input_data = {"input": {"url": "https://0x0.st/H9PH.aac"}}
@click.command() @click.group()
def cli(): @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: def main(args: list[str]) -> None:
if len(args) <= 1: if args[1] == "status":
sys.exit(0)
if args[1] == "health":
logging.info("requesting health status...")
response = requests.get(health_endpoint, headers=headers)
elif args[1] == "status":
if len(args) <= 2: if len(args) <= 2:
logging.error("No job id to get status from supplied.") logging.error("No job id to get status from supplied.")
sys.exit(1) sys.exit(1)
@ -50,9 +89,6 @@ def main(args: list[str]) -> None:
elif args[1] == "purge": elif args[1] == "purge":
logging.info("purging all jobs in queue...") logging.info("purging all jobs in queue...")
response = requests.post(purge_endpoint, headers=headers) 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() json = response.json()
@ -71,4 +107,4 @@ def main(args: list[str]) -> None:
if __name__ == "__main__": if __name__ == "__main__":
main(args) cli(auto_envvar_prefix='VERBANOTE')