Instructions to use AmaadMartin/k_2_model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AmaadMartin/k_2_model with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="AmaadMartin/k_2_model", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("AmaadMartin/k_2_model", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use AmaadMartin/k_2_model with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "AmaadMartin/k_2_model" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "AmaadMartin/k_2_model", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/AmaadMartin/k_2_model
- SGLang
How to use AmaadMartin/k_2_model 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 "AmaadMartin/k_2_model" \ --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": "AmaadMartin/k_2_model", "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 "AmaadMartin/k_2_model" \ --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": "AmaadMartin/k_2_model", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use AmaadMartin/k_2_model with Docker Model Runner:
docker model run hf.co/AmaadMartin/k_2_model
| from typing import Dict, List, Any | |
| from peft import AutoPeftModelForCausalLM | |
| import transformers | |
| import os | |
| import tempfile | |
| from PIL import Image, ImageDraw | |
| from io import BytesIO | |
| import base64, json | |
| COORDINATE_PROMPT = 'In this UI screenshot, what is the position of the element corresponding to the command \"{command}\" (with point)?' | |
| PARTITION_PROMPT = 'In this UI screenshot, what is the partition of the element corresponding to the command \"{command}\" (with quadrant number)?' | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| self.model = transformers.AutoModelForCausalLM.from_pretrained( | |
| path, | |
| device_map="cuda", | |
| trust_remote_code=True, | |
| fp16=True).eval() | |
| self.tokenizer = transformers.AutoTokenizer.from_pretrained( | |
| path, | |
| cache_dir=None, | |
| model_max_length=2048, | |
| padding_side="right", | |
| use_fast=False, | |
| trust_remote_code=True, | |
| ) | |
| self.tokenizer.pad_token_id = self.tokenizer.eod_id | |
| return | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| """ | |
| data args: | |
| image (:obj: `str`) | |
| task (:obj: `str`) | |
| k (:obj: `str`) | |
| context (:obj: 'str') | |
| kwargs | |
| Return: | |
| A :obj:`list` | `dict`: will be serialized and returned | |
| """ | |
| # open temp directory | |
| with tempfile.TemporaryDirectory() as temp_dir: | |
| image = os.path.join(temp_dir, "image.png") | |
| img = Image.open(BytesIO(base64.b64decode(data["inputs"]["image"]))) | |
| img.save(image) | |
| command = data["inputs"]["task"] | |
| K = int(data["inputs"]["k"]) | |
| keep_context = bool(data["inputs"]["context"]) | |
| print(image) | |
| print(command) | |
| print(K) | |
| print(keep_context) | |
| images = [image] | |
| partitions = [] | |
| for k in range(K): | |
| query = self.tokenizer.from_list_format(([{ 'image': context_image } for context_image in images] if keep_context else [{'image': image}]) + | |
| [{'text': PARTITION_PROMPT.format(command=command)}]) | |
| response, _ = self.model.chat(self.tokenizer, query=query, history=None) | |
| partition = int(response.split(" ")[-1]) | |
| partitions.append(partition) | |
| # get cropped image of the partition | |
| with Image.open(image) as img: | |
| width, height = img.size | |
| if partition == 1: | |
| img = img.crop((width // 2, 0, width, height // 2)) | |
| elif partition == 2: | |
| img = img.crop((0, 0, width // 2, height // 2)) | |
| elif partition == 3: | |
| img = img.crop((0, height // 2, width // 2, height)) | |
| elif partition == 4: | |
| img = img.crop((width // 2, height // 2, width, height)) | |
| new_path = os.path.join(temp_dir, f"partition{k}.png") | |
| img.save(new_path) | |
| image = new_path | |
| images.append(image) | |
| query = self.tokenizer.from_list_format(([{ 'image': context_image } for context_image in images] if keep_context else [{'image': image}]) + | |
| [{'text': COORDINATE_PROMPT.format(command=command)}]) | |
| response, _ = self.model.chat(self.tokenizer, query=query, history=None) | |
| print("Coordinate Response:", response) | |
| x = float(response.split(",")[0].split("(")[1]) | |
| y = float(response.split(",")[1].split(")")[0]) | |
| for partition in partitions[::-1]: | |
| if partition == 1: | |
| x = x/2 + 0.5 | |
| y = y/2 | |
| elif partition == 2: | |
| x = x/2 | |
| y = y/2 | |
| elif partition == 3: | |
| x = x/2 | |
| y = y/2 + 0.5 | |
| elif partition == 4: | |
| x = x/2 + 0.5 | |
| y = y/2 + 0.5 | |
| print("rescaled point:", x, y) | |
| response = {} | |
| response['x'] = x | |
| response['y'] = y | |
| return response | |