Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from openprompt.plms import load_plm
|
| 6 |
+
from openprompt import PromptDataLoader
|
| 7 |
+
from openprompt.prompts import ManualVerbalizer
|
| 8 |
+
from openprompt.prompts import ManualTemplate
|
| 9 |
+
from openprompt.data_utils import InputExample
|
| 10 |
+
from openprompt import PromptForClassification
|
| 11 |
+
|
| 12 |
+
# def sentiment_analysis(sentence, template, positive, neutral, nagetive ):
|
| 13 |
+
# model_name = "bert-base-uncased"
|
| 14 |
+
# nlp = pipeline("fill-mask", model=model_name)
|
| 15 |
+
# text = template.replace("[SENTENCE]",sentence)
|
| 16 |
+
# # return str(nlp(text))
|
| 17 |
+
# return sentence + template + positive + neutral + nagetive
|
| 18 |
+
|
| 19 |
+
# demo = gr.Interface(fn=sentiment_analysis,
|
| 20 |
+
# inputs = [gr.Textbox(placeholder="Enter sentence here.",label="sentence"),
|
| 21 |
+
# gr.Textbox(placeholder="Your template must have a [SENTENCE] token and a [MASK] token.",label="template"),
|
| 22 |
+
# gr.Textbox(placeholder="Separate words with Spaces.",label="positive"),
|
| 23 |
+
# gr.Textbox(placeholder="Separate words with Spaces.",label="neutral"),
|
| 24 |
+
# gr.Textbox(placeholder="Separate words with Spaces.",label="negative")
|
| 25 |
+
# ],
|
| 26 |
+
# outputs="text")
|
| 27 |
+
|
| 28 |
+
# demo.launch(server_port=8080)
|
| 29 |
+
|
| 30 |
+
def readLMwords():
|
| 31 |
+
alldata = pd.read_csv("LoughranMcDonald_MasterDictionary_2020.csv")
|
| 32 |
+
positive = list(alldata[alldata["Positive"]!=0]["Word"].str.lower())
|
| 33 |
+
negative = list(alldata[alldata["Negative"]!=0]["Word"].str.lower())
|
| 34 |
+
uncertainty = list(alldata[alldata["Uncertainty"]!=0]["Word"].str.lower())
|
| 35 |
+
return positive,negative,uncertainty
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def sentiment_analysis(sentence, model_name):
|
| 39 |
+
model_name = model_name
|
| 40 |
+
sentences = sentence.split('\n')
|
| 41 |
+
template = '{"placeholder":"text_a"} Shares are {"mask"}.'
|
| 42 |
+
classes = ['positive', 'neutral', 'negative']
|
| 43 |
+
positive,negative,neutral = readLMwords()
|
| 44 |
+
label_words = {
|
| 45 |
+
"positive": positive,
|
| 46 |
+
"neutral": neutral,
|
| 47 |
+
"negative": negative,
|
| 48 |
+
}
|
| 49 |
+
type_dic = {
|
| 50 |
+
"CCCC/ARCH_tuned_bert":"bert",
|
| 51 |
+
"bert-base-uncased":"bert",
|
| 52 |
+
"roberta-base":"roberta",
|
| 53 |
+
"yiyanghkust/finbert-pretrain":"bert",
|
| 54 |
+
"facebook/opt-125m":"opt",
|
| 55 |
+
"facebook/opt-350m":"opt",
|
| 56 |
+
}
|
| 57 |
+
testdata = []
|
| 58 |
+
for i,sentence in enumerate(sentences):
|
| 59 |
+
testdata.append(InputExample(guid=i,text_a=sentence,label=0))
|
| 60 |
+
|
| 61 |
+
plm, tokenizer, model_config, WrapperClass = load_plm(type_dic[model_name], model_name)
|
| 62 |
+
|
| 63 |
+
promptTemplate = ManualTemplate(
|
| 64 |
+
text = template,
|
| 65 |
+
tokenizer = tokenizer,
|
| 66 |
+
)
|
| 67 |
+
promptVerbalizer = ManualVerbalizer(
|
| 68 |
+
classes = classes,
|
| 69 |
+
label_words = label_words,
|
| 70 |
+
tokenizer = tokenizer,
|
| 71 |
+
)
|
| 72 |
+
test_dataloader = PromptDataLoader(
|
| 73 |
+
dataset = testdata,
|
| 74 |
+
tokenizer = tokenizer,
|
| 75 |
+
template = promptTemplate,
|
| 76 |
+
tokenizer_wrapper_class = WrapperClass,
|
| 77 |
+
batch_size = 4,
|
| 78 |
+
max_seq_length = 512,
|
| 79 |
+
)
|
| 80 |
+
prompt_model = PromptForClassification(
|
| 81 |
+
plm=plm,
|
| 82 |
+
template=promptTemplate,
|
| 83 |
+
verbalizer=promptVerbalizer,
|
| 84 |
+
freeze_plm=False #whether or not to freeze the pretrained language model
|
| 85 |
+
)
|
| 86 |
+
result = []
|
| 87 |
+
for step, inputs in enumerate(test_dataloader):
|
| 88 |
+
logits = prompt_model(inputs)
|
| 89 |
+
result.extend(torch.argmax(logits, dim=-1))
|
| 90 |
+
output = '\n'.join([classes[i] for i in result])
|
| 91 |
+
return output #classes[torch.argmax(logits, dim=-1)[0]]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
demo = gr.Interface(fn=sentiment_analysis,
|
| 95 |
+
inputs = [gr.Textbox(placeholder="Enter sentence here.",label="sentence",lines=5),
|
| 96 |
+
gr.Radio(choices=["ARCH_tuned_robert","FNCH_tuned_robert","AREN_tuned_robert",
|
| 97 |
+
"FNEN_tuned_robert","bert-base-uncased"], label="model choics")],
|
| 98 |
+
outputs=gr.Textbox(label="output",lines=5),
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
demo.launch()
|