| from pyabsa import AspectTermExtraction as ATEPC |
| import os |
|
|
| MODEL_PATH = "checkpoints/ATEPC_MULTILINGUAL_CHECKPOINT" |
|
|
| def fix_model_filenames(directory): |
| if not os.path.exists(directory): |
| return False |
|
|
| for f in os.listdir(directory): |
| if f.endswith(".config") and f != "atepc.config": |
| os.rename( |
| os.path.join(directory, f), |
| os.path.join(directory, "atepc.config") |
| ) |
| return True |
|
|
| |
| fix_model_filenames(MODEL_PATH) |
|
|
| |
| aspect_extractor = ATEPC.AspectExtractor( |
| checkpoint=MODEL_PATH, |
| auto_device=True |
| ) |
|
|
| def predict(text): |
| """ |
| Hugging Face inference entrypoint |
| """ |
| if not text or not text.strip(): |
| return [] |
|
|
| result = aspect_extractor.predict( |
| [text], |
| print_result=False, |
| pred_sentiment=True |
| ) |
|
|
| data = result[0] |
|
|
| aspects = data.get("aspect", []) |
| sentiments = data.get("sentiment", []) |
| confidences = data.get("confidence", []) |
|
|
| output = [] |
|
|
| for asp, sent, conf in zip(aspects, sentiments, confidences): |
| output.append({ |
| "aspect": asp, |
| "sentiment": sent, |
| "confidence": round(conf * 100, 1) |
| }) |
|
|
| return output |
|
|