Views
No views yet
1from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
2
3# Load the pre-trained model and tokenizer
4model_name = "quim-motger/t-frex-roberta-large"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForTokenClassification.from_pretrained(model_name)
7
8# Create a pipeline for named entity recognition
9ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
10
11# Example text
12text = "The share note file feature is completely useless."
13
14# Perform named entity recognition
15entities = ner_pipeline(text)
16
17# Print the recognized entities
18for entity in entities:
19 print(f"Entity: {entity['word']}, Label: {entity['entity']}, Score: {entity['score']:.4f}")
20
21# Example with multiple texts
22texts = [
23 "Great app I've tested a lot of free habit tracking apps and this is by far my favorite.",
24 "The only negative feedback I can give about this app is the difficulty level to set a sleep timer on it."
25]
26
27# Perform named entity recognition on multiple texts
28for text in texts:
29 entities = ner_pipeline(text)
30 print(f"Text: {text}")
31 for entity in entities:
32 print(f" Entity: {entity['word']}, Label: {entity['entity']}, Score: {entity['score']:.4f}")
33