hacpdsae2023's picture
fix bug on print vs write
f1aa832
raw
history blame
3.14 kB
import streamlit as st
from datasets import load_dataset
dataset = load_dataset("roneneldan/TinyStories")
st.write(dataset['train'][0])
#-------------------------------------------------------------
#-------------------------------------------------------------
from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer('all-MiniLM-L6-v2')
# Two lists of sentences
sentences1 = ['The cat sits outside',
'A man is playing guitar',
'The new movie is awesome']
sentences2 = ['The dog plays in the garden',
'A woman watches TV',
'The new movie is so great']
#Compute embedding for both lists
embeddings1 = model.encode(sentences1, convert_to_tensor=True)
embeddings2 = model.encode(sentences2, convert_to_tensor=True)
#Compute cosine-similarities
cosine_scores = util.cos_sim(embeddings1, embeddings2)
#Output the pairs with their score
for i in range(len(sentences1)):
st.write("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i], sentences2[i], cosine_scores[i][i]))
#-------------------------------------------------------------
#-------------------------------------------------------------
# ego_graph.py
# An example of how to plot a node's ego network
# (egonet). This indirectly showcases slightly more involved
# interoperability between streamlit-agraph and networkx.
# An egonet can be # created from (almost) any network (graph),
# and exemplifies the # concept of a subnetwork (subgraph):
# A node's egonet is the (sub)network comprised of the focal node
# and all the nodes to whom it is adjacent. The edges included
# in the egonet are those nodes are both included in the aforementioned
# nodes.
# Use the following command to launch the app
# streamlit run <path-to-script>.py
# standard library dependencies
from operator import itemgetter
# external dependencies
import networkx as nx
from streamlit_agraph import agraph, Node, Edge, Config
# First create a graph using the Barabasi-Albert model
n = 2000
m = 2
G = nx.generators.barabasi_albert_graph(n, m, seed=2023)
# Then find the node with the largest degree;
# This node's egonet will be the focus of this example.
node_and_degree = G.degree()
most_connected_node = sorted(G.degree, key=lambda x: x[1], reverse=True)[0]
degree = G.degree(most_connected_node)
# Create egonet for the focal node
hub_ego = nx.ego_graph(G, most_connected_node[0])
# Now create the equivalent Node and Edge lists
nodes = [Node(id=i, label=str(i), size=20) for i in hub_ego.nodes]
edges = [Edge(source=i, target=j, type="CURVE_SMOOTH") for (i,j) in G.edges
if i in hub_ego.nodes and j in hub_ego.nodes]
config = Config(width=500,
height=500,
directed=True,
nodeHighlightBehavior=False,
highlightColor="#F7A7A6", # or "blue"
collapsible=False,
node={'labelProperty':'label'},
# **kwargs e.g. node_size=1000 or node_color="blue"
)
return_value = agraph(nodes=nodes,
edges=edges,
config=config)