Spaces:
Runtime error
Runtime error
Create spacy.py
Browse files
spacy.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import spacy
|
| 3 |
+
import networkx as nx
|
| 4 |
+
import plotly.graph_objs as go
|
| 5 |
+
|
| 6 |
+
# Load the spaCy model
|
| 7 |
+
nlp = spacy.load("en_core_web_sm")
|
| 8 |
+
|
| 9 |
+
def create_knowledge_graph(text):
|
| 10 |
+
# Process the text using spaCy
|
| 11 |
+
doc = nlp(text)
|
| 12 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 13 |
+
|
| 14 |
+
# Create a graph
|
| 15 |
+
G = nx.Graph()
|
| 16 |
+
for entity, label in entities:
|
| 17 |
+
G.add_node(entity, label=label)
|
| 18 |
+
|
| 19 |
+
# Add edges (for simplicity, connect each entity to the next)
|
| 20 |
+
for i in range(len(entities) - 1):
|
| 21 |
+
G.add_edge(entities[i][0], entities[i + 1][0])
|
| 22 |
+
|
| 23 |
+
return G, entities
|
| 24 |
+
|
| 25 |
+
def plot_graph(G):
|
| 26 |
+
pos = nx.spring_layout(G) # positions for all nodes
|
| 27 |
+
edges = G.edges()
|
| 28 |
+
edge_x = []
|
| 29 |
+
edge_y = []
|
| 30 |
+
|
| 31 |
+
for edge in edges:
|
| 32 |
+
x0, y0 = pos[edge[0]]
|
| 33 |
+
x1, y1 = pos[edge[1]]
|
| 34 |
+
edge_x.append(x0)
|
| 35 |
+
edge_x.append(x1)
|
| 36 |
+
edge_x.append(None) # None to break line
|
| 37 |
+
edge_y.append(y0)
|
| 38 |
+
edge_y.append(y1)
|
| 39 |
+
edge_y.append(None) # None to break line
|
| 40 |
+
|
| 41 |
+
node_x = []
|
| 42 |
+
node_y = []
|
| 43 |
+
for node in G.nodes():
|
| 44 |
+
x, y = pos[node]
|
| 45 |
+
node_x.append(x)
|
| 46 |
+
node_y.append(y)
|
| 47 |
+
|
| 48 |
+
# Create Plotly figure
|
| 49 |
+
fig = go.Figure()
|
| 50 |
+
|
| 51 |
+
# Add edges to figure
|
| 52 |
+
fig.add_trace(go.Scatter(
|
| 53 |
+
x=edge_x, y=edge_y,
|
| 54 |
+
line=dict(width=0.5, color='black'),
|
| 55 |
+
hoverinfo='none',
|
| 56 |
+
mode='lines'))
|
| 57 |
+
|
| 58 |
+
# Add nodes to figure
|
| 59 |
+
fig.add_trace(go.Scatter(
|
| 60 |
+
x=node_x, y=node_y,
|
| 61 |
+
mode='markers+text',
|
| 62 |
+
text=list(G.nodes()),
|
| 63 |
+
textposition="top center",
|
| 64 |
+
hoverinfo='text',
|
| 65 |
+
marker=dict(
|
| 66 |
+
showscale=True,
|
| 67 |
+
colorscale='YlGnBu',
|
| 68 |
+
size=10,
|
| 69 |
+
color=node_y,
|
| 70 |
+
colorbar=dict(thickness=15, title="Node Connections", xanchor='left', titleside='right'),
|
| 71 |
+
)))
|
| 72 |
+
|
| 73 |
+
fig.update_layout(showlegend=False, hovermode='closest',
|
| 74 |
+
margin=dict(b=0, t=0, l=0, r=0),
|
| 75 |
+
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
| 76 |
+
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
|
| 77 |
+
|
| 78 |
+
return fig
|
| 79 |
+
|
| 80 |
+
# Streamlit app
|
| 81 |
+
# st.title("Knowledge Graph Generator")
|
| 82 |
+
|
| 83 |
+
text_input = st.text_area("Enter your text here:")
|
| 84 |
+
if st.button("Generate Knowledge Graph"):
|
| 85 |
+
if text_input:
|
| 86 |
+
G, entities = create_knowledge_graph(text_input)
|
| 87 |
+
|
| 88 |
+
# Plot the graph using Plotly
|
| 89 |
+
fig = plot_graph(G)
|
| 90 |
+
st.plotly_chart(fig)
|
| 91 |
+
|
| 92 |
+
st.write("Extracted Entities:")
|
| 93 |
+
st.write(entities)
|
| 94 |
+
else:
|
| 95 |
+
st.warning("Please enter some text.")
|