Spaces:
Running
Running
Update apps/sdg.py
Browse files- apps/sdg.py +57 -56
apps/sdg.py
CHANGED
@@ -5,59 +5,60 @@ import umap.umap_ as umap
|
|
5 |
import pandas as pd
|
6 |
import os
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
5 |
import pandas as pd
|
6 |
import os
|
7 |
|
8 |
+
def app():
|
9 |
+
st.title("SDG Embedding Visualisation")
|
10 |
+
|
11 |
+
with st.spinner("π load language model (sentence transformer)"):
|
12 |
+
model_name = 'sentence-transformers/all-MiniLM-L6-v2'
|
13 |
+
model = SentenceTransformer(model_name)
|
14 |
+
|
15 |
+
with st.spinner("π load sdg data"):
|
16 |
+
df_osdg = pd.read_csv('https://zenodo.org/record/5550238/files/osdg-community-dataset-v21-09-30.csv',sep='\t')
|
17 |
+
df_osdg = df_osdg[df_osdg['agreement']>.95]
|
18 |
+
df_osdg = df_osdg[df_osdg['labels_positive']>3]
|
19 |
+
#df_osdg = df_osdg[:1000]
|
20 |
+
|
21 |
+
_lab_dict = {0: 'no_cat',
|
22 |
+
1:'SDG 1 - No poverty',
|
23 |
+
2:'SDG 2 - Zero hunger',
|
24 |
+
3:'SDG 3 - Good health and well-being',
|
25 |
+
4:'SDG 4 - Quality education',
|
26 |
+
5:'SDG 5 - Gender equality',
|
27 |
+
6:'SDG 6 - Clean water and sanitation',
|
28 |
+
7:'SDG 7 - Affordable and clean energy',
|
29 |
+
8:'SDG 8 - Decent work and economic growth',
|
30 |
+
9:'SDG 9 - Industry, Innovation and Infrastructure',
|
31 |
+
10:'SDG 10 - Reduced inequality',
|
32 |
+
11:'SDG 11 - Sustainable cities and communities',
|
33 |
+
12:'SDG 12 - Responsible consumption and production',
|
34 |
+
13:'SDG 13 - Climate action',
|
35 |
+
14:'SDG 14 - Life below water',
|
36 |
+
15:'SDG 15 - Life on land',
|
37 |
+
16:'SDG 16 - Peace, justice and strong institutions',
|
38 |
+
17:'SDG 17 - Partnership for the goals',}
|
39 |
+
|
40 |
+
labels = [_lab_dict[lab] for lab in df_osdg['sdg'] ]
|
41 |
+
#keys = list(df_osdg['keys'])
|
42 |
+
docs = list(df_osdg['text'])
|
43 |
+
docs_embeddings = model.encode(docs)
|
44 |
+
|
45 |
+
with st.spinner("π prepare visualisation"):
|
46 |
+
n_neighbors = 15
|
47 |
+
n_components = 3
|
48 |
+
random_state =42
|
49 |
+
umap_model = (umap.UMAP(n_neighbors=n_neighbors,
|
50 |
+
n_components=n_components,
|
51 |
+
metric='cosine',
|
52 |
+
random_state=random_state)
|
53 |
+
.fit(docs_embeddings))
|
54 |
+
|
55 |
+
docs_umap = umap_model.transform(docs_embeddings)
|
56 |
+
|
57 |
+
with st.spinner("π create visualisation"):
|
58 |
+
fig = px.scatter_3d(
|
59 |
+
docs_umap, x=0, y=1, z=2,
|
60 |
+
color=labels,
|
61 |
+
opacity = .5)#, hover_data=[keys])
|
62 |
+
fig.update_scenes(xaxis_visible=False, yaxis_visible=False,zaxis_visible=False )
|
63 |
+
fig.update_traces(marker_size=4)
|
64 |
+
st.plotly_chart(fig)
|