Begin moving cli to click
This commit is contained in:
parent
96634f547d
commit
c2710d180b
1 changed files with 51 additions and 15 deletions
|
@ -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')
|
||||
|
|
Loading…
Reference in a new issue