Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import AutoProcessor, BlipForConditionalGeneration, AutoModelForCausalLM, AutoImageProcessor, VisionEncoderDecoderModel, AutoTokenizer | |
import io | |
import base64 | |
# from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel | |
import torch | |
import open_clip | |
import openai | |
from huggingface_hub import hf_hub_download | |
# Carga el modelo de clasificaci贸n de imagen a texto | |
blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large") | |
blip_model_large = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large") | |
# Carga el modelo de texto a voz | |
openai.api_key = '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' | |
model_id = "base" | |
#model_version = "2022-01-01" | |
whisper = openai.Model(model_id=model_id) | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
blip_model_large.to(device) | |
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False): | |
inputs = processor(images=image, return_tensors="pt").to(device) | |
if use_float_16: | |
inputs = inputs.to(torch.float16) | |
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50) | |
if tokenizer is not None: | |
generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
else: | |
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
return generated_caption | |
def generate_caption_coca(model, transform, image): | |
im = transform(image).unsqueeze(0).to(device) | |
with torch.no_grad(), torch.cuda.amp.autocast(): | |
generated = model.generate(im, seq_len=20) | |
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "") | |
def generate_captions(image): | |
caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image) | |
print(caption_blip_large) | |
return caption_blip_large | |
# Define la funci贸n que convierte texto en voz | |
def text_to_speech(text): | |
# Genera el audio utilizando el modelo Whisper | |
response = whisper.generate(prompt=text) | |
print(response) | |
# Extrae el audio del resultado | |
audio = response.choices[0].audio | |
# Codifica el audio en base64 | |
audio_base64 = base64.b64encode(audio).decode("utf-8") | |
# Devuelve el audio como un archivo MP3 | |
return BytesIO(base64.b64decode(audio_base64)) | |
# Define la interfaz de usuario utilizando Gradio | |
inputsImg = [ | |
gr.Image(type="pil", label="Imagen"), | |
] | |
outputs = [ gr.Textbox(label="Caption generated by BLIP-large") ] | |
title = "Clasificaci贸n de imagen a texto y conversi贸n de texto a voz" | |
description = "Carga una imagen y obt茅n una descripci贸n de texto de lo que contiene la imagen, as铆 como un archivo de audio que lee el texto en voz alta." | |
examples = [] | |
interface = gr.Interface(fn=generate_captions, | |
inputs=inputsImg, | |
outputs=outputs, | |
examples=examples, | |
title=title, | |
description=description) | |
interface.launch() |