leavoigt commited on
Commit
edc8421
1 Parent(s): 926da07

Delete utils/netzero_classifier.py

Browse files
Files changed (1) hide show
  1. utils/netzero_classifier.py +0 -88
utils/netzero_classifier.py DELETED
@@ -1,88 +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 NETZERO TARGET',
14
- 'NETZERO':'NETZERO TARGET',
15
- }
16
-
17
- @st.cache_resource
18
- def load_netzeroClassifier(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('netzero','MODEL')
39
-
40
- logging.info("Loading netzero classifier")
41
- doc_classifier = pipeline("text-classification",
42
- model=classifier_name,
43
- top_k =1)
44
-
45
- return doc_classifier
46
-
47
-
48
- @st.cache_data
49
- def netzero_classification(haystack_doc:pd.DataFrame,
50
- threshold:float = 0.8,
51
- classifier_model:pipeline= None
52
- )->Tuple[DataFrame,Series]:
53
- """
54
- Text-Classification on the list of texts provided. Classifier provides the
55
- most appropriate label for each text. these labels are in terms of if text
56
- belongs to which particular Sustainable Devleopment Goal (SDG).
57
- Params
58
- ---------
59
- haystack_doc: List of haystack Documents. The output of Preprocessing Pipeline
60
- contains the list of paragraphs in different format,here the list of
61
- Haystack Documents is used.
62
- threshold: threshold value for the model to keep the results from classifier
63
- classifiermodel: you can pass the classifier model directly,which takes priority
64
- however if not then looks for model in streamlit session.
65
- In case of streamlit avoid passing the model directly.
66
- Returns
67
- ----------
68
- df: Dataframe with two columns['SDG:int', 'text']
69
- x: Series object with the unique SDG covered in the document uploaded and
70
- the number of times it is covered/discussed/count_of_paragraphs.
71
- """
72
- logging.info("Working on Netzero Extraction")
73
- haystack_doc['Netzero Label'] = 'NA'
74
- haystack_doc['Netzero Score'] = 'NA'
75
- temp = haystack_doc[haystack_doc['Target Label'] == 'TARGET']
76
- df = haystack_doc[haystack_doc['Target Label'] == 'NEGATIVE']
77
-
78
- if not classifier_model:
79
- classifier_model = st.session_state['netzero_classifier']
80
-
81
- results = classifier_model(list(temp.text))
82
- labels_= [(l[0]['label'],l[0]['score']) for l in results]
83
- temp['Netzero Label'],temp['Netzero Score'] = zip(*labels_)
84
- df = pd.concat([df,temp])
85
- df = df.reset_index(drop =True)
86
- df.index += 1
87
-
88
- return df