-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_jsons.py
More file actions
51 lines (40 loc) · 1.54 KB
/
Copy pathprocess_jsons.py
File metadata and controls
51 lines (40 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import requests
import os
import json
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import joblib
def create_embedding(text_list):
r = requests.post("http://localhost:11434/api/embed", json={
"model": "bge-m3",
"input": text_list
})
embedding = r.json()["embeddings"]
return embedding
merged_jsons = sorted([f for f in os.listdir("merged_jsons") if f.endswith(".json")])
# print(jsons)
my_dicts = []
chunk_id = 0
for json_file in merged_jsons: # it will load all the (now merged) chunk of a particular video
with open(f"merged_jsons/{json_file}",encoding="utf-8") as f:
content = json.load(f)
print(f"creating embeddings for {json_file}")
embeddings = create_embedding([c["text"] for c in content["chunks"]])
for i, chunk in enumerate(content["chunks"]):
chunk["chunk_id"] = chunk_id
chunk["embedding"] = embeddings[i]
chunk_id +=1
my_dicts.append(chunk)
#if(i==5): #stoping for running example faster
# break
#break # stopping after first file for testing purpose
# print(my_dicts)
df = pd.DataFrame.from_records(my_dicts)
#print(df)
#saving this df using joblib
joblib.dump(df, "embeddings_df.joblib")
# df.to_csv("all_chunks_&_embeddings.csv") # saving all the chunks and embeddings in csv file not necessary though as we are saving in joblib format
#df.to_csv("testt.csv") # for testing purpose
# a = create_embedding(["cat sat on a mat", " I am sitting on a mat"])
# print(a)