test01 / app.py
EnriqueVega1995's picture
Adde image caption
23e8780 unverified
raw
history blame contribute delete
No virus
1.49 kB
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
import gradio as gr
# Inicialización del procesador y modelo de BLIP
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
def generate_captions(image, text=""):
# Convertir la imagen cargada a PIL Image
raw_image = Image.fromarray(image).convert('RGB')
if text: # Conditional image captioning
inputs = processor(raw_image, text, return_tensors="pt")
else: # Unconditional image captioning
inputs = processor(raw_image, return_tensors="pt")
# Generar subtítulos para la imagen
out = model.generate(**inputs)
caption = processor.decode(out[0], skip_special_tokens=True)
return caption
# Interfaz de Gradio
iface = gr.Interface(
fn=generate_captions,
inputs=[
gr.Image(label="Cargar/Arrastrar Imagen"), # Quitado el argumento 'tool'
gr.Textbox(label="Texto Condicional (opcional)", placeholder="Introduce un texto condicional (opcional)...")
],
outputs=gr.Textbox(label="Subtítulo Generado"),
title="Generador de Subtítulos de Imágenes BLIP",
description="Esta aplicación genera subtítulos para imágenes cargadas. También puedes proporcionar un texto condicional para guiar la generación del subtítulo."
)
if __name__ == "__main__":
iface.launch()