File size: 1,446 Bytes
c2f789d
 
 
 
10fdf8a
c2f789d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10fdf8a
 
c2f789d
 
2b152f7
c2f789d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import gradio as gr
from transformers import AutoTokenizer, AutoModelForTokenClassification,RobertaTokenizer
import torch
from fin_readability_sustainability import BERTClass, do_predict
import pandas as pd

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


tokenizer_sus = RobertaTokenizer.from_pretrained('roberta-base')
model_sustain = BERTClass(2, "sustanability")
model_sustain.to(device)
model_sustain.load_state_dict(torch.load('sustainability_model.bin', map_location=device)['model_state_dict'])


from nltk.tokenize import sent_tokenize
def get_sustainability(text):
  df = pd.DataFrame({'sentence':sent_tokenize(text)})
  actual_predictions_sustainability = do_predict(model_sustain, tokenizer_sus, df)
  highlight = []
  for sent, prob in zip(df['sentence'].values, actual_predictions_sustainability[1]):
    if prob>=4.384316:
      highlight.append((sent, 'non-sustainable'))
    elif prob<=1.423736:
      highlight.append((sent, 'sustainable'))
    else:
      highlight.append((sent, '-'))
  return highlight



# b6 = gr.Button("Get Sustainability")
              #b6.click(get_sustainability, inputs = text, outputs = gr.HighlightedText())
              
              
iface = gr.Interface(fn=get_sustainability, inputs=gr.inputs.Textbox(lines=5, placeholder="Enter Financial Text here..."), title="CONBERT",description="SUSTAINABILITY TOOL", outputs=['highlight'], allow_flagging="never")
iface.launch()