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

Delete utils/sector_classifier.py

Browse files
Files changed (1) hide show
  1. utils/sector_classifier.py +0 -106
utils/sector_classifier.py DELETED
@@ -1,106 +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
-
12
- @st.cache_resource
13
- def load_sectorClassifier(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('sector','MODEL')
34
-
35
- logging.info("Loading sector classifier")
36
- # we are using the pipeline as the model is multilabel and DocumentClassifier
37
- # from Haystack doesnt support multilabel
38
- # in pipeline we use 'sigmoid' to explicitly tell pipeline to make it multilabel
39
- # if not then it will automatically use softmax, which is not a desired thing.
40
- # doc_classifier = TransformersDocumentClassifier(
41
- # model_name_or_path=classifier_name,
42
- # task="text-classification",
43
- # top_k = None)
44
-
45
- doc_classifier = pipeline("text-classification",
46
- model=classifier_name,
47
- return_all_scores=True,
48
- function_to_apply= "sigmoid")
49
-
50
- return doc_classifier
51
-
52
-
53
- @st.cache_data
54
- def sector_classification(haystack_doc:pd.DataFrame,
55
- threshold:float = 0.5,
56
- classifier_model:pipeline= None
57
- )->Tuple[DataFrame,Series]:
58
- """
59
- Text-Classification on the list of texts provided. Classifier provides the
60
- most appropriate label for each text. these labels are in terms of if text
61
- belongs to which particular Sustainable Devleopment Goal (SDG).
62
- Params
63
- ---------
64
- haystack_doc: List of haystack Documents. The output of Preprocessing Pipeline
65
- contains the list of paragraphs in different format,here the list of
66
- Haystack Documents is used.
67
- threshold: threshold value for the model to keep the results from classifier
68
- classifiermodel: you can pass the classifier model directly,which takes priority
69
- however if not then looks for model in streamlit session.
70
- In case of streamlit avoid passing the model directly.
71
- Returns
72
- ----------
73
- df: Dataframe with two columns['SDG:int', 'text']
74
- x: Series object with the unique SDG covered in the document uploaded and
75
- the number of times it is covered/discussed/count_of_paragraphs.
76
- """
77
- logging.info("Working on Sector Identification")
78
- haystack_doc['Sector Label'] = 'NA'
79
- # df1 = haystack_doc[haystack_doc['Target Label'] == 'TARGET']
80
- # df = haystack_doc[haystack_doc['Target Label'] == 'NEGATIVE']
81
- if not classifier_model:
82
- classifier_model = st.session_state['sector_classifier']
83
-
84
- predictions = classifier_model(list(haystack_doc.text))
85
-
86
- list_ = []
87
- for i in range(len(predictions)):
88
-
89
- temp = predictions[i]
90
- placeholder = {}
91
- for j in range(len(temp)):
92
- placeholder[temp[j]['label']] = temp[j]['score']
93
- list_.append(placeholder)
94
- labels_ = [{**list_[l]} for l in range(len(predictions))]
95
- truth_df = DataFrame.from_dict(labels_)
96
- truth_df = truth_df.round(2)
97
- truth_df = truth_df.astype(float) >= threshold
98
- truth_df = truth_df.astype(str)
99
- categories = list(truth_df.columns)
100
- truth_df['Sector Label'] = truth_df.apply(lambda x: {i if x[i]=='True' else
101
- None for i in categories}, axis=1)
102
- truth_df['Sector Label'] = truth_df.apply(lambda x: list(x['Sector Label']
103
- -{None}),axis=1)
104
- haystack_doc['Sector Label'] = list(truth_df['Sector Label'])
105
- # df = pd.concat([df,df1])
106
- return haystack_doc