Phase-Technologies commited on
Commit
7a60f63
·
verified ·
1 Parent(s): 0d47ecf

Create inference.py

Browse files
Files changed (1) hide show
  1. inference.py +56 -0
inference.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gc
3
+ from transformers import TextStreamer, pipeline, BitsAndBytesConfig
4
+
5
+ # 1. Clear GPU memory to recover from the previous OOM
6
+ if 'pipe' in locals(): del pipe
7
+ if 'outputs' in locals(): del outputs
8
+ gc.collect()
9
+ torch.cuda.empty_cache()
10
+
11
+ # 2. Configure 4-bit quantization properly for custom architectures
12
+ bnb_config = BitsAndBytesConfig(
13
+ load_in_4bit=True,
14
+ bnb_4bit_compute_dtype=torch.float16,
15
+ bnb_4bit_quant_type="nf4",
16
+ bnb_4bit_use_double_quant=True
17
+ )
18
+
19
+ model_id = "Xerv-AI/tarn"
20
+ pipe = pipeline(
21
+ "image-text-to-text",
22
+ model=model_id,
23
+ device_map="auto",
24
+ trust_remote_code=True,
25
+ model_kwargs={
26
+ "quantization_config": bnb_config,
27
+ "torch_dtype": torch.float16
28
+ }
29
+ )
30
+
31
+ streamer = TextStreamer(pipe.tokenizer, skip_prompt=True)
32
+
33
+ messages = [
34
+ {
35
+ "role": "user",
36
+ "content": [
37
+ {
38
+ "type": "image",
39
+ "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"
40
+ },
41
+ {
42
+ "type": "text",
43
+ "text": "Analyze the visual artifacts present in this image and define the principles of triboelectricity."
44
+ }
45
+ ]
46
+ },
47
+ ]
48
+
49
+ print("=== Initiating Real-Time Telemetry Stream (Quantized) ===")
50
+ outputs = pipe(
51
+ text=messages,
52
+ generate_kwargs={
53
+ "max_new_tokens": 512,
54
+ "streamer": streamer
55
+ }
56
+ )