Spaces:
Runtime error
Runtime error
Update spacy_app.py
Browse files- spacy_app.py +11 -12
spacy_app.py
CHANGED
|
@@ -9,23 +9,20 @@ spacy.cli.download("en_core_web_sm")
|
|
| 9 |
nlp = spacy.load("en_core_web_sm")
|
| 10 |
|
| 11 |
def create_knowledge_graph(text):
|
| 12 |
-
# Process the text using spaCy
|
| 13 |
doc = nlp(text)
|
| 14 |
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 15 |
|
| 16 |
-
# Create a graph
|
| 17 |
G = nx.Graph()
|
| 18 |
for entity, label in entities:
|
| 19 |
G.add_node(entity, label=label)
|
| 20 |
|
| 21 |
-
# Add edges (for simplicity, connect each entity to the next)
|
| 22 |
for i in range(len(entities) - 1):
|
| 23 |
G.add_edge(entities[i][0], entities[i + 1][0])
|
| 24 |
|
| 25 |
return G, entities
|
| 26 |
|
| 27 |
def plot_graph(G):
|
| 28 |
-
pos = nx.spring_layout(G)
|
| 29 |
edges = G.edges()
|
| 30 |
edge_x = []
|
| 31 |
edge_y = []
|
|
@@ -35,10 +32,10 @@ def plot_graph(G):
|
|
| 35 |
x1, y1 = pos[edge[1]]
|
| 36 |
edge_x.append(x0)
|
| 37 |
edge_x.append(x1)
|
| 38 |
-
edge_x.append(None)
|
| 39 |
edge_y.append(y0)
|
| 40 |
edge_y.append(y1)
|
| 41 |
-
edge_y.append(None)
|
| 42 |
|
| 43 |
node_x = []
|
| 44 |
node_y = []
|
|
@@ -47,17 +44,14 @@ def plot_graph(G):
|
|
| 47 |
node_x.append(x)
|
| 48 |
node_y.append(y)
|
| 49 |
|
| 50 |
-
# Create Plotly figure
|
| 51 |
fig = go.Figure()
|
| 52 |
|
| 53 |
-
# Add edges to figure
|
| 54 |
fig.add_trace(go.Scatter(
|
| 55 |
x=edge_x, y=edge_y,
|
| 56 |
line=dict(width=0.5, color='black'),
|
| 57 |
hoverinfo='none',
|
| 58 |
mode='lines'))
|
| 59 |
|
| 60 |
-
# Add nodes to figure
|
| 61 |
fig.add_trace(go.Scatter(
|
| 62 |
x=node_x, y=node_y,
|
| 63 |
mode='markers+text',
|
|
@@ -82,16 +76,21 @@ def plot_graph(G):
|
|
| 82 |
# Streamlit app
|
| 83 |
# st.title("Knowledge Graph Generator")
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
if st.button("Generate Knowledge Graph"):
|
| 87 |
if text_input:
|
| 88 |
G, entities = create_knowledge_graph(text_input)
|
| 89 |
|
| 90 |
-
# Plot the graph using Plotly
|
| 91 |
fig = plot_graph(G)
|
| 92 |
st.plotly_chart(fig)
|
| 93 |
|
| 94 |
st.write("Extracted Entities:")
|
| 95 |
st.write(entities)
|
| 96 |
else:
|
| 97 |
-
st.warning("Please enter some text.")
|
|
|
|
| 9 |
nlp = spacy.load("en_core_web_sm")
|
| 10 |
|
| 11 |
def create_knowledge_graph(text):
|
|
|
|
| 12 |
doc = nlp(text)
|
| 13 |
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 14 |
|
|
|
|
| 15 |
G = nx.Graph()
|
| 16 |
for entity, label in entities:
|
| 17 |
G.add_node(entity, label=label)
|
| 18 |
|
|
|
|
| 19 |
for i in range(len(entities) - 1):
|
| 20 |
G.add_edge(entities[i][0], entities[i + 1][0])
|
| 21 |
|
| 22 |
return G, entities
|
| 23 |
|
| 24 |
def plot_graph(G):
|
| 25 |
+
pos = nx.spring_layout(G)
|
| 26 |
edges = G.edges()
|
| 27 |
edge_x = []
|
| 28 |
edge_y = []
|
|
|
|
| 32 |
x1, y1 = pos[edge[1]]
|
| 33 |
edge_x.append(x0)
|
| 34 |
edge_x.append(x1)
|
| 35 |
+
edge_x.append(None)
|
| 36 |
edge_y.append(y0)
|
| 37 |
edge_y.append(y1)
|
| 38 |
+
edge_y.append(None)
|
| 39 |
|
| 40 |
node_x = []
|
| 41 |
node_y = []
|
|
|
|
| 44 |
node_x.append(x)
|
| 45 |
node_y.append(y)
|
| 46 |
|
|
|
|
| 47 |
fig = go.Figure()
|
| 48 |
|
|
|
|
| 49 |
fig.add_trace(go.Scatter(
|
| 50 |
x=edge_x, y=edge_y,
|
| 51 |
line=dict(width=0.5, color='black'),
|
| 52 |
hoverinfo='none',
|
| 53 |
mode='lines'))
|
| 54 |
|
|
|
|
| 55 |
fig.add_trace(go.Scatter(
|
| 56 |
x=node_x, y=node_y,
|
| 57 |
mode='markers+text',
|
|
|
|
| 76 |
# Streamlit app
|
| 77 |
# st.title("Knowledge Graph Generator")
|
| 78 |
|
| 79 |
+
# File uploader
|
| 80 |
+
uploaded_file = st.file_uploader("Upload a text file", type="txt")
|
| 81 |
+
if uploaded_file is not None:
|
| 82 |
+
text_input = uploaded_file.read().decode("utf-8")
|
| 83 |
+
else:
|
| 84 |
+
text_input = st.text_area("Or enter your text here:")
|
| 85 |
+
|
| 86 |
if st.button("Generate Knowledge Graph"):
|
| 87 |
if text_input:
|
| 88 |
G, entities = create_knowledge_graph(text_input)
|
| 89 |
|
|
|
|
| 90 |
fig = plot_graph(G)
|
| 91 |
st.plotly_chart(fig)
|
| 92 |
|
| 93 |
st.write("Extracted Entities:")
|
| 94 |
st.write(entities)
|
| 95 |
else:
|
| 96 |
+
st.warning("Please enter some text or upload a file.")
|