Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import NoReturn
|
| 2 |
+
import spacy
|
| 3 |
+
import networkx as nx
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# Load the spaCy model for dependency parsing
|
| 10 |
+
nlp = spacy.load("en_core_web_sm")
|
| 11 |
+
|
| 12 |
+
# Function to extract entities using NER
|
| 13 |
+
def extract_entities(text):
|
| 14 |
+
doc = nlp(text)
|
| 15 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 16 |
+
return entities
|
| 17 |
+
|
| 18 |
+
# Function to extract relationships dynamically from the text
|
| 19 |
+
def extract_relationships(text):
|
| 20 |
+
relationships = []
|
| 21 |
+
doc = nlp(text.lower())
|
| 22 |
+
subject, verb, obj, Noun = None, None, None, None
|
| 23 |
+
entities = []
|
| 24 |
+
for token in doc:
|
| 25 |
+
if token.dep_ in ("compound"):
|
| 26 |
+
Noun = token.text + " "
|
| 27 |
+
continue
|
| 28 |
+
if not Noun:
|
| 29 |
+
if token.dep_ in ("nsubj", "nsubjpass"):
|
| 30 |
+
subject = token.text
|
| 31 |
+
if token.dep_ in ("dobj", "attr", "pobj"):
|
| 32 |
+
obj = token.text
|
| 33 |
+
entities.append(obj)
|
| 34 |
+
if token.dep_ in ("ROOT", "xcomp", "ccomp"):
|
| 35 |
+
verb = token.text
|
| 36 |
+
elif Noun:
|
| 37 |
+
if token.dep_ in ("nsubj", "nsubjpass"):
|
| 38 |
+
subject = Noun
|
| 39 |
+
entities.append(subject)
|
| 40 |
+
if token.dep_ in ("dobj", "attr", "pobj"):
|
| 41 |
+
obj = Noun
|
| 42 |
+
entities.append(obj)
|
| 43 |
+
Noun = None
|
| 44 |
+
if token.dep_ == "prep":
|
| 45 |
+
subject = entities[-1]
|
| 46 |
+
if token.head.dep_ == "ROOT":
|
| 47 |
+
verb = token.head.text + " " + token.text
|
| 48 |
+
else:
|
| 49 |
+
verb = token.text
|
| 50 |
+
if subject and verb and obj:
|
| 51 |
+
relationships.append((subject.strip(), verb.strip(), obj.strip()))
|
| 52 |
+
subject, verb, obj = None, None, None
|
| 53 |
+
return relationships, entities
|
| 54 |
+
|
| 55 |
+
# Function to create the knowledge graph
|
| 56 |
+
def create_knowledge_graph(entities, relationships):
|
| 57 |
+
G = nx.DiGraph()
|
| 58 |
+
involved_entities = set()
|
| 59 |
+
for subj, rel, obj in relationships:
|
| 60 |
+
involved_entities.add(subj)
|
| 61 |
+
involved_entities.add(obj)
|
| 62 |
+
for entity in involved_entities:
|
| 63 |
+
G.add_node(entity)
|
| 64 |
+
for subj, rel, obj in relationships:
|
| 65 |
+
G.add_edge(subj, obj, label=rel)
|
| 66 |
+
return G
|
| 67 |
+
|
| 68 |
+
# Function to visualize the graph
|
| 69 |
+
def visualize_graph(G):
|
| 70 |
+
pos = nx.spring_layout(G)
|
| 71 |
+
edge_labels = nx.get_edge_attributes(G, 'label')
|
| 72 |
+
plt.figure(figsize=(12, 8))
|
| 73 |
+
nx.draw(G, pos, with_labels=True, node_size=2000, node_color="lightblue", font_size=10, font_weight="bold")
|
| 74 |
+
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
|
| 75 |
+
buf = io.BytesIO()
|
| 76 |
+
plt.savefig(buf, format="png")
|
| 77 |
+
buf.seek(0)
|
| 78 |
+
plt.close()
|
| 79 |
+
pil_image = Image.open(buf)
|
| 80 |
+
return pil_image
|
| 81 |
+
|
| 82 |
+
# Function to process input and generate output
|
| 83 |
+
def process_text(text: str):
|
| 84 |
+
relationships, entities = extract_relationships(text)
|
| 85 |
+
G = create_knowledge_graph(entities, relationships)
|
| 86 |
+
return visualize_graph(G)
|
| 87 |
+
|
| 88 |
+
# Gradio Interface
|
| 89 |
+
gr.Interface(
|
| 90 |
+
fn=process_text,
|
| 91 |
+
inputs=gr.Textbox(placeholder="Enter knowledge prompt here"),
|
| 92 |
+
outputs=gr.Image(type="pil"),
|
| 93 |
+
title="Knowledge Graph Generator"
|
| 94 |
+
).launch()
|