Views
No views yet
1
2model_path="sugiv/Fuyu-8b-transfer-learned-spiqa-simplified"
3processor = FuyuProcessor.from_pretrained(model_path)
4model = FuyuForCausalLM.from_pretrained(model_path, device_map="auto")
5
6text_prompt = "What color is the bus?\n"
7url = "https://huggingface.co/adept/fuyu-8b/resolve/main/bus.png"
8image = Image.open(requests.get(url, stream=True).raw)
9
10inputs = processor(text=text_prompt, images=image, return_tensors="pt").to("cuda:0")
11# Move inputs to the same device as the model
12device = next(model.parameters()).device
13inputs = {k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in inputs.items()}
14
15# If 'image_patches' is a list of tensors, move each tensor to the correct device
16if 'image_patches' in inputs and isinstance(inputs['image_patches'], list):
17 inputs['image_patches'] = [patch.to(device) for patch in inputs['image_patches']]
18
19outputs = model.generate(
20 **inputs,
21 max_new_tokens=400,
22 repetition_penalty=1.2,
23 no_repeat_ngram_size=3,
24 top_k=40,
25 top_p=0.92,
26 temperature=0.7,
27 do_sample=True
28 )
29
30# Decode the output
31generated_text = processor.decode(outputs[0], skip_special_tokens=True)
32
33# Clean up the generated text
34generated_text = generated_text.replace("|SPEAKER|", "").replace("|NEWLINE|", " ").strip()
35if "\x04" in generated_text:
36 generated_text = generated_text.split("\x04")[-1].strip()
37
38print(generated_text)
39