Upload Example_demo.txt with huggingface_hub
Browse files- Example_demo.txt +30 -0
Example_demo.txt
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForVision2Seq, AutoProcessor
|
| 2 |
+
from peft import PeftModel
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 8 |
+
"unsloth/llava-1.5-7b-hf-bnb-4bit",
|
| 9 |
+
device_map="auto",
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Load and cast adapter
|
| 14 |
+
model = PeftModel.from_pretrained(model, "grohitraj/archive_classification")
|
| 15 |
+
model = model.half() # fix dtype mismatch
|
| 16 |
+
|
| 17 |
+
# Load processor
|
| 18 |
+
processor = AutoProcessor.from_pretrained("grohitraj/archive_classification")
|
| 19 |
+
|
| 20 |
+
# Prepare inputs
|
| 21 |
+
image = Image.open("example.jpg")
|
| 22 |
+
prompt = "<image>\nDescribe about the image for male aged 54:"
|
| 23 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 24 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 25 |
+
|
| 26 |
+
# Generate
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 29 |
+
|
| 30 |
+
print(processor.tokenizer.decode(outputs[0], skip_special_tokens=True))
|