Spaces:
Runtime error
Runtime error
Commit
•
fb5b606
1
Parent(s):
9884e92
Create app.0.py
Browse filesSatisfied with version 0. Want to add Laplacian centrality
app.0.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from datasets import load_dataset
|
3 |
+
import networkx as nx
|
4 |
+
import numpy as np
|
5 |
+
dataset = load_dataset("roneneldan/TinyStories")
|
6 |
+
|
7 |
+
st.write(dataset['train'][10]['text'])
|
8 |
+
|
9 |
+
threshhold = st.slider('Threshhold',0.0,1.0,step=0.1)
|
10 |
+
|
11 |
+
#-------------------------------------------------------------
|
12 |
+
#-------------------------------------------------------------
|
13 |
+
|
14 |
+
from sentence_transformers import SentenceTransformer, util
|
15 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
16 |
+
|
17 |
+
# Sentences from the data set
|
18 |
+
#sentences = [item['text'] for item in dataset['train'][:10]]
|
19 |
+
|
20 |
+
#sentences = [dataset['train'][0],dataset['train'][1],dataset['train'][2]]
|
21 |
+
sentences = [dataset['train'][ii] for ii in range(10)]
|
22 |
+
|
23 |
+
#Compute embedding
|
24 |
+
embeddings = model.encode(sentences, convert_to_tensor=True)
|
25 |
+
|
26 |
+
#Compute cosine-similarities
|
27 |
+
cosine_scores = util.cos_sim(embeddings, embeddings)
|
28 |
+
|
29 |
+
# creating adjacency matrix
|
30 |
+
A = np.zeros((len(sentences),len(sentences)))
|
31 |
+
|
32 |
+
#Output the pairs with their score
|
33 |
+
for i in range(len(sentences)):
|
34 |
+
for j in range(i):
|
35 |
+
#st.write("{} \t\t {} \t\t Score: {:.4f}".format(sentences[i], sentences[j], cosine_scores[i][j]))
|
36 |
+
A[i][j] = cosine_scores[i][j]
|
37 |
+
A[j][i] = cosine_scores[i][j]
|
38 |
+
|
39 |
+
#G = nx.from_numpy_array(A)
|
40 |
+
G = nx.from_numpy_array(cosine_scores.numpy()>threshhold)
|
41 |
+
|
42 |
+
|
43 |
+
#-------------------------------------------------------------
|
44 |
+
#-------------------------------------------------------------
|
45 |
+
# ego_graph.py
|
46 |
+
# An example of how to plot a node's ego network
|
47 |
+
# (egonet). This indirectly showcases slightly more involved
|
48 |
+
# interoperability between streamlit-agraph and networkx.
|
49 |
+
|
50 |
+
# An egonet can be # created from (almost) any network (graph),
|
51 |
+
# and exemplifies the # concept of a subnetwork (subgraph):
|
52 |
+
# A node's egonet is the (sub)network comprised of the focal node
|
53 |
+
# and all the nodes to whom it is adjacent. The edges included
|
54 |
+
# in the egonet are those nodes are both included in the aforementioned
|
55 |
+
# nodes.
|
56 |
+
|
57 |
+
# Use the following command to launch the app
|
58 |
+
# streamlit run <path-to-script>.py
|
59 |
+
|
60 |
+
# standard library dependencies
|
61 |
+
from operator import itemgetter
|
62 |
+
|
63 |
+
# external dependencies
|
64 |
+
import networkx as nx
|
65 |
+
from streamlit_agraph import agraph, Node, Edge, Config
|
66 |
+
|
67 |
+
# First create a graph using the Barabasi-Albert model
|
68 |
+
n = 2000
|
69 |
+
m = 2
|
70 |
+
#G = nx.generators.barabasi_albert_graph(n, m, seed=2023)
|
71 |
+
|
72 |
+
# Then find the node with the largest degree;
|
73 |
+
# This node's egonet will be the focus of this example.
|
74 |
+
node_and_degree = G.degree()
|
75 |
+
most_connected_node = sorted(G.degree, key=lambda x: x[1], reverse=True)[0]
|
76 |
+
degree = G.degree(most_connected_node)
|
77 |
+
|
78 |
+
# Create egonet for the focal node
|
79 |
+
hub_ego = nx.ego_graph(G, most_connected_node[0])
|
80 |
+
|
81 |
+
# Now create the equivalent Node and Edge lists
|
82 |
+
nodes = [Node(title=str(sentences[i]['text']), id=i, label='node_'+str(i), size=20) for i in hub_ego.nodes]
|
83 |
+
edges = [Edge(source=i, target=j, type="CURVE_SMOOTH") for (i,j) in G.edges
|
84 |
+
if i in hub_ego.nodes and j in hub_ego.nodes]
|
85 |
+
|
86 |
+
|
87 |
+
config = Config(width=500,
|
88 |
+
height=500,
|
89 |
+
directed=True,
|
90 |
+
nodeHighlightBehavior=False,
|
91 |
+
highlightColor="#F7A7A6", # or "blue"
|
92 |
+
collapsible=False,
|
93 |
+
node={'labelProperty':'label'},
|
94 |
+
# **kwargs e.g. node_size=1000 or node_color="blue"
|
95 |
+
)
|
96 |
+
|
97 |
+
return_value = agraph(nodes=nodes,
|
98 |
+
edges=edges,
|
99 |
+
config=config)
|