generated from childmindresearch/template-python-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09554a3
commit 9a0b728
Showing
20 changed files
with
1,435 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,32 @@ | ||
# CMI-DAIR Template Python Repository | ||
|
||
This is a template repository. Below is a checklist of things you should do to use it: | ||
|
||
- [x] Run `setup_template.py` to set up the repository. | ||
- [ ] Rewrite this `README` file. | ||
- [ ] Install the `pre-commit` hooks. | ||
- [x] Update the `LICENSE` file to your desired license and set the year. | ||
- [ ] Update the supported versions in `SECURITY.md` or, if not relevant, delete this file. | ||
- [ ] Remove the placeholder src and test files, these are there merely to show how the CI works. | ||
- [ ] Grant third-party app permissions (e.g. Codecov) [here](https://github.com/organizations/cmi-dair/settings/installations), if necessary. | ||
- [ ] Either generate a `CODECOV_TOKEN` secret [here](https://github.com/cmi-dair/flowdump/blob/main/.github/workflows/python_tests.yaml) (if its a private repository) or remove the line `token: ${{ secrets.CODECOV_TOKEN }}` | ||
- [ ] API docs website: After the first successful build, go to the `Settings` tab of your repository, scroll down to the `GitHub Pages` section, and select `gh-pages` as the source. This will generate a link to your API docs. | ||
- [ ] Update stability badge in `README.md` to reflect the current state of the project. A list of stability badges to copy can be found [here](https://github.com/orangemug/stability-badges). The [node documentation](https://nodejs.org/docs/latest-v20.x/api/documentation.html#documentation_stability_index) can be used as a reference for the stability levels. | ||
|
||
# Project name | ||
# CLI-OAI | ||
|
||
[![Build](https://github.com/cmi-dair/cli-oai/actions/workflows/test.yaml/badge.svg?branch=main)](https://github.com/cmi-dair/cli-oai/actions/workflows/test.yaml?query=branch%3Amain) | ||
[![codecov](https://codecov.io/gh/cmi-dair/cli-oai/branch/main/graph/badge.svg?token=22HWWFWPW5)](https://codecov.io/gh/cmi-dair/cli-oai) | ||
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) | ||
![stability-wip](https://img.shields.io/badge/stability-work_in_progress-lightgrey.svg) | ||
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/cmi-dair/cli-oai/blob/main/LICENSE) | ||
[![MIT License](https://img.shields.io/badge/license-LGPL_2.1-blue.svg)](https://github.com/cmi-dair/cli-oai/blob/main/LICENSE) | ||
[![pages](https://img.shields.io/badge/api-docs-blue)](https://cmi-dair.github.io/cli-oai) | ||
|
||
What problem does this tool solve? | ||
CLI-OAI is a Python command-line interface for interacting with the OpenAI API. | ||
|
||
## Features | ||
## Usage | ||
|
||
- A few | ||
- Cool | ||
- Things | ||
Before running oai, make sure the environment variable OPENAI_API_KEY is set to | ||
your API key. To use the CLI, run `oai --help` in your terminal. See the `--help` | ||
function of the subcommands for more information on each command. | ||
|
||
## Installation | ||
|
||
Install this package via : | ||
|
||
```sh | ||
pip install oai | ||
``` | ||
|
||
Or get the newest development version via: | ||
Get the newest development version via: | ||
|
||
```sh | ||
pip install git+https://github.com/cmi-dair/cli-oai | ||
poetry add git+https://github.com/cmi-dair/cli-oai | ||
``` | ||
|
||
## Quick start | ||
|
||
Short tutorial, maybe with a | ||
## Contributing | ||
|
||
```Python | ||
import oai | ||
|
||
oai.short_example() | ||
``` | ||
Please see the [contributing guidelines](CONTRIBUTING.md) for guidelines on contributing to this project. | ||
|
||
## Links or References | ||
## License | ||
|
||
- [https://www.wikipedia.de](https://www.wikipedia.de) | ||
This project is licensed under the terms of the [L-GPLv2.1 license](LICENSE). |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Entry point of the OAI package.""" | ||
import asyncio | ||
|
||
from oai.cli import parser | ||
|
||
|
||
def main() -> None: | ||
"""Entry point for the CLI.""" | ||
asyncio.run(parser.parse_args()) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Modules for the Command Line Interface.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
"""Contains the core business logic of the OpenAI CLI.""" | ||
import asyncio | ||
import logging | ||
import pathlib | ||
import tempfile | ||
|
||
import ffmpeg | ||
|
||
from oai import openai_api | ||
from oai.cli import utils | ||
from oai.core import config | ||
|
||
settings = config.get_settings() | ||
logger = logging.getLogger(settings.LOGGER_NAME) | ||
MAX_FILE_SIZE = 24_500_000 # Max size is 25MB, but we leave some room for error. | ||
|
||
|
||
async def speech_to_text( | ||
filename: pathlib.Path, | ||
model: str, | ||
*, | ||
clip: bool = False, | ||
) -> str: | ||
"""Transcribes audio files with OpenAI's TTS models. | ||
Args: | ||
filename: The file to transcribe. Can be any format that ffmpeg supports. | ||
model: The transcription model to use. | ||
voice: The voice to use. | ||
clip: Whether to clip the file if it is too large, defaults to False. | ||
""" | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
temp_file = pathlib.Path(temp_dir) / "temp.mp3" | ||
ffmpeg.input(filename).output(str(temp_file)).overwrite_output().run() | ||
|
||
if clip: | ||
files = utils.clip_audio(temp_file, temp_dir, MAX_FILE_SIZE) | ||
else: | ||
files = [temp_file] | ||
|
||
stt = openai_api.SpeechToText() | ||
transcription_promises = [stt.run(filename, model=model) for filename in files] | ||
transcriptions = await asyncio.gather(*transcription_promises) | ||
|
||
return " ".join(transcriptions) | ||
|
||
|
||
async def text_to_speech( | ||
text: str, | ||
output_file: str, | ||
model: str, | ||
voice: str, | ||
) -> bytes: | ||
"""Converts text to speech with OpenAI's TTS models. | ||
Args: | ||
text: The text to convert to speech. | ||
output_file: The name of the output file. | ||
model: The model to use. | ||
voice: The voice to use. | ||
""" | ||
tts = openai_api.TextToSpeech() | ||
return await tts.run(text, output_file, model=model, voice=voice) | ||
|
||
|
||
async def image_generation( # noqa: PLR0913 | ||
prompt: str, | ||
output_base_name: str, | ||
model: str, | ||
width: int, | ||
height: int, | ||
quality: str, | ||
n: int, | ||
) -> None: | ||
"""Generates an image from text with OpenAI's Image Generation models. | ||
Args: | ||
prompt: The text to generate an image from. | ||
output_base_name: The base name of the output file. | ||
model: The model to use. | ||
width: The width of the generated image. Defaults to 1024. | ||
height: The height of the generated image. Defaults to 1024. | ||
quality: The quality of the generated image. Defaults to "standard". | ||
n: The number of images to generate. Defaults to 1. | ||
Returns: | ||
bytes: The generated image as bytes. | ||
""" | ||
image_generation = openai_api.ImageGeneration() | ||
urls = await image_generation.run( | ||
prompt, | ||
model=model, | ||
width=width, | ||
height=height, | ||
quality=quality, | ||
n=n, | ||
) | ||
|
||
for index, url in enumerate(urls): | ||
file = pathlib.Path(f"{output_base_name}_{index}.png") | ||
utils.download_file(file, url) |
Oops, something went wrong.