Spaces:
Sleeping
Sleeping
Delete utils/ghg_classifier.py
Browse files- utils/ghg_classifier.py +0 -96
utils/ghg_classifier.py
DELETED
@@ -1,96 +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 |
-
'GHG':'GHG',
|
14 |
-
'NOT_GHG':'NON GHG TRANSPORT TARGET',
|
15 |
-
'NEGATIVE':'OTHERS',
|
16 |
-
}
|
17 |
-
|
18 |
-
|
19 |
-
@st.cache_resource
|
20 |
-
def load_ghgClassifier(config_file:str = None, classifier_name:str = None):
|
21 |
-
"""
|
22 |
-
loads the document classifier using haystack, where the name/path of model
|
23 |
-
in HF-hub as string is used to fetch the model object.Either configfile or
|
24 |
-
model should be passed.
|
25 |
-
1. https://docs.haystack.deepset.ai/reference/document-classifier-api
|
26 |
-
2. https://docs.haystack.deepset.ai/docs/document_classifier
|
27 |
-
Params
|
28 |
-
--------
|
29 |
-
config_file: config file path from which to read the model name
|
30 |
-
classifier_name: if modelname is passed, it takes a priority if not \
|
31 |
-
found then will look for configfile, else raise error.
|
32 |
-
Return: document classifier model
|
33 |
-
"""
|
34 |
-
if not classifier_name:
|
35 |
-
if not config_file:
|
36 |
-
logging.warning("Pass either model name or config file")
|
37 |
-
return
|
38 |
-
else:
|
39 |
-
config = getconfig(config_file)
|
40 |
-
classifier_name = config.get('ghg','MODEL')
|
41 |
-
|
42 |
-
logging.info("Loading ghg classifier")
|
43 |
-
doc_classifier = pipeline("text-classification",
|
44 |
-
model=classifier_name,
|
45 |
-
top_k =1)
|
46 |
-
|
47 |
-
return doc_classifier
|
48 |
-
|
49 |
-
|
50 |
-
@st.cache_data
|
51 |
-
def ghg_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 GHG Extraction")
|
75 |
-
haystack_doc['GHG Label'] = 'NA'
|
76 |
-
haystack_doc['GHG Score'] = 0.0
|
77 |
-
# applying GHG Identifier to only 'Target' paragraphs.
|
78 |
-
temp = haystack_doc[haystack_doc['Target Label'] == 'TARGET']
|
79 |
-
temp = temp.reset_index(drop=True)
|
80 |
-
df = haystack_doc[haystack_doc['Target Label'] == 'NEGATIVE']
|
81 |
-
df = df.reset_index(drop=True)
|
82 |
-
|
83 |
-
if not classifier_model:
|
84 |
-
classifier_model = st.session_state['ghg_classifier']
|
85 |
-
|
86 |
-
results = classifier_model(list(temp.text))
|
87 |
-
labels_= [(l[0]['label'],l[0]['score']) for l in results]
|
88 |
-
temp['GHG Label'],temp['GHG Score'] = zip(*labels_)
|
89 |
-
temp['GHG Label'] = temp['GHG Label'].apply(lambda x: _lab_dict[x])
|
90 |
-
# merge back Target and non-Target dataframe
|
91 |
-
df = pd.concat([df,temp])
|
92 |
-
df = df.reset_index(drop =True)
|
93 |
-
df['GHG Score'] = df['GHG Score'].round(2)
|
94 |
-
df.index += 1
|
95 |
-
|
96 |
-
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|