Update utils/adapmit_classifier.py
Browse files- utils/adapmit_classifier.py +100 -101
utils/adapmit_classifier.py
CHANGED
@@ -1,101 +1,100 @@
|
|
1 |
-
from haystack.schema import Document
|
2 |
-
from typing import List, Tuple
|
3 |
-
from typing_extensions import Literal
|
4 |
-
import logging
|
5 |
-
import pandas as pd
|
6 |
-
from pandas import DataFrame, Series
|
7 |
-
from utils.config import getconfig
|
8 |
-
from utils.preprocessing import processingpipeline
|
9 |
-
import streamlit as st
|
10 |
-
from
|
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 |
-
haystack_doc['
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
truth_df =
|
90 |
-
truth_df = truth_df.
|
91 |
-
truth_df = truth_df.astype(
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
return df
|
|
|
1 |
+
from haystack.schema import Document
|
2 |
+
from typing import List, Tuple
|
3 |
+
from typing_extensions import Literal
|
4 |
+
import logging
|
5 |
+
import pandas as pd
|
6 |
+
from pandas import DataFrame, Series
|
7 |
+
from utils.config import getconfig
|
8 |
+
from utils.preprocessing import processingpipeline
|
9 |
+
import streamlit as st
|
10 |
+
from transformers import pipeline
|
11 |
+
|
12 |
+
@st.cache_resource
|
13 |
+
def load_adapmitClassifier(config_file:str = None, classifier_name:str = None):
|
14 |
+
"""
|
15 |
+
loads the document classifier using haystack, where the name/path of model
|
16 |
+
in HF-hub as string is used to fetch the model object.Either configfile or
|
17 |
+
model should be passed.
|
18 |
+
1. https://docs.haystack.deepset.ai/reference/document-classifier-api
|
19 |
+
2. https://docs.haystack.deepset.ai/docs/document_classifier
|
20 |
+
Params
|
21 |
+
--------
|
22 |
+
config_file: config file path from which to read the model name
|
23 |
+
classifier_name: if modelname is passed, it takes a priority if not \
|
24 |
+
found then will look for configfile, else raise error.
|
25 |
+
Return: document classifier model
|
26 |
+
"""
|
27 |
+
if not classifier_name:
|
28 |
+
if not config_file:
|
29 |
+
logging.warning("Pass either model name or config file")
|
30 |
+
return
|
31 |
+
else:
|
32 |
+
config = getconfig(config_file)
|
33 |
+
classifier_name = config.get('adapmit','MODEL')
|
34 |
+
|
35 |
+
logging.info("Loading Adaptation Mitigation classifier")
|
36 |
+
doc_classifier = pipeline("text-classification",
|
37 |
+
model=classifier_name,
|
38 |
+
return_all_scores=True,
|
39 |
+
function_to_apply= "sigmoid")
|
40 |
+
|
41 |
+
|
42 |
+
return doc_classifier
|
43 |
+
|
44 |
+
|
45 |
+
@st.cache_data
|
46 |
+
def adapmit_classification(haystack_doc:pd.DataFrame,
|
47 |
+
threshold:float = 0.5,
|
48 |
+
classifier_model:pipeline= None
|
49 |
+
)->Tuple[DataFrame,Series]:
|
50 |
+
"""
|
51 |
+
Text-Classification on the list of texts provided. Classifier provides the
|
52 |
+
most appropriate label for each text. these labels are in terms of if text
|
53 |
+
belongs to which particular Sustainable Devleopment Goal (SDG).
|
54 |
+
Params
|
55 |
+
---------
|
56 |
+
haystack_doc: List of haystack Documents. The output of Preprocessing Pipeline
|
57 |
+
contains the list of paragraphs in different format,here the list of
|
58 |
+
Haystack Documents is used.
|
59 |
+
threshold: threshold value for the model to keep the results from classifier
|
60 |
+
classifiermodel: you can pass the classifier model directly,which takes priority
|
61 |
+
however if not then looks for model in streamlit session.
|
62 |
+
In case of streamlit avoid passing the model directly.
|
63 |
+
Returns
|
64 |
+
----------
|
65 |
+
df: Dataframe with two columns['SDG:int', 'text']
|
66 |
+
x: Series object with the unique SDG covered in the document uploaded and
|
67 |
+
the number of times it is covered/discussed/count_of_paragraphs.
|
68 |
+
"""
|
69 |
+
logging.info("Working on Adaptation-Mitigation Identification")
|
70 |
+
haystack_doc['Adapt-Mitig Label'] = 'NA'
|
71 |
+
df1 = haystack_doc[haystack_doc['Target Label'] == 'TARGET']
|
72 |
+
df = haystack_doc[haystack_doc['Target Label'] == 'NEGATIVE']
|
73 |
+
|
74 |
+
if not classifier_model:
|
75 |
+
classifier_model = st.session_state['adapmit_classifier']
|
76 |
+
|
77 |
+
predictions = classifier_model(list(df1.text))
|
78 |
+
# converting the predictions to desired format
|
79 |
+
list_ = []
|
80 |
+
for i in range(len(predictions)):
|
81 |
+
|
82 |
+
temp = predictions[i]
|
83 |
+
placeholder = {}
|
84 |
+
for j in range(len(temp)):
|
85 |
+
placeholder[temp[j]['label']] = temp[j]['score']
|
86 |
+
list_.append(placeholder)
|
87 |
+
labels_ = [{**list_[l]} for l in range(len(predictions))]
|
88 |
+
truth_df = DataFrame.from_dict(labels_)
|
89 |
+
truth_df = truth_df.round(2)
|
90 |
+
truth_df = truth_df.astype(float) >= threshold
|
91 |
+
truth_df = truth_df.astype(str)
|
92 |
+
categories = list(truth_df.columns)
|
93 |
+
truth_df['Adapt-Mitig Label'] = truth_df.apply(lambda x: {i if x[i]=='True'
|
94 |
+
else None for i in categories}, axis=1)
|
95 |
+
truth_df['Adapt-Mitig Label'] = truth_df.apply(lambda x:
|
96 |
+
list(x['Adapt-Mitig Label'] -{None}),axis=1)
|
97 |
+
df1['Adapt-Mitig Label'] = list(truth_df['Adapt-Mitig Label'])
|
98 |
+
df = pd.concat([df,df1])
|
99 |
+
|
100 |
+
return df
|
|