Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
# file: app.py
|
3 |
+
# time: 18:37 23/09/2023
|
4 |
+
# author: Amir Khan
|
5 |
+
# github: https://github.com/Amir22010
|
6 |
+
|
7 |
+
import numpy
|
8 |
+
import ast
|
9 |
+
import gradio as gr
|
10 |
+
import pandas as pd
|
11 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
12 |
+
|
13 |
+
try:
|
14 |
+
tokenizer_english = AutoTokenizer.from_pretrained("amir22010/PyABSA_Hospital_English_allenai_tk-instruct-base-def-pos_FinedTuned_Model")
|
15 |
+
double_english_generator = AutoModelForSeq2SeqLM.from_pretrained("amir22010/PyABSA_Hospital_English_allenai_tk-instruct-base-def-pos_FinedTuned_Model")
|
16 |
+
except:
|
17 |
+
print("english model load error")
|
18 |
+
'''
|
19 |
+
try:
|
20 |
+
tokenizer_multilingual = AutoTokenizer.from_pretrained("amir22010/layoutxlm-xfund-ja")
|
21 |
+
double_multilingual_generator = AutoModelForSeq2SeqLM.from_pretrained("amir22010/layoutxlm-xfund-ja")
|
22 |
+
except:
|
23 |
+
print("multilingual model load error")
|
24 |
+
|
25 |
+
try:
|
26 |
+
tokenizer_keybert = AutoTokenizer.from_pretrained("amir22010/layoutxlm-xfund-ja")
|
27 |
+
double_keybert_generator = AutoModelForSeq2SeqLM.from_pretrained("amir22010/layoutxlm-xfund-ja")
|
28 |
+
except:
|
29 |
+
print("keybert model load error")
|
30 |
+
|
31 |
+
'''
|
32 |
+
def perform_asde_inference(text, dataset, model_id):
|
33 |
+
if not text:
|
34 |
+
df = pd.read_csv('pyabsa_english.csv')
|
35 |
+
random_i = np.random.randint(low=0, high=df.shape[0], size=(1,)).flat[0]
|
36 |
+
selected_df = df.iloc[random_i]
|
37 |
+
text = selected_df['clean_text'].iloc[0]
|
38 |
+
true_aspect = selected_df['actual_aspects'].iloc[0]
|
39 |
+
true_sentiment = selected_df['actual_sentiments'].iloc[0]
|
40 |
+
|
41 |
+
|
42 |
+
bos_instruction = """Definition: The output will be the aspects (both implicit and explicit) and the aspects sentiment polarity. In cases where there are no aspects the output should be noaspectterm:none.
|
43 |
+
Positive example 1-
|
44 |
+
input: this hospital has a good team of doctors who will take care of all your needs brilliantly.
|
45 |
+
output: doctors:positive
|
46 |
+
Positive example 2-
|
47 |
+
input: Arthur as Irv at ham hospital ran an Nagar , Madurai has a doctor who engages you in a conversation and tries to take your mind off the pain and he has trained the staff to do so as well.
|
48 |
+
output: doctor:positive, staff:positive
|
49 |
+
Now complete the following example-
|
50 |
+
input: """
|
51 |
+
delim_instruct = ''
|
52 |
+
eos_instruct = ' \noutput:'
|
53 |
+
|
54 |
+
if model_id == "PyABSA_Hospital_English_allenai/tk-instruct-base-def-pos_FinedTuned_Model":
|
55 |
+
tokenized_text = tokenizer_english(bos_instruction + text + delim_instruct + eos_instruct, return_tensors="pt")
|
56 |
+
output = double_english_generator.generate(tokenized_text.input_ids,max_length=512)
|
57 |
+
model_generated = tokenizer_english.decode(output[0], skip_special_tokens=True)
|
58 |
+
'''
|
59 |
+
elif model_id == "PyABSA_Hospital_Multilingual_allenai/tk-instruct-base-def-pos_FinedTuned_Model":
|
60 |
+
tokenized_text = tokenizer_multilingual(bos_instruction + text + delim_instruct + eos_instruct, return_tensors="pt")
|
61 |
+
output = double_multilingual_generator.generate(tokenized_text.input_ids,max_length=512)
|
62 |
+
result = tokenizer_multilingual.decode(output[0], skip_special_tokens=True)
|
63 |
+
elif model_id == "PyABSA_Hospital_KeyBert_allenai/tk-instruct-base-def-pos_FinedTuned_Model":
|
64 |
+
tokenized_text = tokenizer_keybert(bos_instruction + text + delim_instruct + eos_instruct, return_tensors="pt")
|
65 |
+
output = double_keybert_generator.generate(tokenized_text.input_ids,max_length=512)
|
66 |
+
result = tokenizer_keybert.decode(output[0], skip_special_tokens=True)
|
67 |
+
'''
|
68 |
+
pred_asp = [i.split(':')[0] for i in model_generated.split(',')]
|
69 |
+
pred_sent = [i.split(':')[1] for i in model_generated.split(',')]
|
70 |
+
|
71 |
+
pred_doubles = pd.DataFrame(list(map(list, zip(pred_asp, pred_sent))),columns=['Aspect','Sentiment'])
|
72 |
+
true_doubles = pd.DataFrame(list(map(list, zip(ast.literal_eval(true_aspect), ast.literal_eval(true_sentiment)))),columns=['Aspect','Sentiment'])
|
73 |
+
|
74 |
+
return pred_doubles, true_doubles, text, model_generated
|
75 |
+
|
76 |
+
def run_demo(text, dataset, model_id):
|
77 |
+
try:
|
78 |
+
data = {
|
79 |
+
"text": text,
|
80 |
+
"dataset": dataset
|
81 |
+
}
|
82 |
+
return inference(text, dataset, model_id)
|
83 |
+
except Exception as e:
|
84 |
+
print(e)
|
85 |
+
|
86 |
+
|
87 |
+
def inference(text, dataset, model_id):
|
88 |
+
return perform_asde_inference(text, dataset, model_id)
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
demo = gr.Blocks()
|
92 |
+
with demo:
|
93 |
+
with gr.Row():
|
94 |
+
if triplet_extractor:
|
95 |
+
with gr.Column():
|
96 |
+
gr.Markdown(
|
97 |
+
"# <p align='center'>Hospital Review Aspect Sentiment Generation</p>"
|
98 |
+
)
|
99 |
+
with gr.Row():
|
100 |
+
with gr.Column():
|
101 |
+
asde_input_sentence = gr.Textbox(
|
102 |
+
placeholder="Leave this box blank and choose a dataset will give you a random example...",
|
103 |
+
label="Example:",
|
104 |
+
)
|
105 |
+
gr.Markdown(
|
106 |
+
"You can find code and dataset at [MTech Thesis Project](https://github.com/Amir22010)"
|
107 |
+
)
|
108 |
+
asde_dataset_ids = gr.Radio(
|
109 |
+
choices=[
|
110 |
+
"HospitalReviews"
|
111 |
+
],
|
112 |
+
value="HospitalReviews",
|
113 |
+
label="Datasets",
|
114 |
+
)
|
115 |
+
|
116 |
+
asde_model_ids = gr.Radio(
|
117 |
+
choices=[
|
118 |
+
"PyABSA_Hospital_English_allenai/tk-instruct-base-def-pos_FinedTuned_Model",
|
119 |
+
# "PyABSA_Hospital_Multilingual_allenai/tk-instruct-base-def-pos_FinedTuned_Model",
|
120 |
+
# "PyABSA_Hospital_KeyBert_allenai/tk-instruct-base-def-pos_FinedTuned_Model"
|
121 |
+
],
|
122 |
+
value="PyABSA_Hospital_English_allenai/tk-instruct-base-def-pos_FinedTuned_Model",
|
123 |
+
label="Fine-tuned Models on Hospital Review custom data",
|
124 |
+
)
|
125 |
+
|
126 |
+
asde_inference_button = gr.Button("Let's go!")
|
127 |
+
|
128 |
+
asde_output_text = gr.TextArea(label="Example:")
|
129 |
+
|
130 |
+
asde_model_output_generated_sentence = gr.Textbox(
|
131 |
+
placeholder="Text Generated...",
|
132 |
+
label="Model Prediction Text Generated:",
|
133 |
+
)
|
134 |
+
asde_output_pred_df = gr.DataFrame(
|
135 |
+
label="Predicted Aspect & Sentiment:"
|
136 |
+
)
|
137 |
+
asde_output_true_df = gr.DataFrame(
|
138 |
+
label="Original Aspect & Sentiment:"
|
139 |
+
)
|
140 |
+
|
141 |
+
asde_inference_button.click(
|
142 |
+
fn=run_demo,
|
143 |
+
inputs=[
|
144 |
+
asde_input_sentence,
|
145 |
+
asde_dataset_ids,
|
146 |
+
asde_model_ids
|
147 |
+
gr.Text("ASDE", visible=False),
|
148 |
+
],
|
149 |
+
outputs=[
|
150 |
+
asde_output_pred_df,
|
151 |
+
asde_output_true_df,
|
152 |
+
asde_output_text,
|
153 |
+
asde_model_output_generated_sentence
|
154 |
+
],
|
155 |
+
)
|
156 |
+
gr.Markdown(
|
157 |
+
"""### GitHub Repo: [MTech Thesis Project](https://github.com/Amir22010)
|
158 |
+
### Author: [Amir Khan](https://github.com/Amir22010)
|
159 |
+
"""
|
160 |
+
)
|
161 |
+
|
162 |
+
demo.launch()
|