Instructions to use StanfordAIMI/CheXagent-8b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use StanfordAIMI/CheXagent-8b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="StanfordAIMI/CheXagent-8b", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("StanfordAIMI/CheXagent-8b", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use StanfordAIMI/CheXagent-8b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "StanfordAIMI/CheXagent-8b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "StanfordAIMI/CheXagent-8b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/StanfordAIMI/CheXagent-8b
- SGLang
How to use StanfordAIMI/CheXagent-8b 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 "StanfordAIMI/CheXagent-8b" \ --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": "StanfordAIMI/CheXagent-8b", "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 "StanfordAIMI/CheXagent-8b" \ --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": "StanfordAIMI/CheXagent-8b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use StanfordAIMI/CheXagent-8b with Docker Model Runner:
docker model run hf.co/StanfordAIMI/CheXagent-8b
Download processing_chexagent.py from StanfordAIMI/CheXagent-8b: direct link, hf CLI and curl.
- Browser
- Download file 5.28 kB
-
https://huggingface.co/StanfordAIMI/CheXagent-8b/resolve/main/processing_chexagent.py
- Command line
-
hf download hf://StanfordAIMI/CheXagent-8b/processing_chexagent.py
-
curl -L -o processing_chexagent.py https://huggingface.co/StanfordAIMI/CheXagent-8b/resolve/main/processing_chexagent.py
5.28 kB
| # coding=utf-8 | |
| # Copyright 2023 The CheXagent Authors and The HuggingFace Inc. team. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| from typing import List, Optional, Union | |
| import torch | |
| from transformers.image_utils import ImageInput | |
| from transformers.processing_utils import ProcessorMixin | |
| from transformers.tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput | |
| from transformers.tokenization_utils_base import TruncationStrategy | |
| from transformers.utils import TensorType | |
| class CheXagentProcessor(ProcessorMixin): | |
| attributes = ["image_processor", "tokenizer"] | |
| image_processor_class = "BlipImageProcessor" | |
| tokenizer_class = "AutoTokenizer" | |
| def __init__(self, image_processor, tokenizer): | |
| tokenizer.return_token_type_ids = False | |
| super().__init__(image_processor, tokenizer) | |
| self.current_processor = self.image_processor | |
| def __call__( | |
| self, | |
| images: ImageInput = None, | |
| text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, | |
| add_special_tokens: bool = True, | |
| padding: Union[bool, str, PaddingStrategy] = False, | |
| truncation: Union[bool, str, TruncationStrategy] = None, | |
| max_length: Optional[int] = None, | |
| stride: int = 0, | |
| pad_to_multiple_of: Optional[int] = None, | |
| return_attention_mask: Optional[bool] = None, | |
| return_overflowing_tokens: bool = False, | |
| return_special_tokens_mask: bool = False, | |
| return_offsets_mapping: bool = False, | |
| return_token_type_ids: bool = False, | |
| return_length: bool = False, | |
| verbose: bool = True, | |
| return_tensors: Optional[Union[str, TensorType]] = None, | |
| **kwargs, | |
| ) -> BatchEncoding: | |
| if images is None and text is None: | |
| raise ValueError("You have to specify either images or text.") | |
| # Get only text | |
| if images is None: | |
| self.current_processor = self.tokenizer | |
| text_encoding = self.tokenizer( | |
| text=text, | |
| add_special_tokens=add_special_tokens, | |
| padding=padding, | |
| truncation=truncation, | |
| max_length=max_length, | |
| stride=stride, | |
| pad_to_multiple_of=pad_to_multiple_of, | |
| return_attention_mask=return_attention_mask, | |
| return_overflowing_tokens=return_overflowing_tokens, | |
| return_special_tokens_mask=return_special_tokens_mask, | |
| return_offsets_mapping=return_offsets_mapping, | |
| return_token_type_ids=return_token_type_ids, | |
| return_length=return_length, | |
| verbose=verbose, | |
| return_tensors=return_tensors, | |
| **kwargs, | |
| ) | |
| return text_encoding | |
| # add pixel_values | |
| if images is not None: | |
| encoding_image_processor = self.image_processor(images, return_tensors=return_tensors) | |
| encoding_image_processor["pixel_values"] = torch.stack( | |
| [torch.tensor(pixel_values) for pixel_values in encoding_image_processor["pixel_values"]] | |
| ).unsqueeze(0) | |
| if text is not None: | |
| text_encoding = self.tokenizer( | |
| text=text, | |
| add_special_tokens=add_special_tokens, | |
| padding=padding, | |
| truncation=truncation, | |
| max_length=max_length, | |
| stride=stride, | |
| pad_to_multiple_of=pad_to_multiple_of, | |
| return_attention_mask=return_attention_mask, | |
| return_overflowing_tokens=return_overflowing_tokens, | |
| return_special_tokens_mask=return_special_tokens_mask, | |
| return_offsets_mapping=return_offsets_mapping, | |
| return_token_type_ids=return_token_type_ids, | |
| return_length=return_length, | |
| verbose=verbose, | |
| return_tensors=return_tensors, | |
| **kwargs, | |
| ) | |
| else: | |
| text_encoding = None | |
| if text_encoding is not None: | |
| encoding_image_processor.update(text_encoding) | |
| return encoding_image_processor | |
| def batch_decode(self, *args, **kwargs): | |
| return self.tokenizer.batch_decode(*args, **kwargs) | |
| def decode(self, *args, **kwargs): | |
| return self.tokenizer.decode(*args, **kwargs) | |
| def model_input_names(self): | |
| tokenizer_input_names = self.tokenizer.model_input_names | |
| image_processor_input_names = self.image_processor.model_input_names | |
| return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names)) | |