File size: 9,577 Bytes
d8be960
7eb7682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7d6594
7eb7682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c7b8b4
7eb7682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf90993
d8be960
7eb7682
 
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
import streamlit as st
from lime.lime_text import LimeTextExplainer
from nltk.corpus import stopwords

from deployment_utils import DataPreparator, Predictor, generate_random_sample, generate_highlighted_words, extract_case_information


data_preparator = DataPreparator()
predictor = Predictor()
eng_stop_words = stopwords.words("english")

st.set_page_config(
    page_title="JudgerAI",
    page_icon="🧊",
    layout="wide")

with open("./src/style.css") as f:
    st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)

# application header
left_col, right_col = st.columns(2)

with left_col:
    st.header("Summarize your Case")
    with st.expander(label="Case Summarizer", expanded=True):
        option = st.selectbox(
            'Choose a Method for Entering your Case Facts',
            ('Upload a File', 'Write it Myself'))

        if option == "Upload a File":
            uploaded_file = st.file_uploader(
                label='Upload your Case File (.txt)', type=['txt'])
            if uploaded_file is not None:
                content = uploaded_file.getvalue().decode("utf-8")
                petitioner, respondent, case_facts = extract_case_information(
                    content)

                col1, col2 = st.columns(2)

                with col1:
                    st.write(
                        '<p class="bold-text"> Petitioner </p>', unsafe_allow_html=True)
                    st.info(petitioner)
                with col2:
                    st.write(
                        '<p class="bold-text"> Respondent </p>', unsafe_allow_html=True)
                    st.info(respondent)
                st.write(
                    '<p class="bold-text"> Case Facts </p>', unsafe_allow_html=True)
                st.info(case_facts)

        else:
            case_facts = st.text_area(
                label="Enter your Case Facts youself", height=250)

        submitted = st.button(label="Summarize")

        if submitted:
            with st.spinner("Your Case is being Summarized..."):
                summarized_case_facts = predictor.summarize_facts(case_facts)
                st.write(
                    '<p class="bold-text"> Your Summarized Case Facts </p>', unsafe_allow_html=True)
                st.success(summarized_case_facts)

                summarized_case_facts_file = "petitioner: " + petitioner + "\n" + \
                    "respondent: " + respondent + "\n" + "facts: " + summarized_case_facts

                btn = st.download_button(
                    label="Download",
                    data=summarized_case_facts_file,
                    file_name="summarized_case_facts.txt",
                    mime="file/txt"
                )

with right_col:
    st.header("Predict the Outcome")

    # get_random_case_button = st.button(label="Get Random Sample")

    # input form
    with st.expander(label="Case Outcome Predictor", expanded=True):
        option = st.selectbox(
            'Choose a Method for Entering your Case Information',
            ('Upload a File', 'Write it Myself'))

        if option == 'Upload a File':
            uploaded_file = st.file_uploader(
                label='Upload your Case File (.txt)', type=['txt'], key="prediction_case_uploader")
            if uploaded_file is not None:
                content = uploaded_file.getvalue().decode("utf-8")
                petitioner, respondent, case_facts = extract_case_information(
                    content)

                st.session_state["petitioner"] = petitioner.strip()
                st.session_state["respondent"] = respondent.strip()
                st.session_state["facts"] = case_facts.strip()

        option = st.selectbox(
            "Select Model",
            ("1D Convolutional","BERT","Doc2Vec")
        )

        col1, col2 = st.columns(2)

        with col1:
            petitioner = st.text_input(
                label="Petitioner", key="petitioner")

        with col2:
            respondent = st.text_input(
                label="Respondent", key="respondent")

        global facts
        facts = st.text_area(label="Case Facts",
                             height=300, key="facts")

        # remove stopwords to not highlight it
        facts = " ".join([word for word in facts.split()
                          if word not in eng_stop_words])

        # create `LimeTextExplainer` for models interpretation
        class_names = [petitioner, respondent]
        explainer = LimeTextExplainer(class_names=class_names)

        submitted = st.button(label="Predict")

        if submitted:
            if petitioner and respondent and facts:
                with st.spinner("Analyzing Case Facts ..."):
                    # get predcitions
                    if option == "Doc2Vec":
                        predictions = predictor.predict_doc2vec(facts)
                        output = explainer.explain_instance(
                            facts, predictor.predict_doc2vec)
                        important_words = output.as_list()

                    elif option == "TF-IDF":
                        anonymized_facts = data_preparator._anonymize_facts(
                            petitioner, respondent, facts)
                        predictions = predictor.predict_tf_idf(
                            anonymized_facts)
                        output = explainer.explain_instance(
                            anonymized_facts, predictor.predict_tf_idf)
                        important_words = output.as_list()

                    elif option == "1D Convolutional":
                        predictions = predictor.predict_cnn(facts)
                        output = explainer.explain_instance(
                            facts, predictor.predict_cnn)
                        important_words = output.as_list()

                    elif option == "GloVe":
                        predictions = predictor.predict_glove(facts)
                        output = explainer.explain_instance(
                            facts, predictor.predict_glove)
                        important_words = output.as_list()

                    elif option == "LSTM":
                        predictions = predictor.predict_lstm(facts)
                        output = explainer.explain_instance(
                            facts, predictor.predict_lstm)
                        important_words = output.as_list()

                    elif option == "BERT":
                        predictions = predictor.predict_bert(facts)

                    elif option == "FastText":
                        predictions = predictor.predict_fasttext(facts)

                    elif option == "Ensemble (Doc2Vec + TF-IDF)":
                        doc2vec_predictions = predictor.predict_doc2vec(facts)
                        tf_idf_predictions = predictor.predict_tf_idf(facts)

                        predictions = (doc2vec_predictions +
                                       tf_idf_predictions) / 2

                        doc2vec_output = explainer.explain_instance(
                            facts, predictor.predict_doc2vec)
                        doc2vec_important_words = doc2vec_output.as_list()

                        tf_idf_output = explainer.explain_instance(
                            facts, predictor.predict_tf_idf)
                        tf_idf_important_words = tf_idf_output.as_list()

                        important_words = doc2vec_important_words + tf_idf_important_words

                # displaying predictions
                col1, col2 = st.columns(2)

                with col1:
                    st.write("Percentage of petitioner winning:")
                    st.warning(f"{predictions[0, 0] * 100:.3f}%")

                with col2:
                    st.write("Percentage of respondent winning:")
                    st.info(f"{predictions[0, 1] * 100:.3f}%")

                st.write("Winning party:")
                if predictions[0, 0] > predictions[0, 1]:
                    st.success(petitioner)
                else:
                    st.success(respondent)

                # displaying highlighted words
                st.write(
                    '<p class="bold-text"> Top Words for Model\'s Decision: </p>', unsafe_allow_html=True)

                if option not in ["BERT", "FastText"]:
                    petitioner_words = [word for word,
                                        score in important_words if score < 0]
                    respondent_words = [word for word,
                                        score in important_words if score > 0]

                    for name in petitioner.split(" "):
                        if name in petitioner_words:
                            petitioner_words.remove(name)
                        elif name in respondent_words:
                            respondent_words.remove(name)

                    for name in respondent.split(" "):
                        if name in petitioner_words:
                            petitioner_words.remove(name)
                        elif name in respondent_words:
                            respondent_words.remove(name)

                    rendered_text = generate_highlighted_words(
                        facts, petitioner_words, respondent_words)

                    st.write(rendered_text, unsafe_allow_html=True)

                else:
                    st.warning(
                        "Sadly, this feature is not supported in BERT")

            else:
                st.error("Please, fill in all fields!")