Views
No views yet
optimum-intel to enable ultra-lightweight local deployment exclusively on CPUs. By leveraging OpenVINO, this version drastically minimizes RAM footprints and executes inference smoothly on modern CPU architectures without risking Out-Of-Memory (OOM) memory crashes.int4_asym): Offers a fantastic balance between medical description correctness and token generation latency.pip install --upgrade huggingface_hub optimum[intel] openvino nncf pillow --quiet1from PIL import Image
2import torch
3from optimum.intel.openvino import OVModelForVisualCausalLM
4from transformers import AutoProcessor, TextStreamer
5
6MODEL_ID = "vishnu0967/medgemma-1.5-4b-it-openvino-int4"
7
8print("Loading optimized 4-bit OpenVINO model directly from Hugging Face Hub...")
9# Load model directly from Hugging Face onto CPU
10model = OVModelForVisualCausalLM.from_pretrained(
11 MODEL_ID,
12 device="CPU",
13 ov_config={
14 "PERFORMANCE_HINT": "LATENCY",
15 "INFERENCE_NUM_THREADS": ""
16 }
17)
18
19# Load processor
20processor = AutoProcessor.from_pretrained(MODEL_ID)
21
22image = Image.open("/content/input.png").convert("RGB")
23
24messages = [
25 {
26 "role": "system",
27 "content": [
28 {
29 "type": "text",
30 "text": "You are an expert radiologist."
31 }
32 ]
33 },
34 {
35 "role": "user",
36 "content": [
37 {
38 "type": "text",
39 "text": "Analyze this medical image and explain any abnormalities."
40 },
41 {
42 "type": "image",
43 "image": image
44 }
45 ]
46 }
47]
48
49
50prompt = processor.apply_chat_template(
51 messages,
52 add_generation_prompt=True,
53 tokenize=False
54)
55
56inputs = processor(
57 text=prompt,
58 images=image,
59 return_tensors="pt"
60)
61
62streamer = TextStreamer(
63 processor,
64 skip_prompt=True,
65 skip_special_tokens=True
66)
67
68print("\nGenerating response on CPU...")
69print("--- Radiologist Report ---")
70
71with torch.inference_mode():
72 outputs = model.generate(
73 **inputs,
74 max_new_tokens=520,
75 do_sample=False,
76 streamer=streamer
77 )
78
79response = processor.decode(
80 outputs[0][inputs["input_ids"].shape[-1]:],
81 skip_special_tokens=True
82)
83
84print("\n--- Inference Finished ---")int4_asym) generated with NNCF.PERFORMANCE_HINT="LATENCY" is recommended for interactive usage.max_new_tokens for longer medical reports.pip install --upgrade huggingface_hub optimum[intel] openvino nncf pillow transformers --quiet1from optimum.intel.openvino import OVModelForVisualCausalLM
2from transformers import AutoProcessor
3
4model = OVModelForVisualCausalLM.from_pretrained(
5 "vishnu0967/medgemma-1.5-4b-it-openvino-int4",
6 device="CPU"
7)
8
9processor = AutoProcessor.from_pretrained(
10 "vishnu0967/medgemma-1.5-4b-it-openvino-int4"
11)