File size: 2,300 Bytes
4e60070
fe87a10
 
 
 
79b4f84
 
 
 
fe87a10
79b4f84
 
a507612
2c74f1f
1a92e57
2c74f1f
 
79b4f84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe87a10
79b4f84
 
 
fe87a10
 
2c74f1f
 
 
1a92e57
 
fe87a10
2c74f1f
fe87a10
 
 
2c74f1f
 
fe87a10
2c74f1f
fe87a10
2c74f1f
fe87a10
 
 
 
2c74f1f
 
fe87a10
1a92e57
 
fe87a10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import torch
from PIL import Image
from transformers import AutoModel, AutoTokenizer
import gradio as gr

# Verificar se a GPU está disponível
device = "cuda" if torch.cuda.is_available() else "cpu"
dtype = torch.bfloat16 if device == "cuda" and torch.cuda.is_bf16_supported() else torch.float16

# Carregar o modelo e o tokenizer
model = AutoModel.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True, torch_dtype=dtype)
model = model.to(device=device, dtype=dtype)  # Ajuste para o dispositivo e tipo de dados adequados
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True)
model.eval()

# Função para processar a imagem e a pergunta
def chat_with_model(image, question):
    try:
        # Converter a imagem para RGB (se necessário)
        if isinstance(image, str):
            image = Image.open(image).convert('RGB')
        else:
            image = image.convert('RGB')

        # Preparar a mensagem para o modelo
        msgs = [{'role': 'user', 'content': question}]

        # Gerar resposta do modelo
        res, context, _ = model.chat(
            image=image,
            msgs=msgs,
            context=None,
            tokenizer=tokenizer,
            sampling=True,
            temperature=0.7
        )

        return res
    except Exception as e:
        return f"Erro ao processar a imagem ou pergunta: {str(e)}"

# Interface Gradio
def gradio_interface(image, question):
    response = chat_with_model(image, question)
    return response

# Criar a interface Gradio
with gr.Blocks() as demo:
    gr.Markdown("# MiniCPM-V Chat with Images")
    gr.Markdown("Envie uma imagem e faça perguntas sobre ela.")

    with gr.Row():
        image_input = gr.Image(label="Upload Image", type="pil")  # Campo para upload de imagem
        question_input = gr.Textbox(label="Your Question", placeholder="What is in the image?")  # Campo para a pergunta
    
    output_text = gr.Textbox(label="Model Response", interactive=False)  # Campo para exibir a resposta
    
    submit_button = gr.Button("Submit")  # Botão para enviar
    
    # Ação ao clicar no botão
    submit_button.click(
        fn=gradio_interface,
        inputs=[image_input, question_input],
        outputs=output_text
    )

# Iniciar a interface
demo.launch()