Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import networkx as nx
|
|
| 7 |
import logging
|
| 8 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 9 |
import re
|
| 10 |
-
import os
|
| 11 |
|
| 12 |
# Set up logging configuration
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -20,98 +19,116 @@ def install_spacy_model():
|
|
| 20 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 21 |
install_spacy_model()
|
| 22 |
|
| 23 |
-
# Load the
|
| 24 |
nlp = spacy.load("en_core_web_sm")
|
| 25 |
|
| 26 |
# Neo4j credentials
|
| 27 |
-
uri =
|
| 28 |
-
username =
|
| 29 |
-
password =
|
| 30 |
-
|
| 31 |
-
# Connect to Neo4j
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
-
def
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
for
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import logging
|
| 8 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 9 |
import re
|
|
|
|
| 10 |
|
| 11 |
# Set up logging configuration
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 19 |
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 20 |
install_spacy_model()
|
| 21 |
|
| 22 |
+
# Load the model after ensuring it's installed
|
| 23 |
nlp = spacy.load("en_core_web_sm")
|
| 24 |
|
| 25 |
# Neo4j credentials
|
| 26 |
+
uri = "neo4j+s://ff701b1c.databases.neo4j.io"
|
| 27 |
+
username = "neo4j"
|
| 28 |
+
password = "BfZM7YRKpFz1b_V7acAmOtaSQHPU9xK03rJlfPep88g"
|
| 29 |
+
|
| 30 |
+
# Connect to Neo4j
|
| 31 |
+
driver = None
|
| 32 |
+
try:
|
| 33 |
+
driver = GraphDatabase.driver(uri, auth=(username, password))
|
| 34 |
+
logging.info("β
Connected to Neo4j!")
|
| 35 |
+
|
| 36 |
+
def close_driver():
|
| 37 |
+
if driver:
|
| 38 |
+
driver.close()
|
| 39 |
+
logging.info("π Neo4j driver closed.")
|
| 40 |
+
|
| 41 |
+
def create_entity(tx, name: str):
|
| 42 |
+
tx.run("MERGE (e:Entity {name: $name})", name=name)
|
| 43 |
+
|
| 44 |
+
def create_relationship(tx, subj: str, pred: str, obj: str):
|
| 45 |
+
tx.run("""
|
| 46 |
+
MERGE (a:Entity {name: $subj})
|
| 47 |
+
MERGE (b:Entity {name: $obj})
|
| 48 |
+
MERGE (a)-[:RELATION {name: $pred}]->(b)
|
| 49 |
+
""", subj=subj, pred=pred, obj=obj)
|
| 50 |
+
|
| 51 |
+
# Text Processing
|
| 52 |
+
def load_and_clean_text(file_path: str) -> str:
|
| 53 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 54 |
+
text = file.read()
|
| 55 |
+
text = re.sub(r'\n+', ' ', text)
|
| 56 |
+
return re.sub(r'\s+', ' ', text).strip().lower()
|
| 57 |
+
|
| 58 |
+
# TF-IDF Filtering
|
| 59 |
+
def compute_tfidf_keywords(text: str, top_n=60):
|
| 60 |
+
vectorizer = TfidfVectorizer(stop_words='english')
|
| 61 |
+
X = vectorizer.fit_transform([text])
|
| 62 |
+
scores = zip(vectorizer.get_feature_names_out(), X.toarray()[0])
|
| 63 |
+
sorted_scores = sorted(scores, key=lambda x: x[1], reverse=True)
|
| 64 |
+
return {word for word, _ in sorted_scores[:top_n]}
|
| 65 |
+
|
| 66 |
+
# Triple Extraction
|
| 67 |
+
def get_full_phrase(token) -> str:
|
| 68 |
+
return ' '.join(tok.text for tok in token.subtree if tok.dep_ != 'punct').strip()
|
| 69 |
+
|
| 70 |
+
def extract_rich_triples(doc, tfidf_keywords) -> list:
|
| 71 |
+
triples = []
|
| 72 |
+
for sent in doc.sents:
|
| 73 |
+
subjects = [tok for tok in sent if "subj" in tok.dep_]
|
| 74 |
+
objects = [tok for tok in sent if "obj" in tok.dep_]
|
| 75 |
+
verbs = [tok for tok in sent if tok.pos_ == "VERB"]
|
| 76 |
+
for subj in subjects:
|
| 77 |
+
for obj in objects:
|
| 78 |
+
for verb in verbs:
|
| 79 |
+
s = get_full_phrase(subj)
|
| 80 |
+
o = get_full_phrase(obj)
|
| 81 |
+
if s.lower() in tfidf_keywords or o.lower() in tfidf_keywords:
|
| 82 |
+
triples.append((s, verb.lemma_, o))
|
| 83 |
+
return triples
|
| 84 |
+
|
| 85 |
+
# Graph Visualization
|
| 86 |
+
def visualize_knowledge_graph(triples: list, title: str = "Knowledge Graph"):
|
| 87 |
+
G = nx.DiGraph()
|
| 88 |
+
for subj, pred, obj in triples:
|
| 89 |
+
G.add_node(subj, label='Subject')
|
| 90 |
+
G.add_node(obj, label='Object')
|
| 91 |
+
G.add_edge(subj, obj, label=pred)
|
| 92 |
+
|
| 93 |
+
pos = nx.spring_layout(G, k=1.2, seed=42)
|
| 94 |
+
node_colors = ['skyblue' if G.nodes[n]['label'] == 'Subject' else 'lightgreen' for n in G.nodes]
|
| 95 |
+
|
| 96 |
+
plt.figure(figsize=(16, 16))
|
| 97 |
+
nx.draw(G, pos, with_labels=True, node_size=1200, node_color=node_colors,
|
| 98 |
+
font_size=10, font_weight='bold', edge_color='gray', alpha=0.8)
|
| 99 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): d['label'] for u, v, d in G.edges(data=True)},
|
| 100 |
+
font_size=8, font_color='red')
|
| 101 |
+
plt.title(title, fontsize=20)
|
| 102 |
+
plt.show()
|
| 103 |
+
|
| 104 |
+
# === Main Execution ===
|
| 105 |
+
file_path = r'C:\Users\jaiba\Desktop\KNOWLEDGE GRAPH\data2.txt'
|
| 106 |
+
text = load_and_clean_text(file_path)
|
| 107 |
+
tfidf_keywords = compute_tfidf_keywords(text)
|
| 108 |
+
|
| 109 |
+
doc = nlp(text)
|
| 110 |
+
|
| 111 |
+
triples = extract_rich_triples(doc, tfidf_keywords)
|
| 112 |
+
logging.info(f"π§ Extracted {len(triples)} filtered triples.")
|
| 113 |
+
for t in triples[:10]: # Print only the top 10 triplets
|
| 114 |
+
print("π", t)
|
| 115 |
+
|
| 116 |
+
# === Push to Neo4j ===
|
| 117 |
+
with driver.session() as session:
|
| 118 |
+
for subj, pred, obj in triples:
|
| 119 |
+
session.execute_write(create_entity, subj)
|
| 120 |
+
session.execute_write(create_entity, obj)
|
| 121 |
+
session.execute_write(create_relationship, subj, pred, obj)
|
| 122 |
+
|
| 123 |
+
logging.info("π‘ Triples successfully stored in Neo4j.")
|
| 124 |
+
print("π‘ Triples successfully stored in Neo4j.")
|
| 125 |
+
|
| 126 |
+
# === Final Visualization ===
|
| 127 |
+
visualize_knowledge_graph(triples, title="Filtered Knowledge Graph (TF-IDF)")
|
| 128 |
+
|
| 129 |
+
except Exception as e:
|
| 130 |
+
logging.error(f"β An error occurred: {e}", exc_info=True)
|
| 131 |
+
|
| 132 |
+
finally:
|
| 133 |
+
if driver:
|
| 134 |
+
close_driver()
|