FlixRec/db2pc.py

37 lines
1.0 KiB
Python
Raw Normal View History

2022-05-21 17:54:12 +01:00
import os
import pinecone
2022-05-21 17:18:26 +01:00
from database import *
import pandas as pd
from sentence_transformers import SentenceTransformer
2022-05-21 17:54:12 +01:00
from tqdm import tqdm
2022-05-21 17:18:26 +01:00
2022-05-21 17:54:12 +01:00
database_url = "sqlite:///jlm.db"
2022-05-21 17:18:26 +01:00
engine, Session = init_db_stuff(database_url)
2022-05-21 17:54:12 +01:00
PINECONE_KEY = os.getenv("PINECONE_API_DEFAULT")
pinecone.init(api_key=PINECONE_KEY, environment="us-west1-gcp")
index = pinecone.Index("movies")
2022-05-21 17:18:26 +01:00
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")
2022-05-21 17:54:12 +01:00
batch_size = 32
2022-05-21 17:18:26 +01:00
df = pd.read_sql("Select * from movies", engine)
df["combined_text"] = df["title"] + ": " + df["overview"].fillna('') + " - " + df["tagline"].fillna('') + " Genres:- " + df["genres"].fillna('')
2022-05-21 18:11:59 +01:00
print(f'Length of Combined Text: {len(df["combined_text"].tolist())}')
2022-05-21 17:18:26 +01:00
2022-05-21 17:54:12 +01:00
for x in tqdm(range(0,len(df),batch_size)):
to_send = []
trakt_ids = df["trakt_id"][x:x+batch_size].tolist()
sentences = df["combined_text"][x:x+batch_size].tolist()
embeddings = model.encode(sentences)
for idx, value in enumerate(trakt_ids):
to_send.append(
2022-05-21 18:11:59 +01:00
(
str(value), embeddings[idx].tolist()
))
index.upsert(to_send)