Some cleanup, bringing azure ai service up to date

This commit is contained in:
Moishe Lettvin
2023-12-29 10:12:24 -05:00
parent d1b9a77d86
commit 7c9ecb1a1d
4 changed files with 42 additions and 30 deletions

View File

@@ -14,13 +14,13 @@ from PIL import Image
from azure.cognitiveservices.speech import SpeechSynthesizer, SpeechConfig, ResultReason, CancellationReason
class AzureTTSService(TTSService):
def __init__(self):
def __init__(self, speech_key=None, speech_region=None):
super().__init__()
self.speech_key = os.getenv("AZURE_SPEECH_SERVICE_KEY")
self.speech_region = os.getenv("AZURE_SPEECH_SERVICE_REGION")
speech_key = speech_key or os.getenv("AZURE_SPEECH_SERVICE_KEY")
speech_region = speech_region or os.getenv("AZURE_SPEECH_SERVICE_REGION")
self.speech_config = SpeechConfig(subscription=self.speech_key, region=self.speech_region)
self.speech_config = SpeechConfig(subscription=speech_key, region=speech_region)
self.speech_synthesizer = SpeechSynthesizer(speech_config=self.speech_config, audio_config=None)
def run_tts(self, sentence) -> Generator[bytes, None, None]:
@@ -46,19 +46,23 @@ class AzureTTSService(TTSService):
self.logger.info("Error details: {}".format(cancellation_details.error_details))
class AzureLLMService(LLMService):
def __init__(self):
def __init__(self, api_key=None, azure_endpoint=None, api_version=None, model=None):
super().__init__()
api_key = api_key or os.getenv("AZURE_CHATGPT_KEY")
azure_endpoint = azure_endpoint or os.getenv("AZURE_CHATGPT_ENDPOINT")
api_version = api_version or "2023-12-01-preview"
self.client = AzureOpenAI(
api_key=os.getenv("AZURE_CHATGPT_KEY"),
azure_endpoint=os.getenv("AZURE_CHATGPT_ENDPOINT"),
api_version="2023-12-01-preview",
api_key=api_key,
azure_endpoint=azure_endpoint,
api_version=api_version,
)
self.model = model or os.getenv("AZURE_CHATGPT_DEPLOYMENT_ID")
def get_response(self, messages, stream):
return self.client.chat.completions.create(
stream=stream,
messages=messages,
model=os.getenv("AZURE_CHATGPT_DEPLOYMENT_ID"),
model=self.model,
)
def run_llm_async(self, messages) -> Generator[str, None, None]:
@@ -89,16 +93,26 @@ class AzureLLMService(LLMService):
class AzureImageGenService(ImageGenService):
def __init__(self, api_key=None, azure_endpoint=None, api_version=None, model=None):
super().__init__()
api_key = api_key or os.getenv("AZURE_DALLE_KEY")
azure_endpoint = azure_endpoint or os.getenv("AZURE_DALLE_ENDPOINT")
api_version = api_version or "2023-12-01-preview"
self.model = model or os.getenv("AZURE_DALLE_DEPLOYMENT_ID")
self.client = AzureOpenAI(
api_key=api_key,
azure_endpoint=azure_endpoint,
api_version=api_version,
)
def run_image_gen(self, sentence) -> tuple[str, Image.Image]:
self.logger.info("Generating azure image", sentence)
image = OpenAI().images.generate(
api_type="azure",
api_version="2023-06-01-preview",
api_key=os.getenv("AZURE_DALLE_KEY"),
api_base=os.getenv("AZURE_DALLE_ENDPOINT"),
deployment_id=os.getenv("AZURE_DALLE_DEPLOYMENT_ID"),
prompt=f"{sentence} in the style of {self.image_style}",
image = self.client.images.generate(
model=self.model,
prompt=sentence,
n=1,
size=f"1024x1024",
)