Instructions to use advaitadasein/blip2_test with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use advaitadasein/blip2_test with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # 'pip install "transformers<5.0.0' from transformers import pipeline pipe = pipeline("image-to-text", model="advaitadasein/blip2_test")# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("advaitadasein/blip2_test") model = AutoModelForMultimodalLM.from_pretrained("advaitadasein/blip2_test", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Commit ·
df87eca
1
Parent(s): eaa60c2
uploaded handler and requirements for it
Browse files- handler.py +22 -0
- requirements.txt +2 -0
handler.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 4 |
+
from typing import Dict, List, Any
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
class EndpointHandler():
|
| 8 |
+
def __init__(self, path=""):
|
| 9 |
+
self.processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 10 |
+
self.model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 11 |
+
|
| 12 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
self.model.to(self.device)
|
| 14 |
+
|
| 15 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 16 |
+
image = data.pop("inputs", data)
|
| 17 |
+
|
| 18 |
+
processed = self.processor(images=image, return_tensors="pt").to(self.device)
|
| 19 |
+
|
| 20 |
+
out = self.model.generate(**processed)
|
| 21 |
+
|
| 22 |
+
return self.processor.decode(out[0], skip_special_tokens=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/transformers
|
| 2 |
+
git+https://github.com/huggingface/accelerate
|