Instructions to use kiddobellamy/Llama_Vision with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use kiddobellamy/Llama_Vision with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("kiddobellamy/Llama_Vision") model = AutoModelForCausalLM.from_pretrained("kiddobellamy/Llama_Vision", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from kiddobellamy/Llama_Vision: direct link, hf CLI and curl.
- Browser
- Download file 2.07 kB
-
https://huggingface.co/kiddobellamy/Llama_Vision/resolve/main/handler.py
- Command line
-
hf download hf://kiddobellamy/Llama_Vision/handler.py
-
curl -L -o handler.py https://huggingface.co/kiddobellamy/Llama_Vision/resolve/main/handler.py
2.07 kB
| import torch | |
| from transformers import LlamaForCausalLM, AutoTokenizer, AutoProcessor | |
| from PIL import Image | |
| import base64 | |
| import io | |
| # Load model and processor globally | |
| model_id = "kiddobellamy/Llama_Vision" | |
| # Load the model | |
| model = LlamaForCausalLM.from_pretrained( | |
| model_id, | |
| torch_dtype=torch.float16, # Use torch.float16 if bfloat16 is not supported | |
| device_map="auto", | |
| ) | |
| # Load the tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| # Load the processor if needed (for image processing) | |
| processor = AutoProcessor.from_pretrained(model_id) | |
| def handler(event, context): | |
| try: | |
| # Parse inputs | |
| inputs = event.get('inputs', {}) | |
| image_base64 = inputs.get('image') | |
| prompt = inputs.get('prompt', '') | |
| if not image_base64 or not prompt: | |
| return {'error': 'Both "image" and "prompt" are required in inputs.'} | |
| # Decode the base64 image | |
| image_bytes = base64.b64decode(image_base64) | |
| image = Image.open(io.BytesIO(image_bytes)).convert('RGB') | |
| # Process image if necessary (depends on your model) | |
| # Assuming your processor handles image preprocessing | |
| image_inputs = processor(images=image, return_tensors="pt").to(model.device) | |
| # Tokenize the prompt | |
| text_inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| # Combine image and text inputs if required by your model | |
| # This step depends on how your model processes images and text together | |
| inputs = { | |
| 'input_ids': text_inputs['input_ids'], | |
| 'attention_mask': text_inputs['attention_mask'], | |
| # Include image inputs as required | |
| # 'pixel_values': image_inputs['pixel_values'], | |
| } | |
| # Generate output | |
| output_ids = model.generate(**inputs, max_new_tokens=50) | |
| generated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True) | |
| # Return the result | |
| return {'generated_text': generated_text} | |
| except Exception as e: | |
| return {'error': str(e)} | |
| #111 |