Skip to content

Commit

Permalink
Fixes the version detection, renames to cloai (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinderVosDeWael authored Dec 19, 2023
1 parent 3223dfa commit d73d140
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 56 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cli-oai pull request guidelines
# cli-cloai pull request guidelines

Pull requests are always welcome, and we appreciate any help you give. Note that a code of conduct applies to all spaces managed by the cli-oai project, including issues and pull requests. Please see the [Code of Conduct](CODE_OF_CONDUCT.md) for details.
Pull requests are always welcome, and we appreciate any help you give. Note that a code of conduct applies to all spaces managed by the cli-cloai project, including issues and pull requests. Please see the [Code of Conduct](CODE_OF_CONDUCT.md) for details.

When submitting a pull request, we ask you to check the following:

Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
# CLI-OAI
# CLI-cloai

CLI-OAI is a Python command-line interface for interacting with the OpenAI API. It provides a set of commands to interact with various OpenAI services such as Speech-to-Text (STT), Text-to-Speech (TTS), and Image Generation.
CLI-cloai is a Python command-line interface for interacting with the OpenAI API. It provides a set of commands to interact with various OpenAI services such as Speech-to-Text (STT), Text-to-Speech (TTS), and Image Generation.

## Installation

To install CLI-OAI, you can use the following command:
To install CLI-cloai, you can use the following command:

```sh
poetry add git+https://github.com/cmi-dair/cli-oai
poetry add git+https://github.com/cmi-dair/cli-cloai
```

## Usage

Before running CLI-OAI, make sure the environment variable `OPENAI_API_KEY` is set to your OpenAI API key.
Before running CLI-cloai, make sure the environment variable `OPENAI_API_KEY` is set to your OpenAI API key.

To use the CLI, run `oai --help` in your terminal. This will display a list of available commands and their descriptions.
To use the CLI, run `cloai --help` in your terminal. This will display a list of available commands and their descriptions.

Here is a brief overview of the main commands:

- `oai stt <filename>`: Transcribes audio files with OpenAI's STT models. The `filename` argument is the file to transcribe. It can be any format that ffmpeg supports. Use the `--clip` option to clip the file if it is too large.
- `cloai stt <filename>`: Transcribes audio files with OpenAI's STT models. The `filename` argument is the file to transcribe. It can be any format that ffmpeg supports. Use the `--clip` option to clip the file if it is too large.

- `oai tts <text>`: Generates audio files with OpenAI's Text to Speech models. The `text` argument is the text to convert to speech.
- `cloai tts <text>`: Generates audio files with OpenAI's Text to Speech models. The `text` argument is the text to convert to speech.

- `oai image <prompt>`: Generates images with OpenAI's DALL-E. The `prompt` argument is the text prompt to generate the image from.
- `cloai image <prompt>`: Generates images with OpenAI's DALL-E. The `prompt` argument is the text prompt to generate the image from.

Each command has additional options that can be viewed by running `oai <command> --help`.
Each command has additional options that can be viewed by running `cloai <command> --help`.

## Contributing

Contributions are welcome! Please see the [contributing guidelines](CONTRIBUTING.md) for more information.

## License

CLI-OAI is licensed under the terms of the [L-GPLv2.1 license](LICENSE).
CLI-cloai is licensed under the terms of the [L-GPLv2.1 license](LICENSE).

## Support

If you encounter any issues or have any questions, please report them on our [issues page](https://github.com/cmi-dair/cli-oai/issues).
If you encounter any issues or have any questions, please report them on our [issues page](https://github.com/cmi-dair/cli-cloai/issues).
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "cmi-oai-cli"
version = "0.0.1a6"
name = "cloai"
version = "0.0.1a7"
description = "A CLI for OpenAI's API"
authors = ["Reinder Vos de Wael <[email protected]>"]
license = "LGPL-2.1"
readme = "README.md"
packages = [{include = "oai", from = "src"}]
packages = [{include = "cloai", from = "src"}]

[tool.poetry.dependencies]
python = ">3.8, <4.0"
Expand Down Expand Up @@ -43,7 +43,7 @@ pythonpath = [
]

[tool.poetry.scripts]
oai = 'oai.__main__:main'
cloai = 'cloai.__main__:main'

[tool.tox]
legacy_tox_ini = """
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/oai/__main__.py → src/cloai/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Entry point of the OAI package."""
"""Entry point of the cloai package."""
import asyncio
import os
import sys
Expand All @@ -10,7 +10,7 @@ def main() -> None:
sys.stderr.write("Error: Please set the OPENAI_API_KEY environment variable.")
sys.exit(1)

from oai.cli import parser
from cloai.cli import parser

asyncio.run(parser.parse_args())

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/oai/cli/commands.py → src/cloai/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import ffmpeg

from oai import openai_api
from oai.cli import utils
from oai.core import config
from cloai import openai_api
from cloai.cli import utils
from cloai.core import config

settings = config.get_settings()
logger = logging.getLogger(settings.LOGGER_NAME)
Expand Down
13 changes: 7 additions & 6 deletions src/oai/cli/parser.py → src/cloai/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@
import argparse
import pathlib
import sys
from importlib import metadata

from oai.cli import commands
from oai.core import config, exceptions
from cloai.cli import commands
from cloai.core import config, exceptions

logger = config.get_logger()


PARSER_DEFAULTS = {
"epilog": "Please report issues at https://github.com/cmi-dair/cli-oai.",
"epilog": "Please report issues at https://github.com/cmi-dair/cli-cloai.",
"formatter_class": argparse.ArgumentDefaultsHelpFormatter,
}


async def parse_args() -> None:
"""Parse command line arguments and execute the corresponding command."""
parser = argparse.ArgumentParser(
prog="oai",
prog="cloai",
description="""
CLI wrapper for OpenAI's API. All commands require the OPENAI_API_KEY
environment variable to be set to a valid OpenAI API key.""",
**PARSER_DEFAULTS, # type: ignore[arg-type]
)
version = metadata.version(__package__ or __name__)

version = config.get_version()
parser.add_argument("--version", action="version", version=f"%(prog)s {version}")

subparsers = parser.add_subparsers(dest="command")
_add_stt_parser(subparsers)
_add_tts_parser(subparsers)
Expand Down
2 changes: 1 addition & 1 deletion src/oai/cli/utils.py → src/cloai/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import ffmpeg
import requests

from oai.core import config
from cloai.core import config

if TYPE_CHECKING:
from collections.abc import Generator
Expand Down
1 change: 1 addition & 0 deletions src/cloai/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Core modules of the cloai package."""
15 changes: 12 additions & 3 deletions src/oai/core/config.py → src/cloai/core/config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
"""Configuration for the oai module."""
"""Configuration for the cloai module."""
import functools
import logging
from importlib import metadata
from typing import Literal

import pydantic
import pydantic_settings


def get_version() -> str:
"""Return the version of the package."""
try:
return metadata.version("cloai")
except metadata.PackageNotFoundError:
return "unknown"


class Settings(pydantic_settings.BaseSettings):
"""Represents the settings for the oai module."""
"""Represents the settings for the cloai module."""

LOGGER_NAME: str = "oai"
LOGGER_NAME: str = "cloai"
LOGGER_VERBOSITY: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "DEBUG"

OPENAI_API_KEY: pydantic.SecretStr = pydantic.Field(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Custom exceptions."""
from oai.core import config
from cloai.core import config

logger = config.get_logger()

Expand Down
2 changes: 1 addition & 1 deletion src/oai/openai_api.py → src/cloai/openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import openai

from oai.core import config
from cloai.core import config

settings = config.get_settings()
OPENAI_API_KEY = settings.OPENAI_API_KEY
Expand Down
1 change: 0 additions & 1 deletion src/oai/core/__init__.py

This file was deleted.

19 changes: 19 additions & 0 deletions tests/unit/test___main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Tests for the entrypoint."""
import os

import pytest

from cloai import __main__ as main


def test_main_no_key() -> None:
"""Test case for the main function when OPENAI_API_KEY is not set.
It verifies that the main function raises a SystemExit exception with exit code 1.
"""
del os.environ["OPENAI_API_KEY"]

with pytest.raises(SystemExit) as exc:
main.main()

assert exc.value.code == 1
8 changes: 3 additions & 5 deletions tests/unit/test_cli_utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"""Tests for the CLI utility functions."""


import tempfile

import pytest_mock

from oai.cli import utils
from cloai.cli import utils


def test_clip_audio(mocker: pytest_mock.MockerFixture) -> None:
"""Tests that the audio file is clipped."""
magic = mocker.MagicMock()
mock_ffmpeg = mocker.patch("oai.cli.utils.ffmpeg", magic)
mock_ffmpeg = mocker.patch("cloai.cli.utils.ffmpeg", magic)
mock_ffmpeg.input.return_value = magic
mock_ffmpeg.output.return_value = magic
target_size = 100000
Expand All @@ -30,7 +28,7 @@ def test_clip_audio(mocker: pytest_mock.MockerFixture) -> None:
def test_download_audio(mocker: pytest_mock.MockerFixture) -> None:
"""Tests that a file is downloaded."""
magic = mocker.MagicMock()
mock_requests = mocker.patch("oai.cli.utils.requests")
mock_requests = mocker.patch("cloai.cli.utils.requests")
mock_requests.get.return_value = magic
magic.raise_for_status.return_value = None
magic.content = b"mock_content"
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/test_openai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
import pytest_mock

from oai import openai_api
from cloai import openai_api


@pytest.fixture()
Expand All @@ -25,7 +25,10 @@ def mock_openai(mocker: pytest_mock.MockFixture) -> mock.MagicMock:
audio=mock_audio,
images=mock_images,
)
return mocker.patch("oai.openai_api.openai.OpenAI", return_value=mock_client)
return mocker.patch(
"cloai.openai_api.openai.OpenAI",
return_value=mock_client,
)


class ConcreteOpenAiBaseClass(openai_api.OpenAIBaseClass):
Expand Down
24 changes: 11 additions & 13 deletions tests/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
from __future__ import annotations

import argparse
import os
import pathlib
import sys
from typing import TYPE_CHECKING

import pytest

from oai.cli import parser
from oai.core import exceptions
from cloai.cli import parser
from cloai.core import exceptions

if TYPE_CHECKING:
import pytest_mock


@pytest.fixture()
def _mock_package_metadata(mocker: pytest_mock.MockFixture) -> None:
"""Mocks the package metadata."""
mocker.patch("oai.cli.parser.metadata.version", return_value="0.0.0")
@pytest.fixture(autouse=True)
def _set_environment() -> None:
os.environ["OPENAI_API_KEY"] = "test"


@pytest.mark.parametrize(
Expand Down Expand Up @@ -182,7 +182,7 @@ async def test_run_command_with_whisper(mocker: pytest_mock.MockFixture) -> None
"model": "whisper-1",
}
args = argparse.Namespace(**arg_dict)
mock = mocker.patch("oai.cli.commands.speech_to_text")
mock = mocker.patch("cloai.cli.commands.speech_to_text")

await parser.run_command(args)

Expand All @@ -206,7 +206,7 @@ async def test_run_command_with_dalle(mocker: pytest_mock.MockFixture) -> None:
"n": 1,
}
args = argparse.Namespace(**arg_dict)
mock = mocker.patch("oai.cli.commands.image_generation")
mock = mocker.patch("cloai.cli.commands.image_generation")

await parser.run_command(args)

Expand All @@ -231,7 +231,7 @@ async def test_run_command_with_tts(mocker: pytest_mock.MockFixture) -> None:
"voice": "onyx",
}
args = argparse.Namespace(**arg_dict)
mock = mocker.patch("oai.cli.commands.text_to_speech")
mock = mocker.patch("cloai.cli.commands.text_to_speech")

await parser.run_command(args)

Expand All @@ -244,10 +244,9 @@ async def test_run_command_with_tts(mocker: pytest_mock.MockFixture) -> None:


@pytest.mark.asyncio()
@pytest.mark.usefixtures("_mock_package_metadata")
async def test_parse_args_without_arguments() -> None:
"""Tests the parse_args function with no arguments."""
sys.argv = ["oai"]
sys.argv = ["cloai"]
expected_error_code = 1

with pytest.raises(SystemExit) as excinfo:
Expand All @@ -264,12 +263,11 @@ async def test_parse_args_without_arguments() -> None:
"tts",
],
)
@pytest.mark.usefixtures("_mock_package_metadata")
async def test_parse_args_with_command_no_other_arguments(
command: str,
) -> None:
"""Tests the parse_args function with a command but no other arguments."""
sys.argv = ["oai", command]
sys.argv = ["cloai", command]
expected_error_code = 2

with pytest.raises(SystemExit) as excinfo:
Expand Down

0 comments on commit d73d140

Please sign in to comment.