dlaima commited on
Commit
381fa45
·
verified ·
1 Parent(s): 94810db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  from transformers import pipeline
3
 
4
  # Load the NER pipeline from Hugging Face
@@ -6,21 +6,30 @@ ner_pipeline = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-e
6
 
7
  # Function to perform NER
8
  def perform_ner(text):
9
- # Run NER on the input text
10
- results = ner_pipeline(text)
11
-
12
- # Format results for better readability
13
- formatted_results = [
14
- {
15
- "Entity": result["entity"],
16
- "Word": result["word"],
17
- "Score": round(result["score"], 4),
18
- "Start": result["start"],
19
- "End": result["end"]
20
- }
21
- for result in results
22
- ]
23
- return formatted_results
 
 
 
 
 
 
 
 
 
24
 
25
  # Define the Gradio interface
26
  title = "Named Entity Recognition (NER) App"
@@ -44,3 +53,4 @@ interface.launch()
44
 
45
 
46
 
 
 
1
+ iimport gradio as gr
2
  from transformers import pipeline
3
 
4
  # Load the NER pipeline from Hugging Face
 
6
 
7
  # Function to perform NER
8
  def perform_ner(text):
9
+ try:
10
+ # Run NER on the input text
11
+ results = ner_pipeline(text)
12
+
13
+ # Check for empty results
14
+ if not results:
15
+ return [{"Entity": "-", "Word": "-", "Score": "-", "Start": "-", "End": "-"}]
16
+
17
+ # Format results for better readability
18
+ formatted_results = [
19
+ {
20
+ "Entity": result.get("entity", "-"),
21
+ "Word": result.get("word", "-"),
22
+ "Score": round(result.get("score", 4), 4),
23
+ "Start": result.get("start", "-"),
24
+ "End": result.get("end", "-")
25
+ }
26
+ for result in results
27
+ ]
28
+ return formatted_results
29
+ except Exception as e:
30
+ print(f"Error: {e}")
31
+ # Return a default error response
32
+ return [{"Entity": "Error", "Word": "Error", "Score": 0, "Start": 0, "End": 0}]
33
 
34
  # Define the Gradio interface
35
  title = "Named Entity Recognition (NER) App"
 
53
 
54
 
55
 
56
+