Krish Patel commited on
Commit
f53687d
·
1 Parent(s): 1c838ea

Saving the updated knowledge graph

Browse files
Files changed (3) hide show
  1. final.py +11 -1
  2. requirements.txt +1 -0
  3. save_model.py +43 -0
final.py CHANGED
@@ -89,7 +89,7 @@ def extract_entities(text, nlp):
89
  entities = [(ent.text, ent.label_) for ent in doc.ents]
90
  return entities
91
 
92
- def update_knowledge_graph(text, is_real, knowledge_graph, nlp):
93
  """Update knowledge graph with new information"""
94
  entities = extract_entities(text, nlp)
95
  for entity, entity_type in entities:
@@ -117,6 +117,16 @@ def update_knowledge_graph(text, is_real, knowledge_graph, nlp):
117
  )
118
  else:
119
  knowledge_graph[entity1][entity2]['weight'] += 1
 
 
 
 
 
 
 
 
 
 
120
 
121
  def predict_with_knowledge_graph(text, knowledge_graph, nlp):
122
  """Make predictions using the knowledge graph"""
 
89
  entities = [(ent.text, ent.label_) for ent in doc.ents]
90
  return entities
91
 
92
+ def update_knowledge_graph(text, is_real, knowledge_graph, nlp, save=True, push_to_hf=True):
93
  """Update knowledge graph with new information"""
94
  entities = extract_entities(text, nlp)
95
  for entity, entity_type in entities:
 
117
  )
118
  else:
119
  knowledge_graph[entity1][entity2]['weight'] += 1
120
+ if save:
121
+ from save_model import save_knowledge_graph, push_to_huggingface
122
+ filepath = save_knowledge_graph(knowledge_graph)
123
+
124
+ # Push to Hugging Face if requested
125
+ if push_to_hf:
126
+ repo_id = os.getenv("HF_REPO_ID", "HeheBoi0769/Nexus_NLP_model")
127
+ push_to_huggingface(filepath, repo_id)
128
+
129
+ return knowledge_graph
130
 
131
  def predict_with_knowledge_graph(text, knowledge_graph, nlp):
132
  """Make predictions using the knowledge graph"""
requirements.txt CHANGED
@@ -13,4 +13,5 @@ sentencepiece
13
  timm
14
  plotly
15
  networkx
 
16
  https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.0/en_core_web_sm-3.7.0.tar.gz
 
13
  timm
14
  plotly
15
  networkx
16
+ huggingface_hub
17
  https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.7.0/en_core_web_sm-3.7.0.tar.gz
save_model.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import os
3
+ from huggingface_hub import HfApi, login
4
+ import networkx as nx
5
+
6
+ def save_knowledge_graph(knowledge_graph, filepath="./knowledge_graph_final.pkl"):
7
+ """Save the knowledge graph to a local file"""
8
+ # Convert the graph to a serializable format
9
+ graph_data = {
10
+ 'nodes': {node: data for node, data in knowledge_graph.nodes(data=True)},
11
+ 'edges': {u: {v: data for v, data in knowledge_graph[u].items()}
12
+ for u in knowledge_graph.nodes()}
13
+ }
14
+
15
+ # Save to file
16
+ with open(filepath, 'wb') as f:
17
+ pickle.dump(graph_data, f)
18
+
19
+ print(f"Knowledge graph saved to {filepath}")
20
+ return filepath
21
+
22
+ def push_to_huggingface(filepath, repo_id, token=None):
23
+ """Push the saved knowledge graph to Hugging Face Hub"""
24
+ if token is None:
25
+ token = os.getenv("HF_TOKEN")
26
+ if not token:
27
+ raise ValueError("No Hugging Face token provided. Set HF_TOKEN environment variable or pass token parameter.")
28
+
29
+ # Login to Hugging Face
30
+ login(token=token)
31
+
32
+ # Initialize the Hugging Face API
33
+ api = HfApi()
34
+
35
+ # Upload the file
36
+ api.upload_file(
37
+ path_or_fileobj=filepath,
38
+ path_in_repo="knowledge_graph_final.pkl",
39
+ repo_id=repo_id,
40
+ repo_type="model"
41
+ )
42
+
43
+ print(f"Knowledge graph pushed to Hugging Face Hub: {repo_id}")