Pranava Kailash commited on
Commit
bd6328b
·
1 Parent(s): 6cc3bb2

CyNER2.0 Runtime Memory Optimized v2

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -4,11 +4,14 @@ from collections import defaultdict
4
 
5
  # Load model and tokenizer
6
  path_to_checkpoint = 'PranavaKailash/CyNER-2.0-DeBERTa-v3-base'
7
- tokenizer = AutoTokenizer.from_pretrained(path_to_checkpoint, use_fast=True)
8
  model = AutoModelForTokenClassification.from_pretrained(path_to_checkpoint)
9
- ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
10
 
11
- MAX_INPUT_LENGTH = 500 # Set an appropriate length limit
 
 
 
 
12
 
13
  def tag_sentence(sentence, entities_dict):
14
  """
@@ -53,8 +56,8 @@ def perform_ner(text):
53
  Run NER pipeline and prepare results for display.
54
  """
55
  entities = ner_pipeline(text)
56
-
57
  entities_dict = defaultdict(list)
 
58
  for entity in entities:
59
  entities_dict[entity['entity']].append({
60
  "entity": entity['entity'],
@@ -64,7 +67,7 @@ def perform_ner(text):
64
  "start": entity['start'],
65
  "end": entity['end']
66
  })
67
-
68
  tagged_sentence = tag_sentence(text, entities_dict)
69
  return dict(entities_dict), tagged_sentence
70
 
@@ -75,9 +78,7 @@ st.write("Enter text to get named entity recognition results.")
75
  input_text = st.text_area("Input Text", "Type your text here...")
76
 
77
  if st.button("Analyze"):
78
- if len(input_text) > MAX_INPUT_LENGTH:
79
- st.warning(f"Text is too long! Please enter less than {MAX_INPUT_LENGTH} characters.")
80
- elif input_text.strip():
81
  entities_dict, tagged_sentence = perform_ner(input_text)
82
 
83
  # Display results
@@ -87,4 +88,4 @@ if st.button("Analyze"):
87
  st.subheader("Entities and Details")
88
  st.json(entities_dict)
89
  else:
90
- st.warning("Please enter some text for analysis.")
 
4
 
5
  # Load model and tokenizer
6
  path_to_checkpoint = 'PranavaKailash/CyNER-2.0-DeBERTa-v3-base'
7
+ tokenizer = AutoTokenizer.from_pretrained(path_to_checkpoint, use_fast=True, max_length=768)
8
  model = AutoModelForTokenClassification.from_pretrained(path_to_checkpoint)
 
9
 
10
+ # Ensure the model is loaded on CPU explicitly to avoid any device issues
11
+ model.to('cpu')
12
+
13
+ # Initialize the NER pipeline
14
+ ner_pipeline = pipeline("ner", model=model, tokenizer=tokenizer)
15
 
16
  def tag_sentence(sentence, entities_dict):
17
  """
 
56
  Run NER pipeline and prepare results for display.
57
  """
58
  entities = ner_pipeline(text)
 
59
  entities_dict = defaultdict(list)
60
+
61
  for entity in entities:
62
  entities_dict[entity['entity']].append({
63
  "entity": entity['entity'],
 
67
  "start": entity['start'],
68
  "end": entity['end']
69
  })
70
+
71
  tagged_sentence = tag_sentence(text, entities_dict)
72
  return dict(entities_dict), tagged_sentence
73
 
 
78
  input_text = st.text_area("Input Text", "Type your text here...")
79
 
80
  if st.button("Analyze"):
81
+ if input_text.strip():
 
 
82
  entities_dict, tagged_sentence = perform_ner(input_text)
83
 
84
  # Display results
 
88
  st.subheader("Entities and Details")
89
  st.json(entities_dict)
90
  else:
91
+ st.warning("Please enter some text for analysis.")