Upload 5 files
Browse files- app.py +57 -0
- examples/image1.jpg +0 -0
- examples/image2.jpg +0 -0
- examples/image3.jpg +0 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 6 |
+
|
| 7 |
+
# 1. Carregar o modelo BLIP finetuned
|
| 8 |
+
MODEL_DIR = "Frame_30K_gpu_01_ptt" # Substitua pelo nome correto da pasta do seu modelo
|
| 9 |
+
|
| 10 |
+
# Verifique se o diretório do modelo existe localmente
|
| 11 |
+
|
| 12 |
+
processor = BlipProcessor.from_pretrained(MODEL_DIR)
|
| 13 |
+
model = BlipForConditionalGeneration.from_pretrained(MODEL_DIR)
|
| 14 |
+
|
| 15 |
+
# Mover o modelo para o dispositivo disponível
|
| 16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
model.to(device)
|
| 18 |
+
|
| 19 |
+
def generate_caption(image):
|
| 20 |
+
"""
|
| 21 |
+
Gera uma legenda para a imagem fornecida usando o modelo BLIP finetuned.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
image (PIL.Image.Image): Imagem carregada.
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
str: Legenda gerada.
|
| 28 |
+
"""
|
| 29 |
+
if image is None:
|
| 30 |
+
return "Nenhuma imagem fornecida."
|
| 31 |
+
|
| 32 |
+
# Preprocessar a imagem
|
| 33 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 34 |
+
|
| 35 |
+
# Gerar legenda
|
| 36 |
+
out = model.generate(**inputs)
|
| 37 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 38 |
+
|
| 39 |
+
return caption
|
| 40 |
+
|
| 41 |
+
# Configurar a interface Gradio
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=generate_caption,
|
| 44 |
+
inputs=gr.Image(type="pil", label="Enviar Imagem"),
|
| 45 |
+
outputs=gr.Textbox(label="Legenda Gerada"),
|
| 46 |
+
title="BLIP Português - Geração de Legendas para Imagens",
|
| 47 |
+
description="Envie uma imagem e o modelo BLIP finetuned em português irá gerar uma legenda descritiva para ela.",
|
| 48 |
+
examples=[
|
| 49 |
+
["examples/image1.jpg"],
|
| 50 |
+
["examples/image2.jpg"],
|
| 51 |
+
["examples/image3.jpg"],
|
| 52 |
+
],
|
| 53 |
+
allow_flagging="never"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
iface.launch()
|
examples/image1.jpg
ADDED
|
examples/image2.jpg
ADDED
|
examples/image3.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==1.13.1
|
| 2 |
+
transformers==4.30.0
|
| 3 |
+
gradio==3.38.0
|
| 4 |
+
pillow==9.5.0
|