Skip to content

Commit

Permalink
Merge pull request #15 from harishsiravuri/14-update-knowledge-graph
Browse files Browse the repository at this point in the history
14 update knowledge graph
  • Loading branch information
harishsiravuri committed Dec 3, 2023
2 parents 04883a8 + f89a569 commit 2cdb4ab
Show file tree
Hide file tree
Showing 8 changed files with 446 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ It's time to point you to the official [Documentation Website](https://harishsir

## Contributing
If you'd like to contribute, be sure to check out our [contributing guide](./CONTRIBUTING.md)! If you'd like to work on any outstanding items, check out the `roadmap` section in the docs and get started :smiley:

Thanks goes to these incredible people.

<a href="https://github.com/harishsiravuri/kgforge/graphs/contributors">
<img style="border-radius: 50%" src="https://contrib.rocks/image?repo=harishsiravuri/kgforge" />
</a>
1 change: 1 addition & 0 deletions kgforge/data_models/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class PromptResponse(BaseModel):
"""

concept: str
score: float
prompt_response: str


Expand Down
92 changes: 86 additions & 6 deletions kgforge/kg/kg_construct.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import errno
import logging
import os
from typing import List
Expand All @@ -6,6 +7,8 @@
import networkx as nx
import transformers
from transformers import pipeline
import pickle
import json

from kgforge.config import KGConfig
from kgforge.data_models import Prompt, PromptResponse, ResearchArtifact
Expand Down Expand Up @@ -125,19 +128,29 @@ def answer_question(
"""
if artifact is None:
logger.info("Artifact is needed to answer the question.")
return PromptResponse(concept=prompt.concept, prompt_response="Unavailable")
return PromptResponse(
concept=prompt.concept, score=0, prompt_response="Unavailable"
)
if artifact.full_text is None:
logger.info("Full text not found.")
return PromptResponse(concept=prompt.concept, prompt_response="Unavailable")
return PromptResponse(
concept=prompt.concept, score=0, prompt_response="Unavailable"
)
if prompt.question == "":
raise ValueError("Question cannot be empty")
try:
nlp = pipeline(task="question-answering", model=self.config.model_name)
res = nlp(question=prompt.question, context=artifact.full_text)
return PromptResponse(concept=prompt.concept, prompt_response=res)
return PromptResponse(
concept=prompt.concept,
score=res.get("score", 0),
prompt_response=res.get("answer", "Unavailable"),
)
except transformers.pipelines.base.PipelineException:
logger.error("Error while answering question")
return PromptResponse(concept=prompt.concept, prompt_response="Unavailable")
return PromptResponse(
concept=prompt.concept, score=0, prompt_response="Unavailable"
)

def construct_kg(self) -> None:
"""Constructs knowledge graph using the list of documents
Expand Down Expand Up @@ -181,6 +194,69 @@ def construct_kg(self) -> None:
except Exception as e:
logger.info("Error while constructing the knowledge graph: " + str(e))

def read_graph(self, path: str) -> None:
"""Reads the graph from a file
Usage example:
>>>kg = KnowledgeGraph()
>>>kg.read_graph("kg.pickle")
Args:
path (str): Path to the file where the graph is to be read from
Returns:
None: Reads the graph from a file
Raises:
ValueError: If the path is empty
FileNotFoundError: If the file is not found
"""
if path is None:
raise ValueError("Path cannot be empty")
else:
if not os.path.isfile(path):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path)
else:
with open(path, "rb") as f:
self.graph = pickle.load(f)

def write_graph(self, path: str) -> None:
"""Writes the graph to a file
Usage example:
>>>kg = KnowledgeGraph()
>>>kg.write_graph("kg.pickle")
Args:
path (str): Path to the file where the graph is to be written
Returns:
None: Writes the graph to a file
Raises:
ValueError: If the path is empty
"""
try:
node_arr = []
edge_arr = []

for node in list(self.graph.nodes(data=True)):
node_arr.append(node)

for edge in list(self.graph.edges()):
edge_arr.append(edge)

graph_dict = {"nodes": node_arr, "edges": edge_arr}
with open(path, "w") as f:
json.dump(graph_dict, f, indent=4)
except:
pass
# if path is not None and self.graph is not None:
# with open(path, "wb") as f:
# pickle.dump(self.graph, f)
# else:
# raise ValueError("Path cannot be empty")

def visualize_kg(self, file_path: str = "graph.png"):
"""Visualizes the knowledge graph
Expand All @@ -196,6 +272,10 @@ def visualize_kg(self, file_path: str = "graph.png"):
Raises:
None
"""
pos = nx.spring_layout(self.graph)
nx.draw(self.graph, pos=pos, with_labels=True, font_weight="bold")
pos = nx.spring_layout(self.graph, k=0.7, iterations=50)
nx.draw(self.graph, pos=pos, with_labels=False, font_weight="bold")
ax = plt.gca()
ax.set_aspect('equal')
ax.set_axis_off()

plt.savefig(file_path, format="PNG")
Binary file added tests/test_data/single_graph.json
Binary file not shown.
Loading

0 comments on commit 2cdb4ab

Please sign in to comment.