Image-Text-to-Text
Transformers
Safetensors
English
lfm3_vl
document-extraction
structured-output
json
system-1
conversational
custom_code
Instructions to use gradients-ai-1/Reflex-2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use gradients-ai-1/Reflex-2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="gradients-ai-1/Reflex-2B", trust_remote_code=True) 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)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("gradients-ai-1/Reflex-2B", trust_remote_code=True) model = AutoModelForMultimodalLM.from_pretrained("gradients-ai-1/Reflex-2B", trust_remote_code=True, 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 = processor.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=256) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use gradients-ai-1/Reflex-2B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "gradients-ai-1/Reflex-2B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "gradients-ai-1/Reflex-2B", "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/gradients-ai-1/Reflex-2B
- SGLang
How to use gradients-ai-1/Reflex-2B 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 "gradients-ai-1/Reflex-2B" \ --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": "gradients-ai-1/Reflex-2B", "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 "gradients-ai-1/Reflex-2B" \ --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": "gradients-ai-1/Reflex-2B", "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 gradients-ai-1/Reflex-2B with Docker Model Runner:
docker model run hf.co/gradients-ai-1/Reflex-2B
Download reflex_runtime/postprocess.py from gradients-ai-1/Reflex-2B: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/gradients-ai-1/Reflex-2B/resolve/main/reflex_runtime/postprocess.py
- Command line
-
hf download hf://gradients-ai-1/Reflex-2B/reflex_runtime/postprocess.py
-
curl -L -o postprocess.py https://huggingface.co/gradients-ai-1/Reflex-2B/resolve/main/reflex_runtime/postprocess.py
2.82 kB
| """Clean up a model's JSON answer after decoding. | |
| collapse_loops: a small model sometimes keeps writing table rows after the real table ends, by repeating a block of rows | |
| it already wrote. | |
| Real documents do have identical rows (the same item rung up several times), so only a clear loop is collapsed: | |
| - a block of 2+ rows that appears 3+ times in a row, or | |
| - a single row that appears 8+ times in a row (well above the longest real run, 5). | |
| One copy of the block is kept; the repeats, and a partial copy at the end, are dropped. Lists nested inside rows are | |
| cleaned the same way. | |
| """ | |
| import json | |
| MIN_BLOCK_REPEATS = 3 # block of 2+ rows: occurrences in a row that count as a loop | |
| MIN_SINGLE_REPEATS = 8 # single row: occurrences in a row that count as a loop | |
| MAX_PERIOD = 64 | |
| def _key(row): | |
| return json.dumps(row, sort_keys=True, ensure_ascii=False) | |
| def collapse_loops(rows): | |
| s = [_key(r) for r in rows] | |
| out, i = [], 0 | |
| while i < len(s): | |
| nxt = None | |
| for k in range(1, min(MAX_PERIOD, len(s) - i) + 1): | |
| need = MIN_SINGLE_REPEATS if k == 1 else MIN_BLOCK_REPEATS | |
| if i + need * k > len(s): | |
| continue # too few rows left for this period (a shorter one may still need fewer repeats) | |
| block = s[i:i + k] | |
| reps = 1 | |
| while s[i + reps * k:i + (reps + 1) * k] == block: | |
| reps += 1 | |
| if reps >= need: | |
| j = i + reps * k | |
| m = 0 | |
| while j + m < len(s) and m < k and s[j + m] == block[m]: | |
| m += 1 # a partial copy of the block at the end of the loop | |
| out.extend(rows[i:i + k]) | |
| nxt = j + m | |
| break | |
| if nxt is None: # no loop starts here | |
| out.append(rows[i]) | |
| nxt = i + 1 | |
| i = nxt | |
| return out | |
| def clean(value): | |
| """Collapse row loops in every list of objects inside a parsed answer. Returns (cleaned, rows_removed).""" | |
| removed = 0 | |
| def walk(v): | |
| nonlocal removed | |
| if isinstance(v, dict): | |
| return {k: walk(x) for k, x in v.items()} | |
| if isinstance(v, list): | |
| v = [walk(x) for x in v] | |
| if v and all(isinstance(x, dict) for x in v): | |
| c = collapse_loops(v) | |
| removed += len(v) - len(c) | |
| return c | |
| return v | |
| return v | |
| return walk(value), removed | |
| def clean_reply(text): | |
| """Clean a JSON reply string. Unparseable replies (e.g. cut off at the token limit) are returned unchanged.""" | |
| try: | |
| parsed = json.loads(text) | |
| except (json.JSONDecodeError, TypeError): | |
| return text, 0 | |
| cleaned, removed = clean(parsed) | |
| return (json.dumps(cleaned, ensure_ascii=False) if removed else text), removed | |