Remove PredictionGuard and use Transform to download model
Browse files- README.md +8 -6
- requirements.txt +6 -1
- s2-train-data-into-multi-demension-vector.py +79 -0
README.md
CHANGED
|
@@ -28,11 +28,6 @@ Gradio includes **30+ built-in components**.
|
|
| 28 |
demo.launch(share=True) # Share your demo with just one extra parameter.
|
| 29 |
```
|
| 30 |
|
| 31 |
-
### Why Didn’t Hot Reloading Work?
|
| 32 |
-
(Investigate potential caching issues, missing dependencies, or incorrect function signatures.)
|
| 33 |
-
|
| 34 |
-
---
|
| 35 |
-
|
| 36 |
## Gradio Advanced Features
|
| 37 |
|
| 38 |
### **Gradio.Blocks**
|
|
@@ -102,4 +97,11 @@ _ = MultimodalLanceDB.from_text_image_pairs(
|
|
| 102 |
connection=db,
|
| 103 |
table_name=TBL_NAME,
|
| 104 |
mode="overwrite",
|
| 105 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
demo.launch(share=True) # Share your demo with just one extra parameter.
|
| 29 |
```
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
## Gradio Advanced Features
|
| 32 |
|
| 33 |
### **Gradio.Blocks**
|
|
|
|
| 97 |
connection=db,
|
| 98 |
table_name=TBL_NAME,
|
| 99 |
mode="overwrite",
|
| 100 |
+
)
|
| 101 |
+
```
|
| 102 |
+
# Gotchas
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
- Why Didn’t Hot Reloading Work?
|
| 106 |
+
- Downloading did not work since cat image from flicker was not available
|
| 107 |
+
- PredictionGuard was a huge dud. No KEY is available unless you contact them. For some reason hugging face also did not work. I ended up using transformer and downloading 3.5G of model
|
requirements.txt
CHANGED
|
@@ -1,3 +1,8 @@
|
|
| 1 |
gradio
|
| 2 |
langchain-predictionguard
|
| 3 |
-
IPython
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
langchain-predictionguard
|
| 3 |
+
IPython
|
| 4 |
+
umap-learn
|
| 5 |
+
pytubefix
|
| 6 |
+
youtube_transcript_api
|
| 7 |
+
torch
|
| 8 |
+
transformers
|
s2-train-data-into-multi-demension-vector.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
from numpy.linalg import norm
|
| 5 |
+
import cv2
|
| 6 |
+
from io import StringIO, BytesIO
|
| 7 |
+
from umap import UMAP
|
| 8 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from tqdm import tqdm
|
| 11 |
+
import base64
|
| 12 |
+
from transformers import BridgeTowerProcessor, BridgeTowerModel, BridgeTowerForContrastiveLearning
|
| 13 |
+
from PIL import Image
|
| 14 |
+
import torch
|
| 15 |
+
|
| 16 |
+
url1='http://farm3.staticflickr.com/2519/4126738647_cc436c111b_z.jpg'
|
| 17 |
+
cap1='A motorcycle sits parked across from a herd of livestock'
|
| 18 |
+
|
| 19 |
+
url2='http://farm3.staticflickr.com/2046/2003879022_1b4b466d1d_z.jpg'
|
| 20 |
+
cap2='Motorcycle on platform to be worked on in garage'
|
| 21 |
+
|
| 22 |
+
url3='https://i.natgeofe.com/n/548467d8-c5f1-4551-9f58-6817a8d2c45e/NationalGeographic_2572187_3x2.jpg'
|
| 23 |
+
cap3='a cat laying down stretched out near a laptop'
|
| 24 |
+
|
| 25 |
+
img1 = {
|
| 26 |
+
'flickr_url': url1,
|
| 27 |
+
'caption': cap1,
|
| 28 |
+
'image_path' : './shared_data/motorcycle_1.jpg'
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
img2 = {
|
| 32 |
+
'flickr_url': url2,
|
| 33 |
+
'caption': cap2,
|
| 34 |
+
'image_path' : './shared_data/motorcycle_2.jpg'
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
img3 = {
|
| 38 |
+
'flickr_url' : url3,
|
| 39 |
+
'caption': cap3,
|
| 40 |
+
'image_path' : './shared_data/cat_1.jpg'
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def bt_embeddings_from_local(prompt, base64_image):
|
| 45 |
+
|
| 46 |
+
model = BridgeTowerForContrastiveLearning.from_pretrained("BridgeTower/bridgetower-large-itm-mlm-itc")
|
| 47 |
+
|
| 48 |
+
inputs = BridgeTowerProcessor(prompt, base64_image, padding=True, return_tensors="pt")
|
| 49 |
+
outputs = model(**inputs)
|
| 50 |
+
|
| 51 |
+
cross_modal_embeddings = outputs.cross_embeds
|
| 52 |
+
text_embeddings = outputs.text_embeds
|
| 53 |
+
# image_embeddings = outputs.image_embeds
|
| 54 |
+
|
| 55 |
+
return text_embeddings.tolist() # Return the embeddings as a list for easier use
|
| 56 |
+
|
| 57 |
+
# encoding image at given path or PIL Image using base64
|
| 58 |
+
def encode_image(image_path_or_PIL_img):
|
| 59 |
+
if isinstance(image_path_or_PIL_img, Image.Image):
|
| 60 |
+
# this is a PIL image
|
| 61 |
+
buffered = BytesIO()
|
| 62 |
+
image_path_or_PIL_img.save(buffered, format="JPEG")
|
| 63 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 64 |
+
else:
|
| 65 |
+
# this is a image_path
|
| 66 |
+
with open(image_path_or_PIL_img, "rb") as image_file:
|
| 67 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
| 68 |
+
|
| 69 |
+
embeddings = []
|
| 70 |
+
for img in [img1, img2, img3]:
|
| 71 |
+
img_path = img['image_path']
|
| 72 |
+
caption = img['caption']
|
| 73 |
+
base64_img = encode_image(img_path)
|
| 74 |
+
embedding = bt_embeddings_from_local(caption, base64_img)
|
| 75 |
+
embeddings.append(embedding)
|
| 76 |
+
# Each image-text pair is now converted into multimodal
|
| 77 |
+
# embedding vector which has dimensions of 512.
|
| 78 |
+
|
| 79 |
+
print(len(embeddings[0]))
|