Skip to content

Commit f9239d1

Browse files
Added webpage
1 parent dd0e797 commit f9239d1

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

KGG_webpage/index.html

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Knowledge Graph Generator Project Report</title>
8+
<link rel="stylesheet" href="https://latex.vercel.app/style.min.css" />
9+
<link rel="stylesheet" href="https://latex.vercel.app/prism/prism.css">
10+
<script src="https://cdn.jsdelivr.net/npm/prismjs/prism.min.js"></script>
11+
</head>
12+
13+
<body class="text-justify latex-auto">
14+
<header>
15+
<h1>Knowledge Graph Generator</h1>
16+
<p class="author">
17+
Ruhaan Choudhary<br>
18+
Ritesh Kumar
19+
</p>
20+
<p style="text-align:center;">
21+
<a href="https://github.com/coderuhaan2004/KnowledgeGraphGenerator" target="_blank">GitHub Repository</a>
22+
</p>
23+
</header>
24+
25+
<div class="abstract">
26+
<h2>Abstract</h2>
27+
<p>
28+
The Knowledge Graph Generator (KGG) is a tool designed to extract structured information from unstructured text and visualize it as a knowledge graph. Leveraging large language models and modern visualization libraries, KGG enables users to input any text and receive an interactive graph of entities and their relationships. The project is implemented in Python using HuggingFace Transformers, PyVis, and Google Colab for an accessible and interactive experience.
29+
</p>
30+
</div>
31+
32+
<nav role="navigation" class="toc">
33+
<h2>Contents</h2>
34+
<ol>
35+
<li><a href="#introduction">Introduction</a></li>
36+
<li><a href="#approach">Approach & Methodology</a></li>
37+
<li><a href="#features">Features</a></li>
38+
<li><a href="#applications">Applications</a></li>
39+
<li><a href="#algorithms">Algorithms & Implementation</a></li>
40+
<li><a href="#ui">User Interface</a></li>
41+
<li><a href="#conclusion">Conclusion</a></li>
42+
<li><a href="#bibliography">Bibliography</a></li>
43+
</ol>
44+
</nav>
45+
46+
<main>
47+
<article>
48+
<h2 id="introduction">Introduction</h2>
49+
<p>
50+
The Knowledge Graph Generator (KGG) is an AI-powered application that transforms unstructured text into a structured knowledge graph. By identifying key entities and their relationships, KGG provides a visual and interactive representation of information, aiding in understanding, exploration, and further analysis. The project is built using Python, HuggingFace Transformers, and PyVis, and is designed to run seamlessly in Google Colab.
51+
</p>
52+
53+
<h2 id="approach">Approach & Methodology</h2>
54+
<p>
55+
The core approach of KGG is unique in that, instead of relying on external APIs to access large language models (LLMs), the LLM is loaded and run entirely <b>locally</b> on the user's machine. This ensures data privacy, removes dependency on internet connectivity, and avoids API usage costs. However, running a state-of-the-art LLM such as Open-Orca/Mistral-7B-OpenOrca locally presents significant hardware challenges, as these models typically require more than 15GB of GPU memory.
56+
</p>
57+
<p>
58+
To overcome this, <b>quantization</b> techniques are employed. Specifically, the model is loaded using <code>BitsAndBytesConfig</code> with 4-bit quantization (nf4), drastically reducing the memory footprint and enabling efficient inference even on consumer-grade GPUs. This allows the entire pipeline—from prompt engineering to entity extraction and graph visualization—to be executed locally, making the solution both powerful and accessible.
59+
</p>
60+
<ol>
61+
<li><b>Model Loading and Quantization:</b> The Open-Orca/Mistral-7B-OpenOrca model is loaded locally using HuggingFace Transformers with 4-bit quantization (nf4) for efficient inference. This is achieved using the <code>BitsAndBytesConfig</code> for memory and speed optimization, making it feasible to run the model on hardware with limited GPU resources.</li>
62+
<li><b>Prompt Engineering:</b> A system prompt instructs the model to extract entities and relationships from the context and output them in a JSON format with fields: <code>node1</code>, <code>node2</code>, and <code>relationship</code>.</li>
63+
<li><b>Text Processing:</b> The user-provided text is formatted into a prompt and passed to the locally running model. The model generates a response, which is parsed to extract the JSON array of relationships.</li>
64+
<li><b>Knowledge Graph Construction:</b> The extracted entities and relationships are used to build a graph using PyVis, where nodes represent entities and edges represent relationships.</li>
65+
<li><b>Visualization:</b> The resulting graph is rendered as interactive HTML, allowing users to explore the knowledge graph visually within the notebook or exported as a standalone HTML file.</li>
66+
</ol>
67+
68+
<h2 id="features">Features</h2>
69+
<ul>
70+
<li><b>Automated Entity and Relationship Extraction:</b> Utilizes a large language model to identify and extract entities and their relationships from arbitrary text.</li>
71+
<li><b>Interactive Visualization:</b> Generates an interactive knowledge graph using PyVis, allowing users to explore nodes and edges dynamically.</li>
72+
<li><b>Efficient Model Inference:</b> Employs 4-bit quantization for the language model, reducing memory usage and improving inference speed.</li>
73+
<li><b>Google Colab Integration:</b> Designed for easy use in Google Colab, with a user-friendly interface for input and visualization.</li>
74+
<li><b>Customizable Prompts:</b> The prompt engineering approach allows for flexible extraction of different types of relationships as needed.</li>
75+
</ul>
76+
77+
<h2 id="applications">Applications</h2>
78+
<ul>
79+
<li><b>Information Retrieval and Organization</b>
80+
<ul>
81+
<li><b>Data Management:</b> Organize large volumes of textual data into structured, interconnected entities. Useful for creating databases or enhancing existing ones.</li>
82+
<li><b>Content Summarization:</b> Summarize key information from long documents or articles by extracting main entities and their relationships.</li>
83+
</ul>
84+
</li>
85+
<li><b>Education</b>
86+
<ul>
87+
<li><b>Teaching Aid:</b> Assist educators in creating interactive teaching materials by visually representing complex subjects and their interrelations.</li>
88+
<li><b>Student Projects:</b> Provide a tool for students to visualize and present their research or project findings.</li>
89+
</ul>
90+
</li>
91+
<li><b>Knowledge Discovery</b>
92+
<ul>
93+
<li><b>Research:</b> Aid researchers in identifying relationships between different concepts, facilitating new insights and hypothesis generation.</li>
94+
<li><b>Literature Reviews:</b> Summarize findings from numerous studies by mapping out key terms and their connections.</li>
95+
</ul>
96+
</li>
97+
</ul>
98+
99+
<h2 id="algorithms">Algorithms & Implementation</h2>
100+
<h3>Model Loading and Quantization</h3>
101+
<pre><code class="language-python">
102+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
103+
104+
bnb_config = BitsAndBytesConfig(
105+
load_in_4bit=True,
106+
bnb_4bit_quant_type="nf4",
107+
bnb_4bit_compute_dtype=torch.float16,
108+
bnb_4bit_use_double_quant=True,
109+
)
110+
111+
model_name = "Open-Orca/Mistral-7B-OpenOrca"
112+
model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=bnb_config)
113+
tokenizer = AutoTokenizer.from_pretrained(model_name)
114+
model.config.use_cache = False
115+
model.config.pretraining_tp = 1
116+
</code></pre>
117+
118+
<h3>Prompt Engineering and Extraction</h3>
119+
<pre><code class="language-python">
120+
def getprompt(text):
121+
SYS_PROMPT = (
122+
"You are an AI assistant tasked with extracting structured information from the context to create a knowledge graph. "
123+
"Your goal is to identify key entities and their relationships in the context and present this information in a JSON format "
124+
"with fields: 'node1', 'node2', and 'relationship'."
125+
)
126+
USER_PROMPT = f"context: ```{text}``` \\n\\n output: "
127+
PROMPT = f"{SYS_PROMPT}\\n\\n{USER_PROMPT}"
128+
return PROMPT
129+
130+
def function(text):
131+
prompt = getprompt(text)
132+
inputs = tokenizer.encode(prompt, return_tensors="pt")
133+
outputs = model.generate(inputs, max_length=1024, num_return_sequences=1)
134+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
135+
json_response = response.split("[")[1].split("]")[0]
136+
json_response = "[\\n" + json_response + "]"
137+
json_response = json.loads(json_response)
138+
return json_response
139+
</code></pre>
140+
141+
<h3>Knowledge Graph Construction and Visualization</h3>
142+
<pre><code class="language-python">
143+
from pyvis.network import Network
144+
145+
def generate_knowledge_graph(text):
146+
data = function(text)
147+
net = Network(notebook=True, directed=True, cdn_resources='remote')
148+
for relation in data:
149+
net.add_node(relation['node1'], label=relation['node1'], title=relation['node1'])
150+
net.add_node(relation['node2'], label=relation['node2'], title=relation['node2'])
151+
net.add_edge(relation['node1'], relation['node2'], title=relation['relationship'], label=relation['relationship'])
152+
net.repulsion(node_distance=180, spring_length=100)
153+
return net.generate_html()
154+
</code></pre>
155+
156+
<h2 id="ui">User Interface</h2>
157+
<p>
158+
The user interface is implemented using HTML and JavaScript within the Colab notebook. Users can input text into a search bar, and upon clicking the search button, the knowledge graph is generated and displayed interactively. The UI is styled for clarity and ease of use, with responsive design and dynamic feedback.
159+
</p>
160+
<figure>
161+
<img src="ui_screenshot.png" alt="Knowledge Graph Generator UI" style="width:90%;">
162+
<figcaption>Knowledge Graph Generator User Interface</figcaption>
163+
</figure>
164+
<figure>
165+
<img src="knowledgeGraph.png" alt="Example Knowledge Graph" style="width:90%;">
166+
<figcaption>Example Output: Generated Knowledge Graph</figcaption>
167+
</figure>
168+
169+
<h2 id="conclusion">Conclusion</h2>
170+
<p>
171+
The Knowledge Graph Generator project demonstrates the power of combining large language models with interactive visualization tools to extract and represent structured knowledge from unstructured text. By automating the process of entity and relationship extraction and providing an intuitive interface, KGG makes knowledge discovery accessible and efficient for users in research, education, and industry.
172+
</p>
173+
174+
<h2 id="bibliography">Bibliography</h2>
175+
<ul>
176+
<li>Open-Orca/Mistral-7B-OpenOrca: <a href="https://huggingface.co/Open-Orca/Mistral-7B-OpenOrca" target="_blank">HuggingFace Model Card</a></li>
177+
<li>PyVis Documentation: <a href="https://pyvis.readthedocs.io/en/latest/" target="_blank">https://pyvis.readthedocs.io/en/latest/</a></li>
178+
<li>HuggingFace Transformers: <a href="https://huggingface.co/docs/transformers/index" target="_blank">https://huggingface.co/docs/transformers/index</a></li>
179+
<li>Google Colab: <a href="https://colab.research.google.com/" target="_blank">https://colab.research.google.com/</a></li>
180+
</ul>
181+
</article>
182+
</main>
183+
</body>
184+
</html>

KGG_webpage/knowledgeGraph.png

63.7 KB
Loading

KGG_webpage/ui_screenshot.png

29.6 KB
Loading

0 commit comments

Comments
 (0)