|
import torch |
|
|
|
|
|
from transformers import AutoProcessor, AutoModelForVision2Seq, AwqConfig |
|
|
|
import numpy as np |
|
import pyttsx3 |
|
|
|
|
|
START_TO_COUCH = np.array([[0.5, 0], [0.5, 0.5]]).ravel() |
|
COUCH_TO_KITCHEN = np.array([[0.5, -0.5], [1.0, -1.0]]).ravel() |
|
KITCHEN_TO_START = np.array([[0.5, -0.5], [0, 0]]).ravel() |
|
|
|
engine = pyttsx3.init("espeak") |
|
voices = engine.getProperty("voices") |
|
engine.setProperty("voice", voices[3].id) |
|
|
|
|
|
def speak(text): |
|
print(f"said {text}", flush=True) |
|
engine.say(text) |
|
engine.runAndWait() |
|
|
|
|
|
speak("hello") |
|
|
|
MODE = "quantized" |
|
DEVICE = "cuda" |
|
PROCESSOR = AutoProcessor.from_pretrained("HuggingFaceM4/idefics2-tfrm-compatible") |
|
BAD_WORDS_IDS = PROCESSOR.tokenizer( |
|
["<image>", "<fake_token_around_image>"], add_special_tokens=False |
|
).input_ids |
|
EOS_WORDS_IDS = PROCESSOR.tokenizer( |
|
"<end_of_utterance>", add_special_tokens=False |
|
).input_ids + [PROCESSOR.tokenizer.eos_token_id] |
|
|
|
|
|
if MODE == "regular": |
|
model = AutoModelForVision2Seq.from_pretrained( |
|
"HuggingFaceM4/idefics2-tfrm-compatible", |
|
torch_dtype=torch.float16, |
|
trust_remote_code=True, |
|
_attn_implementation="flash_attention_2", |
|
revision="3dc93be345d64fb6b1c550a233fe87ddb36f183d", |
|
).to(DEVICE) |
|
elif MODE == "quantized": |
|
quant_path = "HuggingFaceM4/idefics2-tfrm-compatible-AWQ" |
|
model = AutoModelForVision2Seq.from_pretrained( |
|
quant_path, trust_remote_code=True |
|
).to(DEVICE) |
|
elif MODE == "fused_quantized": |
|
quant_path = "HuggingFaceM4/idefics2-tfrm-compatible-AWQ" |
|
quantization_config = AwqConfig( |
|
bits=4, |
|
fuse_max_seq_len=4096, |
|
modules_to_fuse={ |
|
"attention": ["q_proj", "k_proj", "v_proj", "o_proj"], |
|
"mlp": ["gate_proj", "up_proj", "down_proj"], |
|
"layernorm": ["input_layernorm", "post_attention_layernorm", "norm"], |
|
"use_alibi": False, |
|
"num_attention_heads": 32, |
|
"num_key_value_heads": 8, |
|
"hidden_size": 4096, |
|
}, |
|
) |
|
model = AutoModelForVision2Seq.from_pretrained( |
|
quant_path, quantization_config=quantization_config, trust_remote_code=True |
|
).to(DEVICE) |
|
else: |
|
raise ValueError("Unknown mode") |
|
|
|
|
|
def ask_vlm(image, instruction): |
|
prompts = [ |
|
"User:", |
|
image, |
|
f"{instruction}.<end_of_utterance>\n", |
|
"Assistant:", |
|
] |
|
speak(instruction) |
|
inputs = PROCESSOR(prompts) |
|
inputs = {k: torch.tensor(v).to(DEVICE) for k, v in inputs.items()} |
|
|
|
generated_ids = model.generate( |
|
**inputs, bad_words_ids=BAD_WORDS_IDS, max_new_tokens=50 |
|
) |
|
generated_texts = PROCESSOR.batch_decode(generated_ids, skip_special_tokens=True) |
|
|
|
text = generated_texts[0].split("\nAssistant: ")[1] |
|
speak(text) |
|
return text |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|