Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,34 +3,41 @@ from PIL import Image
|
|
| 3 |
from transformers import AutoModel, AutoTokenizer
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Carregar o modelo e o tokenizer
|
| 7 |
-
model = AutoModel.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True, torch_dtype=
|
| 8 |
-
model = model.to(device=
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True)
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
# Função para processar a imagem e a pergunta
|
| 13 |
def chat_with_model(image, question):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Interface Gradio
|
| 36 |
def gradio_interface(image, question):
|
|
|
|
| 3 |
from transformers import AutoModel, AutoTokenizer
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Verificar se a GPU está disponível
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
dtype = torch.bfloat16 if device == "cuda" and torch.cuda.is_bf16_supported() else torch.float16
|
| 9 |
+
|
| 10 |
# Carregar o modelo e o tokenizer
|
| 11 |
+
model = AutoModel.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True, torch_dtype=dtype)
|
| 12 |
+
model = model.to(device=device, dtype=dtype) # Ajuste para o dispositivo e tipo de dados adequados
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-V', trust_remote_code=True)
|
| 14 |
model.eval()
|
| 15 |
|
| 16 |
# Função para processar a imagem e a pergunta
|
| 17 |
def chat_with_model(image, question):
|
| 18 |
+
try:
|
| 19 |
+
# Converter a imagem para RGB (se necessário)
|
| 20 |
+
if isinstance(image, str):
|
| 21 |
+
image = Image.open(image).convert('RGB')
|
| 22 |
+
else:
|
| 23 |
+
image = image.convert('RGB')
|
| 24 |
+
|
| 25 |
+
# Preparar a mensagem para o modelo
|
| 26 |
+
msgs = [{'role': 'user', 'content': question}]
|
| 27 |
+
|
| 28 |
+
# Gerar resposta do modelo
|
| 29 |
+
res, context, _ = model.chat(
|
| 30 |
+
image=image,
|
| 31 |
+
msgs=msgs,
|
| 32 |
+
context=None,
|
| 33 |
+
tokenizer=tokenizer,
|
| 34 |
+
sampling=True,
|
| 35 |
+
temperature=0.7
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
return res
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Erro ao processar a imagem ou pergunta: {str(e)}"
|
| 41 |
|
| 42 |
# Interface Gradio
|
| 43 |
def gradio_interface(image, question):
|