import streamlit as st
from sklearn.metrics.pairwise import cosine_similarity
from sentence_transformers import SentenceTransformer
from transformers import CLIPTokenizer, CLIPModel
from scipy.spatial import distance
import numpy as np
import torch
import json
import random
model_id = "openai/clip-vit-base-patch32"
clip_model = CLIPModel.from_pretrained(model_id)
clip_tokenizer = CLIPTokenizer.from_pretrained(model_id)
device = "cuda" if torch.cuda.is_available() else "cpu"
# move the model to the device
clip_model.to(device)
def get_single_text_embedding(text):
inputs = clip_tokenizer(text, return_tensors = "pt")
text_embeddings = clip_model.to(device).get_text_features(**inputs.to(device))
embedding_as_np = text_embeddings.cpu().detach().numpy()
return embedding_as_np
def get_top_N_images(query, image_vectors, top_K=4):
query_vect = get_single_text_embedding(query)
data = cosine_similarity(query_vect, image_vectors)[0]
most_similar_images = sorted(range(len(data)), key=lambda i: data[i], reverse=True)[:top_K]
return most_similar_images
image_data = []
with open('Property-images.jsonl', 'r', encoding="utf-8") as f:
for line in f:
image_data.append(json.loads(line))
with open('property-image-vectors.npy', 'rb') as f:
image_vectors = np.load(f)
def combine(data):
new_data = []
window = 4 # number of sentences to combine
stride = 1 # number of sentences to 'stride' over, used to create overlap
for i in range(0, len(data), stride):
i_end = min(len(data)-1, i+window)
if data[i]['title'] != data[i_end]['title']:
# in this case we skip this entry as we have start/end of two videos
continue
text = ' '
for x in data[i:i_end]:
text += x['text']
new_data.append({
'start': data[i]['start'],
'end': data[i_end]['end'],
'title': data[i]['title'],
'text': text,
'id': data[i]['id'],
'url': data[i]['url'],
'published': data[i]['published']
})
return new_data
model_id = "multi-qa-mpnet-base-dot-v1"
model = SentenceTransformer(model_id)
meta_data = []
with open('Property-transcription.jsonl', 'r', encoding="utf-8") as f:
for line in f:
meta_data.append(json.loads(line))
meta_data = combine(meta_data)
with open('property-vectors.npy', 'rb') as f:
text_vector = np.load(f)
def card(thumbnail: str, title: str, urls: list, contexts: list, starts: list, ends: list):
meta = [(e, s, u, c) for e, s, u, c in zip(ends, starts, urls, contexts)]
meta.sort(reverse=False)
text_content = []
current_start = 0
current_end = 0
for end, start, url, context in meta:
# reformat seconds to timestamp
time = start / 60
mins = f"0{int(time)}"[-2:]
secs = f"0{int(round((time - int(mins))*60, 0))}"[-2:]
timestamp = f"{mins}:{secs}"
if start < current_end and start > current_start:
# this means it is a continuation of the previous sentence
text_content[-1][0] = text_content[-1][0].split(context[:10])[0]
text_content.append([f"[{timestamp}] {context.capitalize()}", url])
else:
text_content.append(["xxLINEBREAKxx", ""])
text_content.append([f"[{timestamp}] {context}", url])
current_start = start
current_end = end
html_text = ""
for text, url in text_content:
if text == "xxLINEBREAKxx":
html_text += "
"
else:
html_text += f"{text.strip()}...
"
print(html_text)
html = f"""