leavoigt commited on
Commit
657ca71
1 Parent(s): edc8421

Delete utils/policyaction_classifier.py

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