š« CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Integrate OpenAI audio models (#561)
Wendong-Fan4312be15cde4
diff --git a/camel/models/__init__.py b/camel/models/__init__.pyindex eaa7854c3..3220eb0aa 100644--- a/camel/models/__init__.py+++ b/camel/models/__init__.py@@ -15,6 +15,7 @@ from .anthropic_model import AnthropicModelfrom .base_model import BaseModelBackendfrom .model_factory import ModelFactoryfrom .open_source_model import OpenSourceModel+from .openai_audio_models import OpenAIAudioModelsfrom .openai_model import OpenAIModelfrom .stub_model import StubModel@@ -25,4 +26,5 @@ __all__ = ['StubModel','OpenSourceModel','ModelFactory',+ 'OpenAIAudioModels',]diff --git a/camel/models/openai_audio_models.py b/camel/models/openai_audio_models.pynew file mode 100644index 000000000..1b2f09c1d--- /dev/null+++ b/camel/models/openai_audio_models.py@@ -0,0 +1,251 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import os+from typing import Any, List, Optional, Union++from openai import OpenAI, _legacy_response++from camel.types import AudioModelType, VoiceType+++class OpenAIAudioModels:+ r"""Provides access to OpenAI's Text-to-Speech (TTS) and Speech_to_Text+ (STT) models."""++ def __init__(+ self,+ ) -> None:+ r"""Initialize an instance of OpenAI."""+ url = os.environ.get('OPENAI_API_BASE_URL')+ self._client = OpenAI(timeout=120, max_retries=3, base_url=url)++ def text_to_speech(+ self,+ input: str,+ model_type: AudioModelType = AudioModelType.TTS_1,+ voice: VoiceType = VoiceType.ALLOY,+ storage_path: Optional[str] = None,+ **kwargs: Any,+ ) -> Union[+ List[_legacy_response.HttpxBinaryResponseContent],+ _legacy_response.HttpxBinaryResponseContent,+ ]:+ r"""Convert text to speech using OpenAI's TTS model. This method+ converts the given input text to speech using the specified model and+ voice.++ Args:+ input (str): The text to be converted to speech.+ model_type (AudioModelType, optional): The TTS model to use.+ Defaults to `AudioModelType.TTS_1`.+ voice (VoiceType, optional): The voice to be used for generating+ speech. Defaults to `VoiceType.ALLOY`.+ storage_path (str, optional): The local path to store the+ generated speech file if provided, defaults to `None`.+ **kwargs (Any): Extra kwargs passed to the TTS API.++ Returns:+ Union[List[_legacy_response.HttpxBinaryResponseContent],+ _legacy_response.HttpxBinaryResponseContent]: List of response+ content object from OpenAI if input charaters more than 4096,+ single response content if input charaters less than 4096.++ Raises:+ Exception: If there's an error during the TTS API call.+ """+ try:+ # Model only support at most 4096 characters one time.+ max_chunk_size = 4095+ audio_chunks = []+ chunk_index = 0+ if len(input) > max_chunk_size:+ while input:+ if len(input) <= max_chunk_size:+ chunk = input+ input = ''+ else:+ # Find the nearest period before the chunk size limit+ while input[max_chunk_size - 1] != '.':+ max_chunk_size -= 1++ chunk = input[:max_chunk_size]+ input = input[max_chunk_size:].lstrip()++ response = self._client.audio.speech.create(+ model=model_type.value,+ voice=voice.value,+ input=chunk,+ **kwargs,+ )+ if storage_path:+ try:+ # Create a new storage path for each chunk+ file_name, file_extension = os.path.splitext(+ storage_path+ )+ new_storage_path = (+ f"{file_name}_{chunk_index}{file_extension}"+ )+ response.write_to_file(new_storage_path)+ chunk_index += 1+ except Exception as e:+ raise Exception(+ "Error during writing the file"+ ) from e++ audio_chunks.append(response)+ return audio_chunks++ else:+ response = self._client.audio.speech.create(+ model=model_type.value,+ voice=voice.value,+ input=input,+ **kwargs,+ )++ if storage_path:+ try:+ response.write_to_file(storage_path)+ except Exception as e:+ raise Exception("Error during write the file") from e++ return response++ except Exception as e:+ raise Exception("Error during TTS API call") from e++ def _split_audio(+ self, audio_file_path: str, chunk_size_mb: int = 24+ ) -> list:+ r"""Split the audio file into smaller chunks. Since the Whisper API+ only supports files that are less than 25 MB.++ Args:+ audio_file_path (str): Path to the input audio file.+ chunk_size_mb (int, optional): Size of each chunk in megabytes.+ Defaults to `24`.++ Returns:+ list: List of paths to the split audio files.+ """+ from pydub import AudioSegment++ audio = AudioSegment.from_file(audio_file_path)+ audio_format = os.path.splitext(audio_file_path)[1][1:].lower()++ # Calculate chunk size in bytes+ chunk_size_bytes = chunk_size_mb * 1024 * 1024++ # Number of chunks needed+ num_chunks = os.path.getsize(audio_file_path) // chunk_size_bytes + 1++ # Create a directory to store the chunks+ output_dir = os.path.splitext(audio_file_path)[0] + "_chunks"+ os.makedirs(output_dir, exist_ok=True)++ # Get audio chunk len in milliseconds+ chunk_size_milliseconds = len(audio) // (num_chunks)++ # Split the audio into chunks+ split_files = []+ for i in range(num_chunks):+ start = i * chunk_size_milliseconds+ end = (i + 1) * chunk_size_milliseconds+ if i + 1 == num_chunks:+ chunk = audio[start:]+ else:+ chunk = audio[start:end]+ # Create new chunk path+ chunk_path = os.path.join(output_dir, f"chunk_{i}.{audio_format}")+ chunk.export(chunk_path, format=audio_format)+ split_files.append(chunk_path)+ return split_files++ def speech_to_text(+ self,+ audio_file_path: str,+ translate_into_english: bool = False,+ **kwargs: Any,+ ) -> str:+ r"""Convert speech audio to text.++ Args:+ audio_file_path (str): The audio file path, supporting one of+ these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or+ webm.+ translate_into_english (bool, optional): Whether to translate the+ speech into English. Defaults to `False`.+ **kwargs (Any): Extra keyword arguments passed to the+ Speech-to-Text (STT) API.++ Returns:+ str: The output text.++ Raises:+ ValueError: If the audio file format is not supported.+ Exception: If there's an error during the STT API call.+ """+ supported_formats = [+ "flac",+ "mp3",+ "mp4",+ "mpeg",+ "mpga",+ "m4a",+ "ogg",+ "wav",+ "webm",+ ]+ file_format = audio_file_path.split(".")[-1].lower()++ if file_format not in supported_formats:+ raise ValueError(f"Unsupported audio file format: {file_format}")+ try:+ if os.path.getsize(audio_file_path) > 24 * 1024 * 1024:+ # Split audio into chunks+ audio_chunks = self._split_audio(audio_file_path)+ texts = []+ for chunk_path in audio_chunks:+ audio_data = open(chunk_path, "rb")+ if translate_into_english:+ translation = self._client.audio.translations.create(+ model="whisper-1", file=audio_data, **kwargs+ )+ texts.append(translation.text)+ else:+ transcription = (+ self._client.audio.transcriptions.create(+ model="whisper-1", file=audio_data, **kwargs+ )+ )+ texts.append(transcription.text)+ os.remove(chunk_path) # Delete temporary chunk file+ return " ".join(texts)+ else:+ # Process the entire audio file+ audio_data = open(audio_file_path, "rb")++ if translate_into_english:+ translation = self._client.audio.translations.create(+ model="whisper-1", file=audio_data, **kwargs+ )+ return translation.text+ else:+ transcription = self._client.audio.transcriptions.create(+ model="whisper-1", file=audio_data, **kwargs+ )+ return transcription.text+ except Exception as e:+ raise Exception("Error during STT API call") from ediff --git a/camel/types/__init__.py b/camel/types/__init__.pyindex cb7bbe9a4..065209df2 100644--- a/camel/types/__init__.py+++ b/camel/types/__init__.py@@ -12,6 +12,7 @@# limitations under the License.# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========from .enums import (+ AudioModelType,EmbeddingModelType,ModelType,OpenAIBackendRole,@@ -23,6 +24,7 @@ from .enums import (TaskType,TerminationMode,VectorDistance,+ VoiceType,)from .openai_types import (ChatCompletion,@@ -59,4 +61,6 @@ __all__ = ['OpenAIImageType','OpenAIImageDetailType','OpenAPIName',+ 'AudioModelType',+ 'VoiceType',]diff --git a/camel/types/enums.py b/camel/types/enums.pyindex f4cd7ff1e..1a67693b0 100644--- a/camel/types/enums.py+++ b/camel/types/enums.py@@ -253,3 +253,38 @@ class OpenAPIName(Enum):COURSERA = "coursera"KLARNA = "klarna"SPEAK = "speak"+++class AudioModelType(Enum):+ TTS_1 = "tts-1"+ TTS_1_HD = "tts-1-hd"++ @property+ def is_openai(self) -> bool:+ r"""Returns whether this type of audio models is an OpenAI-released+ model."""+ return self in {+ AudioModelType.TTS_1,+ AudioModelType.TTS_1_HD,+ }+++class VoiceType(Enum):+ ALLOY = "alloy"+ ECHO = "echo"+ FABLE = "fable"+ ONYX = "onyx"+ NOVA = "nova"+ SHIMMER = "shimmer"++ @property+ def is_openai(self) -> bool:+ r"""Returns whether this type of voice is an OpenAI-released voice."""+ return self in {+ VoiceType.ALLOY,+ VoiceType.ECHO,+ VoiceType.FABLE,+ VoiceType.ONYX,+ VoiceType.NOVA,+ VoiceType.SHIMMER,+ }diff --git a/examples/openai_audio_models/openai_audio_models.py b/examples/openai_audio_models/openai_audio_models.pynew file mode 100644index 000000000..b7051b2c3--- /dev/null+++ b/examples/openai_audio_models/openai_audio_models.py@@ -0,0 +1,38 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========++from camel.models import OpenAIAudioModels++audio_models = OpenAIAudioModels()++# Set example input+input = """CAMEL-AI.org is an open-source community dedicated to the study of autonomous and communicative agents. We believe that studying these agents on a large scale offers valuable insights into their behaviors, capabilities, and potential risks. To facilitate research in this field, we provide, implement, and support various types of agents, tasks, prompts, models, datasets, and simulated environments.++Join us via Slack, Discord, or WeChat in pushing the boundaries of building AI Society."""++# Set example local path to store the file+storage_path = "examples/openai_audio_models/example_audio.mp3"++# Convert the example input into audio and store it locally+audio_models.text_to_speech(input=input, storage_path=storage_path)++# Convert the generated audio file into text+text_output = audio_models.speech_to_text(audio_file_path=storage_path)++print(text_output)+"""+===============================================================================+CamelAI.org is an open-source community dedicated to the study of autonomous and communicative agents. We believe that studying these agents on a large scale offers valuable insights into their behaviors, capabilities, and potential risks. To facilitate research in this field, we provide, implement, and support various types of agents, tasks, prompts, models, datasets, and simulated environments. Join us via Slack, Discord, or WeChat in pushing the boundaries of building AI society.+===============================================================================+"""diff --git a/poetry.lock b/poetry.lockindex 44480d2ed..604e8bea5 100644--- a/poetry.lock+++ b/poetry.lock@@ -252,13 +252,13 @@ files = [[[package]]name = "anyio"-version = "4.3.0"+version = "4.4.0"description = "High level compatibility layer for multiple asynchronous event loop implementations"optional = falsepython-versions = ">=3.8"files = [- {file = "anyio-4.3.0-py3-none-any.whl", hash = "sha256:048e05d0f6caeed70d731f3db756d35dcc1f35747c8c403364a8332c630441b8"},- {file = "anyio-4.3.0.tar.gz", hash = "sha256:f75253795a87df48568485fd18cdd2a3fa5c4f7c5be8e5e36637733fce06fed6"},+ {file = "anyio-4.4.0-py3-none-any.whl", hash = "sha256:c1b2d8f46a8a812513012e1107cb0e68c17159a7a594208005a57dc776e1bdc7"},+ {file = "anyio-4.4.0.tar.gz", hash = "sha256:5aadc6a1bbb7cdb0bede386cac5e2940f5e2ff3aa20277e991cf028e0585ce94"},][package.dependencies]@@ -710,63 +710,63 @@ test-no-images = ["pytest", "pytest-cov", "wurlitzer"][[package]]name = "coverage"-version = "7.5.1"+version = "7.5.2"description = "Code coverage measurement for Python"optional = falsepython-versions = ">=3.8"files = [- {file = "coverage-7.5.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:c0884920835a033b78d1c73b6d3bbcda8161a900f38a488829a83982925f6c2e"},- {file = "coverage-7.5.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:39afcd3d4339329c5f58de48a52f6e4e50f6578dd6099961cf22228feb25f38f"},- {file = "coverage-7.5.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4a7b0ceee8147444347da6a66be737c9d78f3353b0681715b668b72e79203e4a"},- {file = "coverage-7.5.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:4a9ca3f2fae0088c3c71d743d85404cec8df9be818a005ea065495bedc33da35"},- {file = "coverage-7.5.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5fd215c0c7d7aab005221608a3c2b46f58c0285a819565887ee0b718c052aa4e"},- {file = "coverage-7.5.1-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:4bf0655ab60d754491004a5efd7f9cccefcc1081a74c9ef2da4735d6ee4a6223"},- {file = "coverage-7.5.1-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:61c4bf1ba021817de12b813338c9be9f0ad5b1e781b9b340a6d29fc13e7c1b5e"},- {file = "coverage-7.5.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:db66fc317a046556a96b453a58eced5024af4582a8dbdc0c23ca4dbc0d5b3146"},- {file = "coverage-7.5.1-cp310-cp310-win32.whl", hash = "sha256:b016ea6b959d3b9556cb401c55a37547135a587db0115635a443b2ce8f1c7228"},- {file = "coverage-7.5.1-cp310-cp310-win_amd64.whl", hash = "sha256:df4e745a81c110e7446b1cc8131bf986157770fa405fe90e15e850aaf7619bc8"},- {file = "coverage-7.5.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:796a79f63eca8814ca3317a1ea443645c9ff0d18b188de470ed7ccd45ae79428"},- {file = "coverage-7.5.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:4fc84a37bfd98db31beae3c2748811a3fa72bf2007ff7902f68746d9757f3746"},- {file = "coverage-7.5.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6175d1a0559986c6ee3f7fccfc4a90ecd12ba0a383dcc2da30c2b9918d67d8a3"},- {file = "coverage-7.5.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1fc81d5878cd6274ce971e0a3a18a8803c3fe25457165314271cf78e3aae3aa2"},- {file = "coverage-7.5.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:556cf1a7cbc8028cb60e1ff0be806be2eded2daf8129b8811c63e2b9a6c43bca"},- {file = "coverage-7.5.1-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:9981706d300c18d8b220995ad22627647be11a4276721c10911e0e9fa44c83e8"},- {file = "coverage-7.5.1-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:d7fed867ee50edf1a0b4a11e8e5d0895150e572af1cd6d315d557758bfa9c057"},- {file = "coverage-7.5.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:ef48e2707fb320c8f139424a596f5b69955a85b178f15af261bab871873bb987"},- {file = "coverage-7.5.1-cp311-cp311-win32.whl", hash = "sha256:9314d5678dcc665330df5b69c1e726a0e49b27df0461c08ca12674bcc19ef136"},- {file = "coverage-7.5.1-cp311-cp311-win_amd64.whl", hash = "sha256:5fa567e99765fe98f4e7d7394ce623e794d7cabb170f2ca2ac5a4174437e90dd"},- {file = "coverage-7.5.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:b6cf3764c030e5338e7f61f95bd21147963cf6aa16e09d2f74f1fa52013c1206"},- {file = "coverage-7.5.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:2ec92012fefebee89a6b9c79bc39051a6cb3891d562b9270ab10ecfdadbc0c34"},- {file = "coverage-7.5.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:16db7f26000a07efcf6aea00316f6ac57e7d9a96501e990a36f40c965ec7a95d"},- {file = "coverage-7.5.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:beccf7b8a10b09c4ae543582c1319c6df47d78fd732f854ac68d518ee1fb97fa"},- {file = "coverage-7.5.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8748731ad392d736cc9ccac03c9845b13bb07d020a33423fa5b3a36521ac6e4e"},- {file = "coverage-7.5.1-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:7352b9161b33fd0b643ccd1f21f3a3908daaddf414f1c6cb9d3a2fd618bf2572"},- {file = "coverage-7.5.1-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:7a588d39e0925f6a2bff87154752481273cdb1736270642aeb3635cb9b4cad07"},- {file = "coverage-7.5.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:68f962d9b72ce69ea8621f57551b2fa9c70509af757ee3b8105d4f51b92b41a7"},- {file = "coverage-7.5.1-cp312-cp312-win32.whl", hash = "sha256:f152cbf5b88aaeb836127d920dd0f5e7edff5a66f10c079157306c4343d86c19"},- {file = "coverage-7.5.1-cp312-cp312-win_amd64.whl", hash = "sha256:5a5740d1fb60ddf268a3811bcd353de34eb56dc24e8f52a7f05ee513b2d4f596"},- {file = "coverage-7.5.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:e2213def81a50519d7cc56ed643c9e93e0247f5bbe0d1247d15fa520814a7cd7"},- {file = "coverage-7.5.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:5037f8fcc2a95b1f0e80585bd9d1ec31068a9bcb157d9750a172836e98bc7a90"},- {file = "coverage-7.5.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5c3721c2c9e4c4953a41a26c14f4cef64330392a6d2d675c8b1db3b645e31f0e"},- {file = "coverage-7.5.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ca498687ca46a62ae590253fba634a1fe9836bc56f626852fb2720f334c9e4e5"},- {file = "coverage-7.5.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0cdcbc320b14c3e5877ee79e649677cb7d89ef588852e9583e6b24c2e5072661"},- {file = "coverage-7.5.1-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:57e0204b5b745594e5bc14b9b50006da722827f0b8c776949f1135677e88d0b8"},- {file = "coverage-7.5.1-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:8fe7502616b67b234482c3ce276ff26f39ffe88adca2acf0261df4b8454668b4"},- {file = "coverage-7.5.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:9e78295f4144f9dacfed4f92935fbe1780021247c2fabf73a819b17f0ccfff8d"},- {file = "coverage-7.5.1-cp38-cp38-win32.whl", hash = "sha256:1434e088b41594baa71188a17533083eabf5609e8e72f16ce8c186001e6b8c41"},- {file = "coverage-7.5.1-cp38-cp38-win_amd64.whl", hash = "sha256:0646599e9b139988b63704d704af8e8df7fa4cbc4a1f33df69d97f36cb0a38de"},- {file = "coverage-7.5.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:4cc37def103a2725bc672f84bd939a6fe4522310503207aae4d56351644682f1"},- {file = "coverage-7.5.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:fc0b4d8bfeabd25ea75e94632f5b6e047eef8adaed0c2161ada1e922e7f7cece"},- {file = "coverage-7.5.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0d0a0f5e06881ecedfe6f3dd2f56dcb057b6dbeb3327fd32d4b12854df36bf26"},- {file = "coverage-7.5.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:9735317685ba6ec7e3754798c8871c2f49aa5e687cc794a0b1d284b2389d1bd5"},- {file = "coverage-7.5.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d21918e9ef11edf36764b93101e2ae8cc82aa5efdc7c5a4e9c6c35a48496d601"},- {file = "coverage-7.5.1-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:c3e757949f268364b96ca894b4c342b41dc6f8f8b66c37878aacef5930db61be"},- {file = "coverage-7.5.1-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:79afb6197e2f7f60c4824dd4b2d4c2ec5801ceb6ba9ce5d2c3080e5660d51a4f"},- {file = "coverage-7.5.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:d1d0d98d95dd18fe29dc66808e1accf59f037d5716f86a501fc0256455219668"},- {file = "coverage-7.5.1-cp39-cp39-win32.whl", hash = "sha256:1cc0fe9b0b3a8364093c53b0b4c0c2dd4bb23acbec4c9240b5f284095ccf7981"},- {file = "coverage-7.5.1-cp39-cp39-win_amd64.whl", hash = "sha256:dde0070c40ea8bb3641e811c1cfbf18e265d024deff6de52c5950677a8fb1e0f"},- {file = "coverage-7.5.1-pp38.pp39.pp310-none-any.whl", hash = "sha256:6537e7c10cc47c595828b8a8be04c72144725c383c4702703ff4e42e44577312"},- {file = "coverage-7.5.1.tar.gz", hash = "sha256:54de9ef3a9da981f7af93eafde4ede199e0846cd819eb27c88e2b712aae9708c"},+ {file = "coverage-7.5.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:554c7327bf0fd688050348e22db7c8e163fb7219f3ecdd4732d7ed606b417263"},+ {file = "coverage-7.5.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:d0305e02e40c7cfea5d08d6368576537a74c0eea62b77633179748d3519d6705"},+ {file = "coverage-7.5.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:829fb55ad437d757c70d5b1c51cfda9377f31506a0a3f3ac282bc6a387d6a5f1"},+ {file = "coverage-7.5.2-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:894b1acded706f1407a662d08e026bfd0ff1e59e9bd32062fea9d862564cfb65"},+ {file = "coverage-7.5.2-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:fe76d6dee5e4febefa83998b17926df3a04e5089e3d2b1688c74a9157798d7a2"},+ {file = "coverage-7.5.2-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:c7ebf2a37e4f5fea3c1a11e1f47cea7d75d0f2d8ef69635ddbd5c927083211fc"},+ {file = "coverage-7.5.2-cp310-cp310-musllinux_1_1_i686.whl", hash = "sha256:20e611fc36e1a0fc7bbf957ef9c635c8807d71fbe5643e51b2769b3cc0fb0b51"},+ {file = "coverage-7.5.2-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:7c5c5b7ae2763533152880d5b5b451acbc1089ade2336b710a24b2b0f5239d20"},+ {file = "coverage-7.5.2-cp310-cp310-win32.whl", hash = "sha256:1e4225990a87df898e40ca31c9e830c15c2c53b1d33df592bc8ef314d71f0281"},+ {file = "coverage-7.5.2-cp310-cp310-win_amd64.whl", hash = "sha256:976cd92d9420e6e2aa6ce6a9d61f2b490e07cb468968adf371546b33b829284b"},+ {file = "coverage-7.5.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:5997d418c219dcd4dcba64e50671cca849aaf0dac3d7a2eeeb7d651a5bd735b8"},+ {file = "coverage-7.5.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:ec27e93bbf5976f0465e8936f02eb5add99bbe4e4e7b233607e4d7622912d68d"},+ {file = "coverage-7.5.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1f11f98753800eb1ec872562a398081f6695f91cd01ce39819e36621003ec52a"},+ {file = "coverage-7.5.2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6e34680049eecb30b6498784c9637c1c74277dcb1db75649a152f8004fbd6646"},+ {file = "coverage-7.5.2-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3e12536446ad4527ac8ed91d8a607813085683bcce27af69e3b31cd72b3c5960"},+ {file = "coverage-7.5.2-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:3d3f7744b8a8079d69af69d512e5abed4fb473057625588ce126088e50d05493"},+ {file = "coverage-7.5.2-cp311-cp311-musllinux_1_1_i686.whl", hash = "sha256:431a3917e32223fcdb90b79fe60185864a9109631ebc05f6c5aa03781a00b513"},+ {file = "coverage-7.5.2-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:a7c6574225f34ce45466f04751d957b5c5e6b69fca9351db017c9249786172ce"},+ {file = "coverage-7.5.2-cp311-cp311-win32.whl", hash = "sha256:2b144d142ec9987276aeff1326edbc0df8ba4afbd7232f0ca10ad57a115e95b6"},+ {file = "coverage-7.5.2-cp311-cp311-win_amd64.whl", hash = "sha256:900532713115ac58bc3491b9d2b52704a05ed408ba0918d57fd72c94bc47fba1"},+ {file = "coverage-7.5.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:9a42970ce74c88bdf144df11c52c5cf4ad610d860de87c0883385a1c9d9fa4ab"},+ {file = "coverage-7.5.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:26716a1118c6ce2188283b4b60a898c3be29b480acbd0a91446ced4fe4e780d8"},+ {file = "coverage-7.5.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:60b66b0363c5a2a79fba3d1cd7430c25bbd92c923d031cae906bdcb6e054d9a2"},+ {file = "coverage-7.5.2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e5d22eba19273b2069e4efeff88c897a26bdc64633cbe0357a198f92dca94268"},+ {file = "coverage-7.5.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3bb5b92a0ab3d22dfdbfe845e2fef92717b067bdf41a5b68c7e3e857c0cff1a4"},+ {file = "coverage-7.5.2-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:1aef719b6559b521ae913ddeb38f5048c6d1a3d366865e8b320270b7bc4693c2"},+ {file = "coverage-7.5.2-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:8809c0ea0e8454f756e3bd5c36d04dddf222989216788a25bfd6724bfcee342c"},+ {file = "coverage-7.5.2-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:1acc2e2ef098a1d4bf535758085f508097316d738101a97c3f996bccba963ea5"},+ {file = "coverage-7.5.2-cp312-cp312-win32.whl", hash = "sha256:97de509043d3f0f2b2cd171bdccf408f175c7f7a99d36d566b1ae4dd84107985"},+ {file = "coverage-7.5.2-cp312-cp312-win_amd64.whl", hash = "sha256:8941e35a0e991a7a20a1fa3e3182f82abe357211f2c335a9e6007067c3392fcf"},+ {file = "coverage-7.5.2-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:5662bf0f6fb6757f5c2d6279c541a5af55a39772c2362ed0920b27e3ce0e21f7"},+ {file = "coverage-7.5.2-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:3d9c62cff2ffb4c2a95328488fd7aa96a7a4b34873150650fe76b19c08c9c792"},+ {file = "coverage-7.5.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:74eeaa13e8200ad72fca9c5f37395fb310915cec6f1682b21375e84fd9770e84"},+ {file = "coverage-7.5.2-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1f29bf497d51a5077994b265e976d78b09d9d0dff6ca5763dbb4804534a5d380"},+ {file = "coverage-7.5.2-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1f96aa94739593ae0707eda9813ce363a0a0374a810ae0eced383340fc4a1f73"},+ {file = "coverage-7.5.2-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:51b6cee539168a912b4b3b040e4042b9e2c9a7ad9c8546c09e4eaeff3eacba6b"},+ {file = "coverage-7.5.2-cp38-cp38-musllinux_1_1_i686.whl", hash = "sha256:59a75e6aa5c25b50b5a1499f9718f2edff54257f545718c4fb100f48d570ead4"},+ {file = "coverage-7.5.2-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:29da75ce20cb0a26d60e22658dd3230713c6c05a3465dd8ad040ffc991aea318"},+ {file = "coverage-7.5.2-cp38-cp38-win32.whl", hash = "sha256:23f2f16958b16152b43a39a5ecf4705757ddd284b3b17a77da3a62aef9c057ef"},+ {file = "coverage-7.5.2-cp38-cp38-win_amd64.whl", hash = "sha256:9e41c94035e5cdb362beed681b58a707e8dc29ea446ea1713d92afeded9d1ddd"},+ {file = "coverage-7.5.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:06d96b9b19bbe7f049c2be3c4f9e06737ec6d8ef8933c7c3a4c557ef07936e46"},+ {file = "coverage-7.5.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:878243e1206828908a6b4a9ca7b1aa8bee9eb129bf7186fc381d2646f4524ce9"},+ {file = "coverage-7.5.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:482df956b055d3009d10fce81af6ffab28215d7ed6ad4a15e5c8e67cb7c5251c"},+ {file = "coverage-7.5.2-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:a35c97af60a5492e9e89f8b7153fe24eadfd61cb3a2fb600df1a25b5dab34b7e"},+ {file = "coverage-7.5.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:24bb4c7859a3f757a116521d4d3a8a82befad56ea1bdacd17d6aafd113b0071e"},+ {file = "coverage-7.5.2-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:e1046aab24c48c694f0793f669ac49ea68acde6a0798ac5388abe0a5615b5ec8"},+ {file = "coverage-7.5.2-cp39-cp39-musllinux_1_1_i686.whl", hash = "sha256:448ec61ea9ea7916d5579939362509145caaecf03161f6f13e366aebb692a631"},+ {file = "coverage-7.5.2-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:4a00bd5ba8f1a4114720bef283cf31583d6cb1c510ce890a6da6c4268f0070b7"},+ {file = "coverage-7.5.2-cp39-cp39-win32.whl", hash = "sha256:9f805481d5eff2a96bac4da1570ef662bf970f9a16580dc2c169c8c3183fa02b"},+ {file = "coverage-7.5.2-cp39-cp39-win_amd64.whl", hash = "sha256:2c79f058e7bec26b5295d53b8c39ecb623448c74ccc8378631f5cb5c16a7e02c"},+ {file = "coverage-7.5.2-pp38.pp39.pp310-none-any.whl", hash = "sha256:40dbb8e7727560fe8ab65efcddfec1ae25f30ef02e2f2e5d78cfb52a66781ec5"},+ {file = "coverage-7.5.2.tar.gz", hash = "sha256:13017a63b0e499c59b5ba94a8542fb62864ba3016127d1e4ef30d354fc2b00e9"},][package.dependencies]@@ -7048,13 +7048,13 @@ multidict = ">=4.0"[[package]]name = "zipp"-version = "3.18.2"+version = "3.19.0"description = "Backport of pathlib-compatible object wrapper for zip files"optional = falsepython-versions = ">=3.8"files = [- {file = "zipp-3.18.2-py3-none-any.whl", hash = "sha256:dce197b859eb796242b0622af1b8beb0a722d52aa2f57133ead08edd5bf5374e"},- {file = "zipp-3.18.2.tar.gz", hash = "sha256:6278d9ddbcfb1f1089a88fde84481528b07b0e10474e09dcfe53dad4069fa059"},+ {file = "zipp-3.19.0-py3-none-any.whl", hash = "sha256:96dc6ad62f1441bcaccef23b274ec471518daf4fbbc580341204936a5a3dddec"},+ {file = "zipp-3.19.0.tar.gz", hash = "sha256:952df858fb3164426c976d9338d3961e8e8b3758e2e059e0f754b8c4262625ee"},][package.extras]@@ -7062,16 +7062,16 @@ docs = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linktesting = ["big-O", "jaraco.functools", "jaraco.itertools", "jaraco.test", "more-itertools", "pytest (>=6,!=8.1.*)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-ignore-flaky", "pytest-mypy", "pytest-ruff (>=0.2.1)"][extras]-all = ["PyMuPDF", "accelerate", "beautifulsoup4", "cohere", "datasets", "diffusers", "docx2txt", "googlemaps", "neo4j", "openapi-spec-validator", "opencv-python", "prance", "pygithub", "pymilvus", "pyowm", "qdrant-client", "rank-bm25", "requests_oauthlib", "sentence-transformers", "sentencepiece", "soundfile", "torch", "transformers", "unstructured", "wikipedia", "wolframalpha"]+all = ["PyMuPDF", "accelerate", "beautifulsoup4", "cohere", "datasets", "diffusers", "docx2txt", "googlemaps", "neo4j", "openapi-spec-validator", "opencv-python", "prance", "pydub", "pygithub", "pymilvus", "pyowm", "qdrant-client", "rank-bm25", "requests_oauthlib", "sentence-transformers", "sentencepiece", "soundfile", "torch", "transformers", "unstructured", "wikipedia", "wolframalpha"]encoders = ["sentence-transformers"]graph-storages = ["neo4j"]huggingface-agent = ["accelerate", "datasets", "diffusers", "opencv-python", "sentencepiece", "soundfile", "torch", "transformers"]retrievers = ["cohere", "rank-bm25"]test = ["mock", "pytest"]-tools = ["PyMuPDF", "beautifulsoup4", "docx2txt", "googlemaps", "openapi-spec-validator", "prance", "pygithub", "pyowm", "requests_oauthlib", "unstructured", "wikipedia", "wolframalpha"]+tools = ["PyMuPDF", "beautifulsoup4", "docx2txt", "googlemaps", "openapi-spec-validator", "prance", "pydub", "pygithub", "pyowm", "requests_oauthlib", "unstructured", "wikipedia", "wolframalpha"]vector-databases = ["pymilvus", "qdrant-client"][metadata]lock-version = "2.0"python-versions = ">=3.8.1,<3.12"-content-hash = "dedf1730369d7daa31fcfbeb99e4140a241840a1671587e26d9e603817d0639a"+content-hash = "8de82f2fd542a53ff7185ddaaf92f33a227971118c18b7d281d9aff9dbcf2250"diff --git a/pyproject.toml b/pyproject.tomlindex 894828ad0..72994b9eb 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -61,6 +61,7 @@ requests_oauthlib = { version = "^1.3.1", optional = true }prance = { version = "^23.6.21.0", optional = true }openapi-spec-validator = { version = "^0.7.1", optional = true }unstructured = { extras = ["all-docs"], version = "^0.10.30", optional = true }+pydub = { version = "^0.25.1", optional = true }pygithub = { version = "^2.3.0", optional = true }# encoders@@ -112,6 +113,7 @@ tools = ["prance","openapi-spec-validator","unstructured",+ "pydub","pygithub",]@@ -152,6 +154,7 @@ all = ["prance","openapi-spec-validator","unstructured",+ "pydub",# vector-database"qdrant-client","pymilvus",@@ -270,6 +273,7 @@ module = ["cohere","sentence_transformers.*","pymilvus",+ "pydub","pygithub"]ignore_missing_imports = truediff --git a/test/models/test_openai_audio_models.py b/test/models/test_openai_audio_models.pynew file mode 100644index 000000000..3a5d24bc5--- /dev/null+++ b/test/models/test_openai_audio_models.py@@ -0,0 +1,85 @@+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+# Licensed under the Apache License, Version 2.0 (the āLicenseā);+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an āAS ISā BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========+import os+import tempfile+from unittest.mock import ANY, Mock, patch++from camel.models import OpenAIAudioModels+from camel.types import AudioModelType, VoiceType+++def test_text_to_speech():+ openai_audio = OpenAIAudioModels()+ input_text = "Hello, world!"+ mock_response = Mock()+ mock_response.text = "Mock audio response"+ mock_client = Mock()+ mock_client.audio.speech.create.return_value = mock_response+ openai_audio._client = mock_client++ response = openai_audio.text_to_speech(input_text)++ assert response.text == "Mock audio response"+ mock_client.audio.speech.create.assert_called_once_with(+ model=AudioModelType.TTS_1.value,+ voice=VoiceType.ALLOY.value,+ input=input_text,+ )+++def test_speech_to_text():+ openai_audio = OpenAIAudioModels()+ # Create a temporary audio file+ with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_file:+ temp_file.write(b'Test audio data')+ temp_file_path = temp_file.name++ mock_response = Mock()+ mock_response.text = "Mock transcription response"+ mock_client = Mock()+ mock_client.audio.transcriptions.create.return_value = mock_response+ openai_audio._client = mock_client++ response = openai_audio.speech_to_text(temp_file_path)++ assert response == "Mock transcription response"+ mock_client.audio.transcriptions.create.assert_called_once_with(+ model="whisper-1", file=ANY+ )++ # Clean up temporary file+ os.remove(temp_file_path)+++@patch(+ "camel.models.openai_audio_models.os.path.getsize", return_value=1024+) # Mocking the file size+@patch(+ "camel.models.openai_audio_models.open", new_callable=Mock+) # Mocking the open function+def test_speech_to_text_large_audio(mock_open, mock_getsize):+ openai_audio = OpenAIAudioModels()+ audio_file_path = "large_audio.wav"+ mock_split_audio = Mock(return_value=["chunk1.wav", "chunk2.wav"])+ openai_audio._split_audio = mock_split_audio+ mock_response = Mock()+ mock_response.text = "Mock transcription response"+ mock_client = Mock()+ mock_client.audio.transcriptions.create.return_value = mock_response+ openai_audio._client = mock_client++ response = openai_audio.speech_to_text(audio_file_path)++ assert response == "Mock transcription response"+ assert mock_client.audio.transcriptions.create.call_count == 1