File size: 4,357 Bytes
c6164c4
ecfca68
 
a881899
49dc376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1de592f
a881899
49dc376
 
1de592f
 
 
 
 
7595329
1de592f
 
37ad6f6
49dc376
798acc3
49dc376
 
798acc3
37ad6f6
 
798acc3
 
 
37ad6f6
 
 
 
 
 
49dc376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import warnings
import streamlit as st
warnings.filterwarnings("ignore", category=UserWarning, module="streamlit")
import pandas as pd
import json
import os
from huggingface_hub import HfApi, login
from streamlit_cookies_manager import EncryptedCookieManager

st.set_page_config(
    page_title="Holistic AI - ML Verticals papers",
    page_icon="👋",
    layout='wide'
)

def program():
    st.title("Papers")
    
    dataset_name = "holistic-ai/mitigation_ml_bias_strategies"
    token = os.getenv("HF_TOKEN")
    
    api = HfApi()
    login(token)
    
    repo_path = api.snapshot_download(repo_id=dataset_name, repo_type="dataset")
    dirnames = [dirname for dirname in os.listdir(repo_path) if not dirname.startswith(".")]
    dirnames = sorted(dirnames, key=lambda x:len(x))
    
    st.sidebar.title("Namespaces")
    selected_namespace = st.sidebar.selectbox("Select Namespace", dirnames)

    selected_paper_type = st.sidebar.selectbox("Select Paper Type", ['Metrics',"Mitigators"])
    
    if selected_namespace:
        
        if selected_paper_type=='Metrics':
            with open(f'{repo_path}/{selected_namespace}/grouped_metrics.json') as file:
                data = json.load(file)
        elif selected_paper_type=='Mitigators':
            with open(f'{repo_path}/{selected_namespace}/grouped_mitigators.json') as file:
                data = json.load(file)
    
        task_names = list(data.keys())
    
        st.sidebar.title("Tasks")
        selected_task = st.sidebar.selectbox("Select a Task", task_names)
    
        if selected_task:
            st.header(selected_task)
            results = data[selected_task]
            rec = {str(r['id']): r for r in results['recommendations']}
            
            for group in results['groups']:
                ids = [i.strip() for i in group['ids'].split(",")]

                selected_rec = [rec[i]  for i in ids]

                selected_rec = pd.DataFrame(selected_rec)
                selected_rec['date'] = pd.to_datetime(selected_rec.apply(lambda x:x['metadata']['date'], axis=1))
                selected_rec = selected_rec.sort_values(by='date', ascending=False).to_dict('records')
                
                rec2html = ''.join([f"""<tr><td style="border: 1px solid #ddd; padding: 8px;">{i+1}</td><td style="border: 1px solid #ddd; padding: 8px;"><a href="{rec['metadata']['id']}" target="_blank">{rec['title']}</a></td><td style="border: 1px solid #ddd; padding: 8px;">{rec['metadata']['date']}</td></tr>""" for i, rec in enumerate(selected_rec)])
                title = group['title'].split(':', 1)[1].strip()
                st.markdown(f"""
                <div style="border: 1px solid #ccc; padding: 10px; margin: 10px 0; border-radius: 5px; width: 100%;">
                    <p><b>{title}</b></p>
                    <p>{group['recommendation']}</p>
                    <table style="width: 100%; border-collapse: collapse;">
                        <thead>
                            <tr>
                                <th style="border: 1px solid #ddd; padding: 8px;">Index</th>
                                <th style="border: 1px solid #ddd; padding: 8px;">Paper</th>
                                <th style="border: 1px solid #ddd; padding: 8px;">Year</th>
                            </tr>
                        </thead>
                        <tbody>
                            {rec2html}
                        </tbody>
                    </table>
                </div>
                """, unsafe_allow_html=True)

SECRET_KEY = os.getenv('SECRET_KEY')

cookies = EncryptedCookieManager(
    prefix="login",
    password=os.getenv('COOKIES_PASSWORD')
)

if not cookies.ready():
    st.stop()

def main():
    # Título de la aplicación
    st.title("Holistic AI - ML Papers")

    if not cookies.get("authenticated"):
        # Entrada de la clave secreta
        user_key = st.text_input("Password:", type="password")

        if st.button("Login"):
            # Verificar si la clave ingresada coincide con la clave secreta
            if user_key == SECRET_KEY:
                cookies.__setitem__("authenticated", "True")
                st.experimental_rerun()
            else:
                st.error("Access not granted. Incorrect Password.")
    else:
        program()
if __name__ == "__main__":
    main()