Spaces:
Sleeping
Sleeping
File size: 7,739 Bytes
aa68450 ea3bd45 eb58c95 aa68450 4547220 ac1ff33 ea3bd45 eb58c95 ac1ff33 aa68450 ea3bd45 aa68450 ea3bd45 aa68450 ea3bd45 aa68450 ea3bd45 aa68450 f80e04a e8fb6a3 aa68450 f80e04a 4547220 aa68450 e8fb6a3 aa68450 e8fb6a3 aa68450 e8fb6a3 aa68450 ea3bd45 aa68450 ea3bd45 e8fb6a3 ea3bd45 aa68450 ea3bd45 e8fb6a3 4547220 32e3d31 4547220 120dad2 e8fb6a3 ea3bd45 4547220 8a6a919 4547220 e8fb6a3 4547220 ea3bd45 4547220 |
1 2 3 4 5 6 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
from collections import Counter
import pandas as pd
import streamlit as st
import json
from plotly import express as px
from safetensors import safe_open
from semantic_search import predict
from sentence_transformers import SentenceTransformer
import os
import plotly.graph_objects as go
HF_TOKEN = os.environ.get("HF_TOKEN")
def get_tree_map_data(
data: dict,
countings_parents: dict,
countings_labels: dict,
root: str = " ",
) -> tuple:
names: list = [""]
parents: list = [root]
values: list = ["0"]
for group, labels in data.items():
parents.append(root)
if group in countings_parents:
values.append(str(countings_parents[group]))
group_name_with_count = (
group
+ "<br>"
+ "Anzahl Datensätze:"
+ " "
+ str(countings_parents[group])
)
names.append(group_name_with_count)
else:
values.append("0")
group_name_with_count = group + "<br>" + "Anzahl Datensätze:" + " " + "0"
names.append(group_name_with_count)
for label in labels:
if "-" in label:
label = label.split("-")
label = label[0] + "<br> -" + label[1]
if label in countings_labels:
label_name_with_count = (
label
+ "<br>"
+ "<br>"
+ "Anzahl Datensätze:"
+ "<br>"
+ ""
+ str(countings_labels[label])
)
names.append(label_name_with_count)
parents.append(group_name_with_count)
values.append(str(countings_labels[label]))
if label not in countings_labels:
if "<br>" in label:
if (
label.split("<br>")[0].strip() + label.split("<br>")[-1]
in countings_labels
):
label_name_with_count = (
label
+ "<br>"
+ "<br>"
+ "Anzahl Datensätze:"
+ "<br>"
+ ""
+ str(
countings_labels[
label.split("<br>")[0].strip()
+ label.split("<br>")[-1]
]
)
)
else:
print(label)
label_name_with_count = (
label
+ "<br>"
+ "<br>"
+ "Anzahl Datensätze:"
+ "<br>"
+ ""
+ "0"
)
names.append(label_name_with_count)
parents.append(group_name_with_count)
values.append("0")
return parents, names, values
def load_json(path: str) -> dict:
with open(path, "r") as fp:
return json.load(fp)
# Load Data
data = load_json("data.json")
taxonomy = load_json("taxonomy_processed_v3.json")
taxonomy_labels = [el["group"] + " - " + el["label"] for el in taxonomy]
theme_counts = dict(Counter([el["THEMA"] for el in data]))
labels_counts = dict(Counter([el["BEZEICHNUNG"] for el in data]))
names = [""]
parents = ["Musterdatenkatalog"]
taxonomy_group_label_mapper: dict = {el["group"]: [] for el in taxonomy}
for el in taxonomy:
if el["group"] != "Sonstiges":
taxonomy_group_label_mapper[el["group"]].append(el["label"])
else:
taxonomy_group_label_mapper[el["group"]].append("Sonstiges ")
del taxonomy_group_label_mapper["Sonstiges"]
parents, names, values = get_tree_map_data(
data=taxonomy_group_label_mapper,
countings_parents=theme_counts,
countings_labels=labels_counts,
root="Musterdatenkatalog",
)
df = pd.DataFrame(data={"thema": parents, "bezeichnung": names, "value": values})
df["value"] = df["value"].astype(str)
df["bezeichnung"] = df["bezeichnung"]
fig = go.Figure(
go.Treemap(
labels=df["bezeichnung"],
parents=df["thema"],
textinfo="label",
)
)
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
fig.update_layout(height=1000, width=1000, template="plotly")
tensors = {}
with safe_open("corpus_embeddings.pt", framework="pt", device="cpu") as f:
for k in f.keys():
tensors[k] = f.get_tensor(k)
model = SentenceTransformer(
model_name_or_path="and-effect/musterdatenkatalog_clf",
device="cpu",
use_auth_token=HF_TOKEN,
)
st.set_page_config(layout="wide")
st.title("Musterdatenkatalog")
st.markdown(
"""
<style>
.font {
font-size:20px !important;
}
</style>
""",
unsafe_allow_html=True,
)
st.markdown(
'<p class="font">This demo showcases the algorithm of Musterdatenkatalog (MDK) of the Bertelsmann Stiftung. The MDK is a taxonomy of Open Data in municipalities in Germany. It is intended to help municipalities in Germany, as well as data analysts and journalists, to get an overview of the topics and the extent to which cities have already published data sets.</p>',
unsafe_allow_html=True,
)
st.markdown(
'<p class="font"> For more details checkout the <a href=https://www.bertelsmann-stiftung.de/de/unsere-projekte/smart-country/musterdatenkatalog> Musterdatenkatalog.</p>',
unsafe_allow_html=True,
)
col1, col2, col3 = st.columns(3)
col1.metric("Datensätze", len(data))
col2.metric("Themen", len(theme_counts))
col3.metric("Bezeichnungen", len(labels_counts))
st.title("Taxonomy")
st.plotly_chart(fig)
st.title("Predict a Dataset")
st.markdown(
"""
<style>
/* Style columns */
[data-testid="column"] {
border-radius: 15px;
background-color: white;
box-shadow: 0 0 10px #eee;
border: 1px solid #ddd;
padding: 1rem;;
}
/* Style containers */
[data-testid="stVerticalBlock"] > [style*="flex-direction: column;"] > [data-testid="stVerticalBlock"] {
border-radius: 15px;
background-color: white;
box-shadow: 0 0 10px #eee;
border: 1px solid #ddd;
padding: 1rem;;
}
</style>
""",
unsafe_allow_html=True,
)
col1, col2 = st.columns([1.2, 1])
with col2:
st.subheader("Example Input Dataset Names")
examples = [
"Spielplätze",
"Berliner Weihnachtsmärkte 2022",
"Hochschulwechslerquoten zum Masterstudium nach Bundesländern",
"Umringe der Bebauungspläne von Etgert",
]
for example in examples:
if st.button(example):
if "key" not in st.session_state:
st.session_state["query"] = example
with col1:
if "query" not in st.session_state:
query = st.text_input(
"Enter dataset name",
)
if "query" in st.session_state and st.session_state.query in examples:
query = st.text_input("Enter dataset name", value=st.session_state.query)
if "query" in st.session_state and st.session_state.query not in examples:
del st.session_state["query"]
query = st.text_input("Enter dataset name")
top_k = st.select_slider("Top Results", options=[1, 2, 3, 4, 5], value=1)
predictions = predict(
query=query,
corpus_embeddings=tensors["corpus_embeddings"],
corpus_labels=taxonomy_labels,
top_k=top_k,
model=model,
)
if st.button("Predict"):
for prediction in predictions:
st.write(prediction)
|