Create model.py
Browse files
model.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
|
5 |
+
class ImagePromptModel:
|
6 |
+
def __init__(self):
|
7 |
+
self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
8 |
+
self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
+
|
10 |
+
def generate_prompt(self, image_path):
|
11 |
+
image = Image.open(image_path).convert('RGB')
|
12 |
+
inputs = self.processor(images=image, return_tensors="pt")
|
13 |
+
out = self.model.generate(**inputs)
|
14 |
+
return self.processor.decode(out[0], skip_special_tokens=True)
|