Spaces:
Sleeping
Sleeping
Fernando Cervan commited on
Commit ·
66dbe05
1
Parent(s): 5518628
Salvando alterações
Browse files- app.py +18 -44
- imagem_base64.txt +0 -0
app.py
CHANGED
|
@@ -1,50 +1,24 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import torch
|
| 7 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 8 |
-
|
| 9 |
-
# Usar o Moondream - um modelo multimodal muito pequeno (~1.6B parâmetros)
|
| 10 |
-
model_id = "vikhyatk/moondream1"
|
| 11 |
-
|
| 12 |
-
# Carregar imagem
|
| 13 |
-
imagem = Image.open("cnh-michele-digital.jpeg")
|
| 14 |
-
if imagem.mode != "RGB":
|
| 15 |
-
imagem = imagem.convert("RGB")
|
| 16 |
-
|
| 17 |
-
# Carregar processador e modelo
|
| 18 |
-
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 19 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
-
model_id,
|
| 21 |
-
torch_dtype=torch.float32, # Usar float32 para CPU
|
| 22 |
-
device_map="cpu",
|
| 23 |
-
trust_remote_code=True
|
| 24 |
)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
prompt = "Extraia os seguintes dados do documento: nome, CPF e data de nascimento."
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
text=prompt,
|
| 32 |
-
images=imagem,
|
| 33 |
-
return_tensors="pt"
|
| 34 |
-
).to("cpu")
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
output = model.generate(
|
| 39 |
-
**inputs,
|
| 40 |
-
max_new_tokens=256,
|
| 41 |
-
do_sample=False
|
| 42 |
-
)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
| 1 |
+
from transformers import (
|
| 2 |
+
PaliGemmaProcessor,
|
| 3 |
+
PaliGemmaForConditionalGeneration,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
)
|
| 5 |
+
from transformers.image_utils import load_image
|
| 6 |
+
import torch
|
| 7 |
|
| 8 |
+
model_id = "google/paligemma2-3b-mix-224"
|
|
|
|
| 9 |
|
| 10 |
+
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg"
|
| 11 |
+
image = load_image(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto").eval()
|
| 14 |
+
processor = PaliGemmaProcessor.from_pretrained(model_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
prompt = "describe en"
|
| 17 |
+
model_inputs = processor(text=prompt, images=image, return_tensors="pt").to(torch.bfloat16).to(model.device)
|
| 18 |
+
input_len = model_inputs["input_ids"].shape[-1]
|
| 19 |
|
| 20 |
+
with torch.inference_mode():
|
| 21 |
+
generation = model.generate(**model_inputs, max_new_tokens=100, do_sample=False)
|
| 22 |
+
generation = generation[0][input_len:]
|
| 23 |
+
decoded = processor.decode(generation, skip_special_tokens=True)
|
| 24 |
+
print(decoded)
|
imagem_base64.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|