Spaces:
Sleeping
Sleeping
Update pages/21_GraphRag.py
Browse files- pages/21_GraphRag.py +60 -46
pages/21_GraphRag.py
CHANGED
|
@@ -1,46 +1,60 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModel
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import networkx as nx
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Load pre-trained model and tokenizer
|
| 8 |
+
model_name = "bert-base-uncased"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModel.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
# Function to get embeddings
|
| 13 |
+
def get_embeddings(texts):
|
| 14 |
+
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
return outputs.last_hidden_state[:, 0, :].numpy()
|
| 18 |
+
|
| 19 |
+
# Sample data (replace with your own data import)
|
| 20 |
+
documents = [
|
| 21 |
+
"The quick brown fox jumps over the lazy dog.",
|
| 22 |
+
"A journey of a thousand miles begins with a single step.",
|
| 23 |
+
"To be or not to be, that is the question.",
|
| 24 |
+
"All that glitters is not gold.",
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
# Get embeddings for documents
|
| 28 |
+
embeddings = get_embeddings(documents)
|
| 29 |
+
|
| 30 |
+
# Create graph
|
| 31 |
+
G = nx.Graph()
|
| 32 |
+
|
| 33 |
+
# Add nodes and edges based on cosine similarity
|
| 34 |
+
threshold = 0.5 # Adjust this threshold as needed
|
| 35 |
+
for i in range(len(documents)):
|
| 36 |
+
G.add_node(i, text=documents[i])
|
| 37 |
+
for j in range(i+1, len(documents)):
|
| 38 |
+
similarity = torch.cosine_similarity(torch.tensor(embeddings[i]), torch.tensor(embeddings[j]), dim=0)
|
| 39 |
+
if similarity > threshold:
|
| 40 |
+
G.add_edge(i, j, weight=similarity.item())
|
| 41 |
+
|
| 42 |
+
# Visualize the graph
|
| 43 |
+
pos = nx.spring_layout(G)
|
| 44 |
+
nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=500, font_size=8, font_weight='bold')
|
| 45 |
+
edge_labels = nx.get_edge_attributes(G, 'weight')
|
| 46 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
|
| 47 |
+
plt.title("Document Similarity Graph")
|
| 48 |
+
plt.show()
|
| 49 |
+
|
| 50 |
+
# Example of querying the graph
|
| 51 |
+
query = "What is the meaning of life?"
|
| 52 |
+
query_embedding = get_embeddings([query])[0]
|
| 53 |
+
|
| 54 |
+
# Find most similar document
|
| 55 |
+
similarities = [torch.cosine_similarity(torch.tensor(query_embedding), torch.tensor(emb), dim=0) for emb in embeddings]
|
| 56 |
+
most_similar_idx = max(range(len(similarities)), key=similarities.__getitem__)
|
| 57 |
+
|
| 58 |
+
print(f"Most similar document to the query: {documents[most_similar_idx]}")
|
| 59 |
+
|
| 60 |
+
# You can extend this to implement more complex graph-based retrieval algorithms
|