|
import spacy
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
|
|
|
|
MODEL_PATH = Path("./training_400/model-best")
|
|
|
|
|
|
def load_model(path):
|
|
"""Loads the spaCy model."""
|
|
if not path.exists():
|
|
print(f"β Error: Model directory not found at {path.resolve()}")
|
|
print("Please ensure the path is correct and you have trained the model.")
|
|
sys.exit(1)
|
|
try:
|
|
|
|
|
|
nlp = spacy.load(path)
|
|
print(f"\nβ Successfully loaded model from: {path.resolve()}")
|
|
return nlp
|
|
except Exception as e:
|
|
print(f"β Error loading model from {path.resolve()}: {e}")
|
|
print("Please ensure the model path is correct and the model files are intact (especially meta.json).")
|
|
sys.exit(1)
|
|
|
|
def predict_entities(nlp, text):
|
|
"""Processes text and prints found entities."""
|
|
if not text or text.isspace():
|
|
print("Input text is empty.")
|
|
return
|
|
|
|
|
|
display_text = f"\"{text[:100]}...\"" if len(text) > 100 else f"\"{text}\""
|
|
print(f"\n---> Processing text: {display_text}")
|
|
|
|
|
|
doc = nlp(text)
|
|
|
|
|
|
if doc.ents:
|
|
print("\n--- Entities Found ---")
|
|
for ent in doc.ents:
|
|
print(f" Text: '{ent.text}'")
|
|
print(f" Label: {ent.label_}")
|
|
print(f" Start: {ent.start_char}, End: {ent.end_char}")
|
|
print("-" * 25)
|
|
else:
|
|
print("\n--- No entities found in this text. ---")
|
|
print("=" * 40)
|
|
|
|
def main():
|
|
"""Main function to load model and run interactive prediction loop."""
|
|
nlp_model = load_model(MODEL_PATH)
|
|
|
|
print("\n==============================")
|
|
print(" Interactive NER Predictor")
|
|
print("==============================")
|
|
print(f"Model loaded: {MODEL_PATH.name}")
|
|
print("Enter Tamil text below to identify entities.")
|
|
print("Type 'quit' or 'exit' (or just press Enter on an empty line) to stop.")
|
|
print("-" * 40)
|
|
|
|
while True:
|
|
try:
|
|
|
|
user_input = input("Enter text >> ")
|
|
|
|
|
|
if user_input.lower() in ["quit", "exit", ""]:
|
|
print("\nExiting predictor.")
|
|
break
|
|
|
|
|
|
predict_entities(nlp_model, user_input)
|
|
|
|
except EOFError:
|
|
print("\nExiting predictor.")
|
|
break
|
|
except KeyboardInterrupt:
|
|
print("\nExiting predictor.")
|
|
break
|
|
except Exception as e:
|
|
print(f"\nAn unexpected error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |