|
import random
|
|
import gradio as gr
|
|
import pandas as pd
|
|
|
|
|
|
from pyabsa import AspectTermExtraction as ATEPC
|
|
|
|
|
|
from src.models import aspect_extractor
|
|
from src.utils import load_atepc_examples
|
|
|
|
|
|
|
|
DESIRED_ATEPC_DATASETS = [
|
|
"Laptop14",
|
|
"Restaurant14",
|
|
"SemEval",
|
|
"Twitter",
|
|
"TShirt"
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
print("Loading ATEPC dataset examples for Gradio interface...")
|
|
atepc_dataset_examples = {}
|
|
|
|
|
|
for dataset_name in DESIRED_ATEPC_DATASETS:
|
|
try:
|
|
|
|
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.")
|
|
|
|
|
|
|
|
|
|
def run_atepc_inference(input_text: str, selected_dataset: str) -> tuple[pd.DataFrame, str]:
|
|
|
|
|
|
if aspect_extractor is None:
|
|
return pd.DataFrame({"Error": ["Model not initialized. Please check logs."]},
|
|
columns=["Error"]), "Model Unavailable"
|
|
|
|
analyzed_text = input_text.strip()
|
|
|
|
|
|
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:
|
|
|
|
prediction_result = aspect_extractor.predict(analyzed_text, pred_sentiment=True)
|
|
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(title="PyABSA Demonstration: Aspect-based Sentiment Analysis") as sentiment_analysis_app:
|
|
|
|
gr.Markdown("# <p align='center'>PyABSA: Multilingual Aspect-based Sentiment Analysis</p>")
|
|
gr.Markdown("---")
|
|
|
|
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."
|
|
)
|
|
|
|
|
|
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
|
|
)
|
|
|
|
|
|
|
|
atepc_dataset_selection = gr.Radio(
|
|
choices=DESIRED_ATEPC_DATASETS,
|
|
value=DESIRED_ATEPC_DATASETS[0] if DESIRED_ATEPC_DATASETS else None,
|
|
label="Select Dataset (for random examples):",
|
|
interactive=True
|
|
)
|
|
|
|
|
|
atepc_run_button = gr.Button("Analyze Aspects!", variant="primary")
|
|
|
|
|
|
atepc_output_sentence = gr.TextArea(label="Analyzed Sentence:", interactive=False)
|
|
atepc_prediction_results_df = gr.DataFrame(label="Aspect Prediction Results:", interactive=False)
|
|
|
|
|
|
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("---")
|
|
|
|
|
|
|
|
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)
|
|
|
|
|