Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ entity_extraction_prompt = PromptTemplate(
|
|
| 24 |
entity_chain = LLMChain(llm=llm, prompt=entity_extraction_prompt)
|
| 25 |
|
| 26 |
def create_knowledge_graph(text):
|
| 27 |
-
|
| 28 |
result = entity_chain.run(text)
|
| 29 |
|
| 30 |
# Parse the result and create a graph
|
|
@@ -47,6 +47,7 @@ def create_knowledge_graph(text):
|
|
| 47 |
return G
|
| 48 |
|
| 49 |
def visualize_graph(G):
|
|
|
|
| 50 |
pos = nx.spring_layout(G)
|
| 51 |
plt.figure(figsize=(12, 8))
|
| 52 |
nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=10, font_weight='bold')
|
|
@@ -58,6 +59,7 @@ def visualize_graph(G):
|
|
| 58 |
return plt
|
| 59 |
|
| 60 |
def generate_knowledge_graph(text):
|
|
|
|
| 61 |
if text:
|
| 62 |
knowledge_graph = create_knowledge_graph(text)
|
| 63 |
fig = visualize_graph(knowledge_graph)
|
|
@@ -73,6 +75,20 @@ footer_html = """
|
|
| 73 |
<a href="https://girishwangikar.github.io/Girish_Wangikar_Portfolio.github.io/" target="_blank">Portfolio</a></p>
|
| 74 |
</footer>
|
| 75 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Gradio interface
|
| 77 |
iface = gr.Interface(
|
| 78 |
fn=generate_knowledge_graph,
|
|
@@ -80,6 +96,7 @@ iface = gr.Interface(
|
|
| 80 |
outputs=gr.Plot(),
|
| 81 |
title="Knowledge Graph Generator",
|
| 82 |
description="Enter text to generate a knowledge graph.",
|
|
|
|
| 83 |
theme="default",
|
| 84 |
css="""
|
| 85 |
footer {
|
|
@@ -99,9 +116,7 @@ iface = gr.Interface(
|
|
| 99 |
footer a:hover {
|
| 100 |
text-decoration: underline;
|
| 101 |
}
|
| 102 |
-
"""
|
| 103 |
-
article=footer_html
|
| 104 |
-
)
|
| 105 |
|
| 106 |
# Launch the interface
|
| 107 |
if __name__ == "__main__":
|
|
|
|
| 24 |
entity_chain = LLMChain(llm=llm, prompt=entity_extraction_prompt)
|
| 25 |
|
| 26 |
def create_knowledge_graph(text):
|
| 27 |
+
"""Extracts entities and relationships from text and creates a knowledge graph."""
|
| 28 |
result = entity_chain.run(text)
|
| 29 |
|
| 30 |
# Parse the result and create a graph
|
|
|
|
| 47 |
return G
|
| 48 |
|
| 49 |
def visualize_graph(G):
|
| 50 |
+
"""Visualizes the knowledge graph using matplotlib."""
|
| 51 |
pos = nx.spring_layout(G)
|
| 52 |
plt.figure(figsize=(12, 8))
|
| 53 |
nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=10, font_weight='bold')
|
|
|
|
| 59 |
return plt
|
| 60 |
|
| 61 |
def generate_knowledge_graph(text):
|
| 62 |
+
"""Generates and visualizes a knowledge graph from input text."""
|
| 63 |
if text:
|
| 64 |
knowledge_graph = create_knowledge_graph(text)
|
| 65 |
fig = visualize_graph(knowledge_graph)
|
|
|
|
| 75 |
<a href="https://girishwangikar.github.io/Girish_Wangikar_Portfolio.github.io/" target="_blank">Portfolio</a></p>
|
| 76 |
</footer>
|
| 77 |
"""
|
| 78 |
+
|
| 79 |
+
# Instructions for using the app
|
| 80 |
+
instructions_html = """
|
| 81 |
+
<div>
|
| 82 |
+
<h2>Instructions:</h2>
|
| 83 |
+
<ol>
|
| 84 |
+
<li>Enter text in the input box that contains entities and their relationships. For example, "Paris - capital of: France".</li>
|
| 85 |
+
<li>Click the "Submit" button to generate the knowledge graph.</li>
|
| 86 |
+
<li>View the resulting knowledge graph visualization, which will display the entities as nodes and their relationships as labeled edges.</li>
|
| 87 |
+
<li>Feel free to experiment with different texts to explore relationships visually!</li>
|
| 88 |
+
</ol>
|
| 89 |
+
</div>
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
# Gradio interface
|
| 93 |
iface = gr.Interface(
|
| 94 |
fn=generate_knowledge_graph,
|
|
|
|
| 96 |
outputs=gr.Plot(),
|
| 97 |
title="Knowledge Graph Generator",
|
| 98 |
description="Enter text to generate a knowledge graph.",
|
| 99 |
+
article=instructions_html + footer_html,
|
| 100 |
theme="default",
|
| 101 |
css="""
|
| 102 |
footer {
|
|
|
|
| 116 |
footer a:hover {
|
| 117 |
text-decoration: underline;
|
| 118 |
}
|
| 119 |
+
""")
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# Launch the interface
|
| 122 |
if __name__ == "__main__":
|