Amit234 commited on
Commit
e0c1df9
·
verified ·
1 Parent(s): 660cf9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -5
app.py CHANGED
@@ -2,15 +2,37 @@ import gradio as gr
2
  from transformers import pipeline
3
 
4
  # Load your model from Hugging Face
5
- checkpoint = "Amit234/distilbert-finetuned-med-ner/checkpoint-3924"
6
  token_classifier = pipeline("token-classification", model=checkpoint, aggregation_strategy="simple")
7
 
8
- # Define prediction function
9
- def classify_text(text):
10
- return token_classifier(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # Create Gradio interface
13
- interf = gr.Interface(fn=classify_text, inputs="text", outputs="json", title="NER for medical data")
14
 
15
  # Launch the interface
16
  if __name__ == "__main__":
 
2
  from transformers import pipeline
3
 
4
  # Load your model from Hugging Face
5
+ checkpoint = "Amit234/distilbert-finetuned-med-NER" # Use the path to your Hugging Face model
6
  token_classifier = pipeline("token-classification", model=checkpoint, aggregation_strategy="simple")
7
 
8
+ def merge_tokens(entities):
9
+ merged_entities = []
10
+ current_entity = None
11
+
12
+ for entity in entities:
13
+ if current_entity is None:
14
+ current_entity = entity
15
+ elif "##" in entity['word']:
16
+ current_entity['word'] += entity['word'][2:] # Remove "##" and concatenate the rest
17
+ current_entity['end'] = entity['end']
18
+ else:
19
+ merged_entities.append(current_entity)
20
+ current_entity = entity
21
+
22
+ if current_entity is not None:
23
+ merged_entities.append(current_entity)
24
+
25
+ return merged_entities
26
+
27
+
28
+
29
+ def final_function(text):
30
+ entities = token_classifier(text)
31
+ merged_entities = merge_tokens(entities)
32
+ return merged_entities
33
 
34
  # Create Gradio interface
35
+ interf = gr.Interface(fn=final_function, inputs="text", outputs="json", title="NER for medical data")
36
 
37
  # Launch the interface
38
  if __name__ == "__main__":