Image-Text-to-Text
Transformers
Safetensors
Polish
llama
text-generation
vision
multimodal
llava
siglip
bielik
llm
conversational
text-generation-inference
Instructions to use Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct") model = AutoModelForCausalLM.from_pretrained("Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct
- SGLang
How to use Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct with Docker Model Runner:
docker model run hf.co/Wojtekb30/Bielik-1.5B-v3.0-VLM-Instruct
| import torch | |
| import os | |
| from PIL import Image | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, AutoModel, AutoImageProcessor | |
| import tkinter as tk | |
| from tkinter import filedialog | |
| from safetensors.torch import load_file | |
| # ============================================================ | |
| # KONFIGURACJA 艢CIE呕EK MODELI ORAZ URZ膭DZENIA OBLICZENIOWEGO | |
| # ============================================================ | |
| # 艢cie偶ka do modelu wizyjnego | |
| VISION_MODEL_PATH = "google/siglip-so400m-patch14-384" | |
| #VISION_MODEL_PATH = "siglip-so400m-patch14-384" | |
| # 艢cie偶ka do scalonego modelu j臋zykowego (LLM + adaptery) | |
| MERGED_MODEL_PATH = "./" | |
| # 艢cie偶ka do pliku z wagami projektora multimodalnego | |
| PROJECTOR_FILE = "./mm_projector.safetensors" | |
| # Automatyczny wyb贸r urz膮dzenia: GPU (CUDA) je艣li dost臋pne, w przeciwnym razie CPU | |
| DEVICE = "cuda" if torch.cuda.is_available() else "cpu" | |
| def wybierz_plik(): | |
| """ | |
| Otwiera systemowe okno dialogowe wyboru pliku. | |
| Zwraca 艣cie偶k臋 do wybranego pliku lub pusty string, | |
| je艣li u偶ytkownik anulowa艂 wyb贸r. | |
| """ | |
| root = tk.Tk() | |
| root.withdraw() | |
| sciezka = filedialog.askopenfilename() | |
| root.destroy() # Zamkni臋cie instancji Tkinter w celu zwolnienia zasob贸w | |
| return sciezka if sciezka else "" | |
| # ============================================================ | |
| # DEFINICJA PROJEKTORA MULTIMODALNEGO | |
| # ============================================================ | |
| class MultimodalProjector(torch.nn.Module): | |
| """ | |
| Projektor odpowiedzialny za mapowanie reprezentacji | |
| wizualnych (vision tower) do przestrzeni osadze艅 (embedding贸w) | |
| modelu j臋zykowego (LLM). | |
| Sk艂ada si臋 z dw贸ch warstw liniowych z aktywacj膮 GELU. | |
| """ | |
| def __init__(self, vision_dim, llm_dim): | |
| """ | |
| :param vision_dim: Rozmiar wektora cech z modelu vision. | |
| :param llm_dim: Rozmiar przestrzeni ukrytej modelu j臋zykowego. | |
| """ | |
| super().__init__() | |
| self.net = torch.nn.Sequential( | |
| torch.nn.Linear(vision_dim, llm_dim), | |
| torch.nn.GELU(), | |
| torch.nn.Linear(llm_dim, llm_dim) | |
| ) | |
| def forward(self, x): | |
| """ | |
| Przekszta艂ca wej艣ciowe cechy wizualne do przestrzeni LLM. | |
| """ | |
| return self.net(x) | |
| def load_models(): | |
| """ | |
| 艁aduje wszystkie wymagane komponenty systemu multimodalnego: | |
| - model j臋zykowy (LLM), | |
| - tokenizer, | |
| - model vision (vision tower), | |
| - procesor obrazu, | |
| - projektor multimodalny. | |
| Zwraca komplet za艂adowanych obiekt贸w. | |
| """ | |
| print(f"艁adowanie Scalonego Bielika z {MERGED_MODEL_PATH}...") | |
| # 1. 艁adowanie modelu j臋zykowego (LLM) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MERGED_MODEL_PATH, | |
| torch_dtype=torch.float16, | |
| trust_remote_code=True, | |
| device_map="auto" | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(MERGED_MODEL_PATH) | |
| # 2. 艁adowanie modelu vision oraz procesora obrazu | |
| print("艁adowanie Vision Tower...") | |
| vision_tower = AutoModel.from_pretrained( | |
| VISION_MODEL_PATH, | |
| torch_dtype=torch.float16 | |
| ).to(DEVICE) | |
| # Procesor odpowiada za preprocessing obrazu (resize, normalizacja itd.) | |
| vision_processor = AutoImageProcessor.from_pretrained(VISION_MODEL_PATH) | |
| # 3. 艁adowanie wag projektora multimodalnego | |
| print("艁adowanie Projektora...") | |
| projector_weights = load_file(PROJECTOR_FILE) | |
| # Dynamiczne pobranie wymiar贸w ukrytych z konfiguracji modeli | |
| vision_dim = vision_tower.config.vision_config.hidden_size | |
| llm_dim = model.config.hidden_size | |
| print(f"Wykryte wymiary: Vision={vision_dim}, LLM={llm_dim}") | |
| # Inicjalizacja projektora z odpowiednimi wymiarami | |
| projector = MultimodalProjector( | |
| vision_dim=vision_dim, | |
| llm_dim=llm_dim | |
| ).to(DEVICE).to(torch.float16) | |
| # Za艂adowanie wag do projektora | |
| projector.load_state_dict(projector_weights) | |
| return model, vision_tower, vision_processor, projector, tokenizer | |
| def chat(): | |
| """ | |
| G艂贸wna p臋tla interakcyjna aplikacji. | |
| Umo偶liwia prac臋 w trybie: | |
| - tekstowym, | |
| - multimodalnym (tekst + obraz). | |
| """ | |
| # Za艂adowanie wszystkich komponent贸w systemu | |
| model, vision_tower, vision_processor, projector, tokenizer = load_models() | |
| print("\n" + "=" * 50) | |
| print("BIELIK LLaVA - GOTOWY DO ROZMOWY") | |
| print("Otworzy si臋 okno wyboru pliku. Anulowanie wyboru = tryb tekstowy.") | |
| print("Wpisz 'exit' w konsoli aby wyj艣膰.") | |
| print("=" * 50 + "\n") | |
| while True: | |
| # ============================================ | |
| # A. Wyb贸r obrazu (opcjonalnie) | |
| # ============================================ | |
| print("\nWybierz plik obrazka w oknie...") | |
| img_path = wybierz_plik() | |
| pixel_values = None | |
| has_image = False | |
| if img_path: | |
| if os.path.exists(img_path): | |
| try: | |
| print(f"Wybrano: {img_path}") | |
| # Wczytanie i konwersja obrazu do RGB | |
| image = Image.open(img_path).convert('RGB') | |
| # Przetwarzanie obrazu do tensora zgodnego z vision tower | |
| pixel_values = vision_processor( | |
| images=image, | |
| return_tensors="pt" | |
| ).pixel_values.to(DEVICE, dtype=torch.float16) | |
| has_image = True | |
| except Exception as e: | |
| print(f"B艂膮d 艂adowania obrazka: {e}") | |
| continue | |
| else: | |
| print("艢cie偶ka nieprawid艂owa.") | |
| else: | |
| print("Tryb tekstowy (bez obrazka).") | |
| # ============================================ | |
| # B. Pobranie promptu tekstowego | |
| # ============================================ | |
| prompt = input("Tw贸j Prompt: ").strip() | |
| if prompt.lower() == 'exit': | |
| break | |
| if not prompt: | |
| continue | |
| # ============================================ | |
| # C. Generowanie odpowiedzi | |
| # ============================================ | |
| with torch.no_grad(): | |
| # 1. Ekstrakcja cech wizualnych (je艣li obraz zosta艂 podany) | |
| img_embeds = None | |
| if has_image: | |
| vision_feats = vision_tower.vision_model(pixel_values).last_hidden_state | |
| img_embeds = projector(vision_feats) | |
| # 2. Przygotowanie wej艣cia tekstowego w odpowiednim formacie czatu | |
| if has_image: | |
| text_input = ( | |
| f"<|im_start|>user\n<image>\n{prompt}<|im_end|>\n" | |
| f"<|im_start|>assistant\n" | |
| ) | |
| else: | |
| text_input = ( | |
| f"<|im_start|>user\n{prompt}<|im_end|>\n" | |
| f"<|im_start|>assistant\n" | |
| ) | |
| # Tokenizacja tekstu | |
| input_ids = tokenizer.encode( | |
| text_input, | |
| return_tensors="pt" | |
| ).to(DEVICE) | |
| # Konwersja token贸w do embedding贸w | |
| inputs_embeds = model.model.embed_tokens(input_ids) | |
| # 3. 艁膮czenie embedding贸w obrazu i tekstu | |
| final_embeds = inputs_embeds | |
| if has_image: | |
| input_ids_clean = tokenizer.encode( | |
| text_input, | |
| return_tensors="pt" | |
| ).to(DEVICE) | |
| inputs_embeds_clean = model.model.embed_tokens(input_ids_clean) | |
| # Konkatenacja embedding贸w obrazu oraz tekstu w osi sekwencji | |
| final_embeds = torch.cat( | |
| [img_embeds, inputs_embeds_clean], | |
| dim=1 | |
| ) | |
| # 4. Generowanie odpowiedzi przez model j臋zykowy | |
| print("Generowanie...", end="", flush=True) | |
| # Utworzenie attention_mask o tej samej d艂ugo艣ci co final_embeds | |
| batch_size = final_embeds.shape[0] | |
| seq_len = final_embeds.shape[1] | |
| attention_mask = torch.ones( | |
| (batch_size, seq_len), | |
| device=DEVICE | |
| ) | |
| output_ids = model.generate( | |
| inputs_embeds=final_embeds, | |
| attention_mask=attention_mask, | |
| max_new_tokens=256, | |
| temperature=0.3, | |
| do_sample=True, | |
| pad_token_id=tokenizer.eos_token_id, | |
| eos_token_id=tokenizer.eos_token_id | |
| ) | |
| # Dekodowanie wygenerowanej sekwencji token贸w | |
| generated_text = tokenizer.decode( | |
| output_ids[0], | |
| skip_special_tokens=True | |
| ) | |
| print("\r", end="") | |
| print(f"Bielik: {generated_text}") | |
| print("-" * 30) | |
| if __name__ == "__main__": | |
| chat() |