File size: 6,114 Bytes
e8a589d
a635b88
e8a589d
8cb26d4
e8a589d
a635b88
1c8fe13
 
 
 
 
 
 
 
 
 
 
a635b88
1c8fe13
8cb26d4
 
1c8fe13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8a589d
 
 
 
 
a635b88
e3068b9
1c8fe13
e3068b9
 
 
 
 
 
 
 
 
 
 
1c8fe13
e3068b9
1c8fe13
e8a589d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c8fe13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a635b88
1c8fe13
 
 
 
 
 
e8a589d
 
 
 
78f0286
e8a589d
 
 
 
 
 
 
 
 
 
2e3d986
 
e8a589d
2cb848b
ffb48a2
1c8fe13
e8a589d
 
 
 
 
 
1c8fe13
 
 
 
 
 
 
e8a589d
1c8fe13
e8a589d
 
 
 
 
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
import os
import yaml
import json
from pathlib import Path
from compliance_analysis import check_overall_compliance

# if __name__ == "__main__":
#     demo.launch()

import streamlit as st
import yaml
from pathlib import Path
import pandas as pd


def load_data(files):
    cards = []
    for file in files:
        content = file.read().decode("utf-8")
        if Path(file.name).name == "project_cc.yaml":
            project_cc_yaml = yaml.safe_load(content)
            data = project_cc_yaml
            card_type = "project"
            cards.append((card_type, data))
        if Path(file.name).name == "data_cc.yaml":
            data_cc_yaml = yaml.safe_load(content)
            data = data_cc_yaml
            card_type = "data"
            cards.append((card_type, data))
        if Path(file.name).name == "model_cc.yaml":
            model_cc_yaml = yaml.safe_load(content)
            data = model_cc_yaml
            card_type = "model"
            cards.append((card_type, data))
    return cards

# def process_files(files):
#     results = []
#     for file in files:
#         content = file.read().decode("utf-8")
#         if Path(file.name).name == "project_cc.yaml":
#             project_cc_yaml = yaml.safe_load(content)
#             if project_cc_yaml:
#                 msg = run_compliance_analysis_on_project(project_cc_yaml)
#                 results.append(msg)            
#     return results

# def process_files(data):
#     results = []
#     msg = run_compliance_analysis_on_project(yaml.safe_load(data))
#     results.append(msg)            
#     return results

# def extract_properties(data):
        
#     flattened_data = []

#     for category, items in data.items():
#         for item, attributes in items.items():
#             flattened_data.append({
#                 "Category": category,
#                 "Item": item,
#                 "Verbose": attributes["verbose"],
#                 "Value": attributes["value"]
#             })
#     df = pd.DataFrame(flattened_data)
    
#     return df

def gather_cards(files):
    cards = {}
    cards['project_file'] = ''
    cards['data_files'] = []
    cards['model_files'] = []
    for file in files:
        file_path = os.path.join('/tmp', file.name)        
        with open(file_path, 'wb') as f:
            f.write(file.getbuffer())
        with open(file_path, 'r') as file_path:
            content = yaml.safe_load(file_path.read())            
            if content['card_type'] == 'project':
                cards['project_file'] = file_path.name
            if content['card_type'] == "data":
                cards['data_files'].append(file_path.name)
            if content['card_type'] == "model":
                cards['model_files'].append(file_path.name)
    return cards

def compliance_analysis(cards):
    results = []
    dispositive_variables = check_overall_compliance(cards)
    results.append(dispositive_variables)#['msg'])
    return results

# Streamlit app
# st.set_page_config(page_title="AI", layout="wide")
# st.markdown(
#     """
#     <style>
#     [data-testid="stSidebar"][aria-expanded="true"] > div:first-child{
#         width: 600px;
#     }
#     [data-testid="stSidebar"][aria-expanded="false"] > div:first-child{
#         width: 600px;
#         margin-left: -400px;
#     }
     
#     """,
#     unsafe_allow_html=True,
# )

st.title("AI")

uploaded_files = st.file_uploader("Upload YAML Files", type="yaml", accept_multiple_files=True)
# project_files = st.file_uploader("Upload Project Files", type="yaml", accept_multiple_files=True)

if uploaded_files:

    cards = load_data(uploaded_files)
    for card in cards:
        
        data = card[1]
    
        if data != None:    

            st.title("Compliance Checkboxes")
            st.title(f"{card[0]}")

            for section, items in data.items():
                if section != 'card_type':
                    st.header(section.replace('_', ' ').title())  # section header
                    for key, details in items.items():
                        if 'verbose' in details and 'value' in details:
                            st.subheader(key.replace('_', ' ').title())  # section header
                            # details['value'] = st.checkbox(details['verbose'], value=details['value'])
                            if isinstance(details['value'], str):
                                details['value'] = st.text_input(details['verbose'], value=details['value'])
                            elif isinstance(details['value'], bool):
                                details['value'] = st.checkbox(details['verbose'], value=details['value'])                        
                        if 'verbose' not in details and 'value' not in details:
                            st.subheader(key.replace('_', ' ').title())  # section header
                            for key, details in details.items():
                                st.subheader(key.replace('_', ' ').title())  # section header
                                details['value'] = st.checkbox(details['verbose'], value=details['value'])
                            # st.divider()
                    # st.divider()
            # st.write("Updated Data:", data)
           
            yaml_data = yaml.dump(data, sort_keys=False)

            # st.download_button(
            #     label=f"Download Updated Data as YAML{card[0]}",
            #     data=yaml_data,
            #     file_name="updated_data.yaml",
            #     mime="text/yaml"
            # )

            # json_data = json.dumps(data, indent=2)
            # st.download_button(
            #     label="Download Updated Data as JSON",
            #     data=json_data,
            #     file_name="updated_data.json",
            #     mime="application/json"
            # )   

    cards = gather_cards(uploaded_files)    
    if st.button(f"Run Analysis"):
        results = compliance_analysis(cards)    
        # st.text_area("Analysis Results", value=json.dumps(results, indent=4), height=600)
        st.write("Analysis Results", results)