Mhammad Ibrahim commited on
Commit
f77bff8
·
1 Parent(s): f4ce35e
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -1,31 +1,33 @@
1
- # import gradio as gr
2
-
3
- # def greet(name):
4
- # return "Hello " + name + "!!"
5
-
6
- # demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- # demo.launch()
8
-
9
  import gradio as gr
10
  import torch
11
  from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
12
 
13
  # Load model and tokenizer from Hugging Face Hub
14
  tokenizer = AutoTokenizer.from_pretrained("Mhammad2023/bert-finetuned-ner-torch")
15
- model = AutoModelForTokenClassification.from_pretrained(
16
- "Mhammad2023/bert-finetuned-ner-torch"
 
 
 
 
 
 
17
  )
18
- classifier = pipeline("token-classification", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
19
 
20
  def predict(text):
21
  results = classifier(text)
22
  if not results:
23
  return "No entities found"
24
-
25
  output = []
26
  for entity in results:
27
- output.append(f"{entity['word']}: {entity['entity']} ({round(entity['score']*100, 2)}%)")
28
-
29
  return "\n".join(output)
30
 
31
- gr.Interface(fn=predict, inputs="text", outputs="text", title="Named Entity Recognition").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
4
 
5
  # Load model and tokenizer from Hugging Face Hub
6
  tokenizer = AutoTokenizer.from_pretrained("Mhammad2023/bert-finetuned-ner-torch")
7
+ model = AutoModelForTokenClassification.from_pretrained("Mhammad2023/bert-finetuned-ner-torch")
8
+
9
+ # Use aggregation_strategy="simple" to group B/I tokens
10
+ classifier = pipeline(
11
+ "token-classification",
12
+ model=model,
13
+ tokenizer=tokenizer,
14
+ aggregation_strategy="simple"
15
  )
 
16
 
17
  def predict(text):
18
  results = classifier(text)
19
  if not results:
20
  return "No entities found"
21
+
22
  output = []
23
  for entity in results:
24
+ output.append(f"{entity['word']}: {entity['entity_group']} ({round(entity['score']*100, 2)}%)")
25
+
26
  return "\n".join(output)
27
 
28
+ gr.Interface(
29
+ fn=predict,
30
+ inputs="text",
31
+ outputs="text",
32
+ title="Named Entity Recognition"
33
+ ).launch()