Views
No views yet
google/siglip-so400m-patch14-384 image encoder with the Llama 3.2-1B language model. It has been trained on 20% of the unsloth/LaTeX_OCR dataset, which itself is a subset of the linxy/LaTeX_OCR dataset.git clone https://github.com/ritabratamaiti/AnyModal.gitpip install torch transformers torchvision huggingface_hub tqdm matplotlib Pillow1import llm
2import anymodal
3import torch
4import vision
5from PIL import Image
6from huggingface_hub import hf_hub_download, snapshot_download
7
8# Load language model and tokenizer
9llm_tokenizer, llm_model = llm.get_llm(
10 "meta-llama/Llama-3.2-1B",
11 access_token="GET_YOUR_OWN_TOKEN_FROM_HUGGINGFACE",
12 quantized=False,
13 use_peft=False,
14)
15
16device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17llm_model.to(device)
18
19llm_hidden_size = llm.get_hidden_size(llm_tokenizer, llm_model)
20
21# Load vision model components
22image_processor, vision_model, vision_hidden_size = vision.get_image_encoder(
23 "google/siglip-so400m-patch14-384", use_peft=False
24)
25
26# Initialize vision tokenizer and encoder
27vision_encoder = vision.VisionEncoder(vision_model)
28vision_tokenizer = vision.Projector(vision_hidden_size, llm_hidden_size, num_hidden=1)
29
30# Initialize MultiModalModel
31multimodal_model = anymodal.MultiModalModel(
32 input_processor=None,
33 input_encoder=vision_encoder,
34 input_tokenizer=vision_tokenizer,
35 language_tokenizer=llm_tokenizer,
36 language_model=llm_model,
37 prompt_text="The latex expression of the equation in the image is: ",
38)
39
40# Load pre-trained weights
41if not os.path.exists("latex_ocr"):
42 os.makedirs("latex_ocr")
43
44snapshot_download("AnyModal/latex-ocr-Llama-3.2-1B", local_dir="latex_ocr")
45multimodal_model._load_model("latex_ocr")
46
47# Generate LaTeX expression from an image
48image_path = "example_equation.jpg" # Path to your image
49image = Image.open(image_path).convert("RGB")
50processed_image = image_processor(image, return_tensors="pt")
51processed_image = {key: val.squeeze(0) for key, val in processed_image.items()}
52
53# Generate LaTeX caption
54generated_caption = multimodal_model.generate(processed_image, max_new_tokens=120)
55print("Generated LaTeX Caption:", generated_caption)google/siglip-so400m-patch14-384 model, pre-trained for visual feature extraction, was used as the image encoder.