Spaces:
Runtime error
Runtime error
File size: 1,954 Bytes
4e60070 fe87a10 2c74f1f a507612 2c74f1f 1a92e57 2c74f1f fe87a10 4e60070 fe87a10 2c74f1f fe87a10 2c74f1f fe87a10 2c74f1f fe87a10 2c74f1f 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 |
import torch
from PIL import Image
from transformers import AutoModel, AutoTokenizer
import gradio as gr
# Carregar o modelo e o tokenizer
model = AutoModel.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True, torch_dtype=torch.bfloat16)
model = model.to(device='cuda', dtype=torch.bfloat16) # 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):
# 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
# 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() |