Instructions to use chongzicbo/MathImg2Latex with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use chongzicbo/MathImg2Latex with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="chongzicbo/MathImg2Latex")# Load model directly from transformers import AutoTokenizer, AutoModelForImageTextToText tokenizer = AutoTokenizer.from_pretrained("chongzicbo/MathImg2Latex") model = AutoModelForImageTextToText.from_pretrained("chongzicbo/MathImg2Latex") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use chongzicbo/MathImg2Latex with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "chongzicbo/MathImg2Latex" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "chongzicbo/MathImg2Latex", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/chongzicbo/MathImg2Latex
- SGLang
How to use chongzicbo/MathImg2Latex 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 "chongzicbo/MathImg2Latex" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "chongzicbo/MathImg2Latex", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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 "chongzicbo/MathImg2Latex" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "chongzicbo/MathImg2Latex", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use chongzicbo/MathImg2Latex with Docker Model Runner:
docker model run hf.co/chongzicbo/MathImg2Latex
github: https://github.com/chongzicbo/MathImg2Latex/tree/main
import torch
from PIL import Image
from transformers import VisionEncoderDecoderModel
from transformers.models.nougat import NougatTokenizerFast
from nougat_latex.util import process_raw_latex_code
from nougat_latex import NougatLaTexProcessor
os.environ["RUN_ON_GPU_IDs"] = "1"
device = torch.device("cpu")
model_path = "chongzicbo/MathImg2Latex"
tokenizer = NougatTokenizerFast.from_pretrained(model_path)
latex_processor = NougatLaTexProcessor.from_pretrained(model_path)
model = VisionEncoderDecoderModel.from_pretrained(model_path)
model.to(device)
img_path = "/data/code/MathOCR/MathImg2Latex/examples/test_data/test86_screenshot_bigger.jpg"
image = Image.open(img_path)
if not image.mode == "RGB":
image = image.convert("RGB")
pixel_values = latex_processor(image, return_tensors="pt").pixel_values
task_prompt = tokenizer.bos_token
decoder_input_ids = tokenizer(
task_prompt, add_special_tokens=False, return_tensors="pt"
).input_ids
with torch.no_grad():
outputs = model.generate(
pixel_values.to(device),
decoder_input_ids=decoder_input_ids.to(device),
max_length=model.decoder.config.max_length,
early_stopping=True,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
use_cache=True,
num_beams=1,
bad_words_ids=[[tokenizer.unk_token_id]],
return_dict_in_generate=True,
)
sequence = tokenizer.batch_decode(outputs.sequences)[0]
sequence = (
sequence.replace(tokenizer.eos_token, "")
.replace(tokenizer.pad_token, "")
.replace(tokenizer.bos_token, "")
)
sequence = process_raw_latex_code(sequence)
print(sequence)
- Downloads last month
- -
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support