Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from tqdm.auto import tqdm
|
| 5 |
+
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 8 |
+
from huggingface_hub.repocard import metadata_load
|
| 9 |
+
import streamlit.components.v1 as components
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def make_clickable_model(model_name):
|
| 13 |
+
link = "https://huggingface.co/" + model_name
|
| 14 |
+
return f'<a target="_blank" href="{link}">{model_name}</a>'
|
| 15 |
+
|
| 16 |
+
# Make user clickable link
|
| 17 |
+
def make_clickable_user(user_id):
|
| 18 |
+
link = "https://huggingface.co/" + user_id
|
| 19 |
+
return f'<a target="_blank" href="{link}">{user_id}</a>'
|
| 20 |
+
|
| 21 |
+
def get_model_ids():
|
| 22 |
+
api = HfApi()
|
| 23 |
+
models = api.list_models(filter="deprem-clf-v1")
|
| 24 |
+
model_ids = [x.modelId for x in models]
|
| 25 |
+
return model_ids
|
| 26 |
+
|
| 27 |
+
def get_metadata(model_id):
|
| 28 |
+
try:
|
| 29 |
+
readme_path = hf_hub_download(model_id, filename="README.md")
|
| 30 |
+
return metadata_load(readme_path)
|
| 31 |
+
except requests.exceptions.HTTPError:
|
| 32 |
+
# 404 README.md not found
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
def parse_metrics_accuracy(meta):
|
| 36 |
+
if "model-index" not in meta:
|
| 37 |
+
return None
|
| 38 |
+
result = meta["model-index"][0]["results"]
|
| 39 |
+
metrics = result[0]["metrics"]
|
| 40 |
+
accuracy = metrics[2]["value"]
|
| 41 |
+
print("Accuracy", accuracy)
|
| 42 |
+
return accuracy
|
| 43 |
+
|
| 44 |
+
def parse_metrics_recall(meta):
|
| 45 |
+
if "model-index" not in meta:
|
| 46 |
+
return None
|
| 47 |
+
result = meta["model-index"][0]["results"]
|
| 48 |
+
metrics = result[0]["metrics"]
|
| 49 |
+
recall = metrics[0]["value"]
|
| 50 |
+
print("Recall", recall)
|
| 51 |
+
return recall
|
| 52 |
+
|
| 53 |
+
def parse_metrics_f1(meta):
|
| 54 |
+
if "model-index" not in meta:
|
| 55 |
+
return None
|
| 56 |
+
result = meta["model-index"][0]["results"]
|
| 57 |
+
metrics = result[0]["metrics"]
|
| 58 |
+
f1 = metrics[1]["value"]
|
| 59 |
+
print("F1-score", f1)
|
| 60 |
+
return f1
|
| 61 |
+
|
| 62 |
+
#@st.cache(ttl=600)
|
| 63 |
+
def get_data():
|
| 64 |
+
data = []
|
| 65 |
+
model_ids = get_model_ids()
|
| 66 |
+
for model_id in tqdm(model_ids):
|
| 67 |
+
meta = get_metadata(model_id)
|
| 68 |
+
if meta is None:
|
| 69 |
+
continue
|
| 70 |
+
user_id = model_id.split('/')[0]
|
| 71 |
+
row = {}
|
| 72 |
+
row["User"] = user_id
|
| 73 |
+
row["Model"] = model_id
|
| 74 |
+
recall = parse_metrics_recall(meta)
|
| 75 |
+
row["Recall"] = recall
|
| 76 |
+
f1 = parse_metrics_recall(meta)
|
| 77 |
+
row["F1-Score"] = f1
|
| 78 |
+
data.append(row)
|
| 79 |
+
return pd.DataFrame.from_records(data)
|
| 80 |
+
|
| 81 |
+
dataframe = get_data()
|
| 82 |
+
dataframe = dataframe.fillna("")
|
| 83 |
+
|
| 84 |
+
st.markdown("# Deprem Niyet Analizi için Lider Tablosu")
|
| 85 |
+
|
| 86 |
+
st.markdown("Bu lider tablosu modellerimizi versiyonladıktan sonra hangi modeli üretime çıkarmamız gerektiğinin takibini yapmak için kullanılır.")
|
| 87 |
+
st.markdown(
|
| 88 |
+
"Model card'da metadata'da tags kısmına deprem-clf-v1 yazarsanız modeliniz buraya otomatik eklenir."
|
| 89 |
+
)
|
| 90 |
+
st.markdown(
|
| 91 |
+
"Burada recall, f1-score ve accuracy'nin macro average'ına bakıyoruz. Model card'ın metadata kısmında bu üç veriyi log'lamanız yeterli. Burada classification report çıkarırken confidence threshold 0.2."
|
| 92 |
+
)
|
| 93 |
+
st.markdown("Örnek metadata için [bu model card'ın metadata kısmını](https://huggingface.co/deprem-ml/deprem-roberta-intent/blob/main/README.md) kopyalayıp yapıştırarak kendi metriklerinize göre ayarlayabilirsiniz.")
|
| 94 |
+
st.markdown(
|
| 95 |
+
"Modelin üstüne tıklayıp model card'a gidebilirsiniz."
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# turn the model ids into clickable links
|
| 101 |
+
dataframe["User"] = dataframe["User"].apply(make_clickable_user)
|
| 102 |
+
dataframe["Model"] = dataframe["Model"].apply(make_clickable_model)
|
| 103 |
+
dataframe = dataframe.sort_values(by=['F1-Score'], ascending=False)
|
| 104 |
+
table_html = dataframe.to_html(escape=False, index=False)
|
| 105 |
+
table_html = table_html.replace("<th>", '<th align="left">') # left-align the headers
|
| 106 |
+
st.write(table_html, unsafe_allow_html=True)
|