firqaaa commited on
Commit
ad6f069
1 Parent(s): dffcacd

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +50 -1
README.md CHANGED
@@ -28,7 +28,56 @@ More information needed
28
 
29
  ## How to use
30
 
31
- More information needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  ## Intended uses & limitations
34
 
 
28
 
29
  ## How to use
30
 
31
+ ```python
32
+ from peft import PeftModel, PeftConfig
33
+ from transformers import AutoProcessor, LlavaForConditionalGeneration
34
+ import torch
35
+ from PIL import Image
36
+ import requests
37
+
38
+ # Check device availability
39
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
40
+ print(f"Using device: {device}")
41
+
42
+ # Load model configurations
43
+ config = PeftConfig.from_pretrained("firqaaa/vsft-llava-1.5-7b-hf-liveness-trl")
44
+ processor = AutoProcessor.from_pretrained("llava-hf/llava-1.5-7b-hf")
45
+ base_model = LlavaForConditionalGeneration.from_pretrained("llava-hf/llava-1.5-7b-hf",
46
+ torch_dtype=torch.float16,
47
+ low_cpu_mem_usage=True,
48
+ device_map="auto",
49
+ load_in_4bit=True,
50
+ attn_implementation="flash_attention_2")
51
+ model = PeftModel.from_pretrained(base_model, "firqaaa/vsft-llava-1.5-7b-hf-liveness-trl")
52
+ model.to(device)
53
+
54
+ # url = "https://github.com/haotian-liu/LLaVA/blob/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg?raw=true"
55
+ # image = Image.open(requests.get(url, stream=True).raw)
56
+
57
+ image_path = "/home/firqaaa/Python/Retired-Yann-LeCun/silicone_frames/5/frame7.jpg"
58
+ image = Image.open(image_path)
59
+
60
+ prompt = """USER: <image>\nI ask you to be an liveness image annotator expert to determine if an image "Real" or "Spoof".
61
+ If an image is a "Spoof" define what kind of attack, is it spoofing attack that used Print(flat), Replay(monitor, laptop), or Mask(paper, crop-paper, silicone)?
62
+ If an image is a "Real" or "Normal" return "No Attack".
63
+ Whether if an image is "Real" or "Spoof" give an explanation to this.
64
+ Return your response using following format :
65
+
66
+ Real/Spoof :
67
+ Attack Type :
68
+ Explanation :\nASSISTANT:"""
69
+
70
+ # Prepare inputs and move to device
71
+ inputs = processor(prompt, images=image, return_tensors="pt").to(device)
72
+
73
+ # Generate output
74
+ output = model.generate(**inputs, max_new_tokens=300)
75
+
76
+ print("Response :")
77
+ # Decode and print the output
78
+ decoded_output = processor.decode(output[0], skip_special_tokens=True).split("ASSISTANT:")[-1].strip()
79
+ print(decoded_output)
80
+ ```
81
 
82
  ## Intended uses & limitations
83