摘要
在这篇博客中,我们展示了多个深度学习模型,它们可以相互补充,形成一个语音到图像的解决方案。我们建立了稳定的扩散模型,并利用Amazon Transcribe和Amazon Translate来形成我们的解决方案。一旦MP3录音被上传到S3,一个lambda函数将调用Amazon Transcribe来转录录音(语音到文本),然后它将调用Amazon Translate来翻译转录的文本(文本到文本),最后,它将调用部署在SageMaker上的HuggingFace模型,从翻译后的文本中生成图像(文本到图像),并上传到特定的子bucket。
一些结果
概述
人工智能中的文本到图像是根据给定的提示生成图像。它需要一个突出的自然语言处理和图像生成水平。文本破译生成图像是人工智能的一个主要领域,并且仍然是一项正在进行的工作,因为真正的图像收集和处理是昂贵的。
一旦MP3录音被上传到S3,一个lambda函数将调用亚马逊Transcribe来转录录音(语音到文本),然后它将调用亚马逊Translate来翻译转录的文本(文本到文本),最后,它将调用部署在SageMaker上的HuggingFace模型,从翻译后的文本中生成图像(文本到图像),这也被上传到特定子桶中。
稳定扩散
使用的算法是由hugging face提供的稳定扩散,这是一个数据科学平台,提供的工具使用户能够在开源代码和技术的基础上建立、训练和部署ML模型。稳定扩散是一个潜在的扩散模型,是一种生成性神经网络的品种,由稳定AI开发。它是在取自LAION-5B(一个由58.5亿个经过CLIP过滤的图像-文本对组成的数据集)的图像和标题对上训练的。稳定扩散是一个潜在的文本到图像的扩散模型,能够在任何文本输入的情况下生成照片般逼真的图像,培养自主的自由,以产生令人难以置信的图像,使数十亿人在几秒钟内创造出令人惊叹的艺术。
兰姆达函数
Lambda函数的开发是为了调用深度学习模型并补充其反应。在这个lambda中,有三个主要的模型被执行。每个模型的执行代码将在本节后详细展示。
Lambda函数的代码如下:
import json
import boto3
from transcribe import transcribe_mp3
from translate import translate_text
from hf_model import generate_images
from upload2s3 import upload_file
client = boto3.client ( 's3' )
def lambda_handler(event, context):
#parse out the bucket & file name from the event handler
for record in event['Records']:
file_bucket = record['s3']['bucket']['name']
file_name = record['s3']['object']['key']
object_url = 'https://s3.amazonaws.com/{0}/{1}'.format(file_bucket, file_name)
transcribed_text = transcribe_mp3(file_name, object_url)
translated_text = translate_text(transcribed_text)
generated_images = generate_images(translated_text, 2)
for i, img in enumerate(generated_images):
img_name = f'{translated_text.replace(" ", "_").replace(".","")}-{i}.jpeg'
img_path = "/tmp/" + img_name
img.save(img_path)
upload_file(
file_name=img_path,
bucket='', # enter the s3 bucket name
object_name='result/' + img_name
)
return "lambda handled Successfully!"
转录模式
为此,我们使用了Amazon Transcribe。它是一种自动语音识别服务,使用深度学习模型将音频转换为文本。
该代码包括boto3库,它允许我们访问AWS服务,代码如下:
import json, boto3
from urllib.request import urlopen
import time
transcribe = boto3.client('transcribe')
def transcribe_mp3(file_name, object_url):
response = transcribe.start_transcription_job(
TranscriptionJobName=file_name.replace('/','')[:10],
LanguageCode='ar-AE',
MediaFormat='mp3',
Media={
'MediaFileUri': object_url
})
while True :
status = transcribe.get_transcription_job(TranscriptionJobName='audio-rawV')
if status['TranscriptionJob']['TranscriptionJobStatus'] in ['COMPLETED','FAILED']:
break
print('In Progress')
time.sleep(5)
load_url = urlopen(status['TranscriptionJob']['Transcript']['TranscriptFileUri'])
load_json = json.dumps(json.load(load_url))
text = str(json.loads(load_json)['results']['transcripts'][0]['transcript'])
return text
翻译模型
为了使应用程序对世界各地的用户更加多样化,我们使用了亚马逊翻译。这是一个神经机器翻译服务,使用深度学习模型来提供快速和高质量的语言翻译。整合后,用户可以输入任何语言的提示,AWS Translate会根据算法要求将其转化为英语语言。该实施方案使用Boto3库,该库将SageMaker上编写的代码连接到AWS翻译服务。然后,一个输入需要被翻译成英文,最后保存为提示变量。
所用的代码如下:
import boto3, json
translate = boto3.client ('translate')
def translate_text(text):
result = translate.translate_text(
Text = text,
SourceLanguageCode = "auto",
TargetLanguageCode = "en")
prompt = result["TranslatedText"]
return prompt
拥抱脸模型
要部署HuggingFace模型,你可以很容易地只按照Phil Schmid在他的博客中提供的步骤,题为 "Stable Diffusion on Amazon SageMaker" 。一旦模型部署完毕,你可以注意到我们要通过lambda代码调用的SageMaker端点名称。
调用SageMaker端点的代码如下:
import os, io, boto3, json, csv
from io import BytesIO
import base64
from PIL import Image
ENDPOINT_NAME = '' # enter your ENDPOINT_NAME
runtime= boto3.client('sagemaker-runtime')
# helper decoder
def decode_base64_image(image_string):
base64_image = base64.b64decode(image_string)
buffer = BytesIO(base64_image)
return Image.open(buffer)
def generate_images(prompt, num_images_per_prompt):
data = {
"inputs": prompt,
"num_images_per_prompt" : num_images_per_prompt
}
payload = json.dumps(data, indent=2).encode('utf-8')
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
ContentType='application/json',
Body=payload)
response_decoded = json.loads(response['Body'].read().decode())
decoded_images = [decode_base64_image(image) for image in response_decoded["generated_images"]]
return decoded_images