File size: 6,226 Bytes
bd9b5f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import random
import gradio as gr
import pandas as pd

# Import necessary components from PyABSA for dataset listing
from pyabsa import AspectTermExtraction as ATEPC

# Import the initialized ATEPC model and utility functions from our local files
from src.models import aspect_extractor
from src.utils import load_atepc_examples


# Defining the specific datasets
DESIRED_ATEPC_DATASETS = [
    "Laptop14",
    "Restaurant14",
    "SemEval",
    "Twitter",
    "TShirt"
]

# Pre load Dataset Examples
# This dictionary will store example sentences for each desired dataset,
# which are used when the user leaves the input text box blank.

print("Loading ATEPC dataset examples for Gradio interface...")
atepc_dataset_examples = {}
# Iterate through available ATEPC datasets and load examples for each
# Filter to only include the desired datasets
for dataset_name in DESIRED_ATEPC_DATASETS:
    try:
        # Check if the dataset name is valid in ATEPC.ATEPCDatasetList
        if hasattr(ATEPC.ATEPCDatasetList(), dataset_name):
            atepc_dataset_examples[dataset_name] = load_atepc_examples(dataset_name)
        else:
            print(f"Warning: Dataset '{dataset_name}' not found in ATEPC.ATEPCDatasetList. Skipping.")
    except Exception as e:
        print(f"Error loading examples for ATEPC dataset '{dataset_name}': {e}")
print("ATEPC dataset examples loading complete.")


# Inference Function for Gradio

def run_atepc_inference(input_text: str, selected_dataset: str) -> tuple[pd.DataFrame, str]:

    # Check if the aspect_extractor model was successfully initialized
    if aspect_extractor is None:
        return pd.DataFrame({"Error": ["Model not initialized. Please check logs."]},
                            columns=["Error"]), "Model Unavailable"

    analyzed_text = input_text.strip() # Remove leading/trailing whitespace

    # If no text is provided, select a random example from the pre-loaded data
    if not analyzed_text:
        examples = atepc_dataset_examples.get(selected_dataset)
        if examples:
            analyzed_text = random.choice(examples)
        else:
            return pd.DataFrame({"Message": ["No examples available for this dataset or input text provided."]},
                                columns=["Message"]), "Please provide text or select a valid dataset."

    print(f"Performing ATEPC Inference on: '{analyzed_text}' (Dataset: {selected_dataset})")

    try:
        # Predict aspects and their sentiments
        prediction_result = aspect_extractor.predict(analyzed_text, pred_sentiment=True)

        # Check if any aspects were detected
        if not prediction_result or not prediction_result.get("aspect"):
            return pd.DataFrame({"Message": ["No aspects detected for the given text."]},
                                columns=["Message"]), analyzed_text

        # Create a DataFrame from the prediction results
        df_result = pd.DataFrame(
            {
                "Aspect": prediction_result["aspect"],
                "Sentiment": prediction_result["sentiment"],
                "Confidence": [round(c, 4) for c in prediction_result["confidence"]],
                "Position": prediction_result["position"],
            }
        )
        return df_result, analyzed_text
    except Exception as e:
        print(f"Error during ATEPC inference: {e}")
        return pd.DataFrame({"Error": [f"An error occurred: {e}"]},
                            columns=["Error"]), analyzed_text


# Gradio User Interface Definition

# Initialize the Gradio Blocks interface
with gr.Blocks(title="PyABSA Demonstration: Aspect-based Sentiment Analysis") as sentiment_analysis_app:
    # Main title for the entire application
    gr.Markdown("# <p align='center'>PyABSA: Multilingual Aspect-based Sentiment Analysis</p>")
    gr.Markdown("---") # Visual separator

    with gr.Row():
        with gr.Column():
            gr.Markdown("## 📈 Analyze Aspects and Sentiments")
            gr.Markdown(
                "This tool identifies specific aspects (entities or attributes) in a sentence "
                "and determines the sentiment (positive, negative, neutral) associated with each. "
                "For example, in 'The laptop's battery life is excellent', 'battery life' would be "
                "identified with a 'positive' sentiment."
            )

            # Input area for ATEPC
            atepc_input_box = gr.Textbox(
                placeholder="Type a sentence here, or leave blank to load a random example from a dataset...",
                label="Input Sentence:",
                lines=3
            )


            # Dataset selection for ATEPC examples restricted to desired list
            atepc_dataset_selection = gr.Radio(
                choices=DESIRED_ATEPC_DATASETS, # Use the predefined list
                value=DESIRED_ATEPC_DATASETS[0] if DESIRED_ATEPC_DATASETS else None, # Set default to first or None
                label="Select Dataset (for random examples):",
                interactive=True
            )

            # Button to trigger ATEPC inference
            atepc_run_button = gr.Button("Analyze Aspects!", variant="primary")

            # Output areas for ATEPC
            atepc_output_sentence = gr.TextArea(label="Analyzed Sentence:", interactive=False)
            atepc_prediction_results_df = gr.DataFrame(label="Aspect Prediction Results:", interactive=False)

            # Define the interaction for the ATEPC button click
            atepc_run_button.click(
                fn=run_atepc_inference,
                inputs=[atepc_input_box, atepc_dataset_selection],
                outputs=[atepc_prediction_results_df, atepc_output_sentence],
                api_name="run_atepc_inference"
            )

    gr.Markdown("---") # Visual separator


# Launch the Gradio application
if __name__ == "__main__":
    if aspect_extractor is None:
        print("Warning: PyABSA ATEPC model failed to initialize. The application may not function correctly.")
    sentiment_analysis_app.launch(share=False, debug=True)