Spaces:
Runtime error
Runtime error
HarshaBattula commited on
Commit Β·
32adcec
1
Parent(s): e1eddce
adding gradio interface for aspect polarity detection
Browse files- app.py +87 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import locale
|
| 2 |
+
locale.getpreferredencoding = lambda: "UTF-8"
|
| 3 |
+
from pyabsa import available_checkpoints
|
| 4 |
+
from pyabsa import ATEPCCheckpointManager
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def annotate_sentiment(tokens, aspect_positions, sentiment_list):
|
| 10 |
+
"""
|
| 11 |
+
Function to post-process the output from the aspect extractor model.
|
| 12 |
+
Annotates tokens based on sentiment, either positive (+) or negative (-).
|
| 13 |
+
|
| 14 |
+
:param tokens: list of tokens to be processed
|
| 15 |
+
:param aspect_positions: list of positions in tokens that refer to aspects
|
| 16 |
+
:param sentiment_list: list of sentiments corresponding to each aspect
|
| 17 |
+
:return: list of tuples where each tuple contains a token and its corresponding sentiment (if any)
|
| 18 |
+
"""
|
| 19 |
+
annotated_tokens = []
|
| 20 |
+
aspect_index = 0
|
| 21 |
+
for i, token_group in enumerate(tokens):
|
| 22 |
+
if aspect_index < len(aspect_positions) and i == aspect_positions[aspect_index]:
|
| 23 |
+
for token in token_group:
|
| 24 |
+
sentiment = '+' if sentiment_list[aspect_index] == 'Positive' else '-'
|
| 25 |
+
annotated_tokens.append((token, sentiment))
|
| 26 |
+
aspect_index += 1
|
| 27 |
+
else:
|
| 28 |
+
for token in token_group:
|
| 29 |
+
annotated_tokens.append((token, None))
|
| 30 |
+
|
| 31 |
+
# Add space between groups of tokens, if it's not the last group
|
| 32 |
+
if i != len(tokens) - 1:
|
| 33 |
+
annotated_tokens.append((' ', None))
|
| 34 |
+
|
| 35 |
+
return annotated_tokens
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def annotate_text_sentiment(text):
|
| 39 |
+
"""
|
| 40 |
+
Interface function to extract aspects and their sentiments from the given text.
|
| 41 |
+
Uses an aspect extractor model, then processes its output for sentiment annotation.
|
| 42 |
+
|
| 43 |
+
:param text: string to be processed
|
| 44 |
+
:return: list of tuples where each tuple contains a token from the text and its corresponding sentiment (if any)
|
| 45 |
+
"""
|
| 46 |
+
aspect_extraction_result = aspect_extractor.extract_aspect(inference_source=[text], pred_sentiment=True)[0]
|
| 47 |
+
tokens = aspect_extraction_result["tokens"]
|
| 48 |
+
|
| 49 |
+
# If no aspects found, aspect_positions and sentiment_list are empty
|
| 50 |
+
if len(aspect_extraction_result['position']) == 0:
|
| 51 |
+
aspect_positions = []
|
| 52 |
+
sentiment_list = []
|
| 53 |
+
else:
|
| 54 |
+
aspect_positions = [position[0]-1 for position in aspect_extraction_result['position']]
|
| 55 |
+
sentiment_list = aspect_extraction_result["sentiment"]
|
| 56 |
+
|
| 57 |
+
annotated_tokens = annotate_sentiment(tokens, aspect_positions, sentiment_list)
|
| 58 |
+
return annotated_tokens
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Initializing the aspect extractor model
|
| 62 |
+
checkpoint_map = available_checkpoints()
|
| 63 |
+
aspect_extractor = ATEPCCheckpointManager.get_aspect_extractor(checkpoint='english',
|
| 64 |
+
auto_device=True # False means load model on CPU
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
demo = gr.Interface(
|
| 70 |
+
annotate_text_sentiment,
|
| 71 |
+
[
|
| 72 |
+
gr.Textbox(
|
| 73 |
+
label="Enter the text text",
|
| 74 |
+
info="aspect and sentiment detection",
|
| 75 |
+
lines=3,
|
| 76 |
+
),
|
| 77 |
+
],
|
| 78 |
+
gr.HighlightedText(
|
| 79 |
+
label="Aspect Detector based on DEBERT",
|
| 80 |
+
combine_adjacent=True,
|
| 81 |
+
show_legend=True,
|
| 82 |
+
).style(color_map={"+": "green", "-": "red"}),
|
| 83 |
+
theme=gr.themes.Base()
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pyabsa==1.16.27
|
| 2 |
+
gradio
|
| 3 |
+
locale
|