Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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-
|
6 |
token_classifier = pipeline("token-classification", model=checkpoint, aggregation_strategy="simple")
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Create Gradio interface
|
13 |
-
interf = gr.Interface(fn=
|
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__":
|