leavoigt commited on
Commit
8a9ef0d
1 Parent(s): 9ce66c8

Delete utils/adapmit_classifier.py

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