emiliol commited on
Commit
7d5dede
·
verified ·
1 Parent(s): 0376b9b

ChatGPT updates

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -1,14 +1,26 @@
1
- from transformers import pipeline
2
  import gradio as gr
3
 
4
- # Load the NER pipeline
5
- ner_model = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
 
 
 
 
 
 
6
 
7
  def extract_named_entities(text):
8
- # Use the NER model to extract named entities
9
- entities = ner_model(text)
10
- # Format the output for better readability
11
- return [{"Entity": ent["entity_group"], "Text": ent["word"], "Score": round(ent["score"], 3)} for ent in entities]
 
 
 
 
 
 
12
 
13
  # Define the Gradio interface
14
  iface = gr.Interface(
@@ -17,8 +29,12 @@ iface = gr.Interface(
17
  outputs=gr.Dataframe(headers=["Entity", "Text", "Score"], label="Named Entities"),
18
  title="Named Entity Recognition",
19
  description="Input some text and get the named entities (like names, locations, organizations).",
 
 
 
 
 
20
  )
21
 
22
- # Launch the app
23
  if __name__ == "__main__":
24
  iface.launch()
 
1
+ from transformers import pipeline, AutoTokenizer, AutoModelForTokenClassification
2
  import gradio as gr
3
 
4
+ # Load the tokenizer and model
5
+ try:
6
+ tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
7
+ model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER")
8
+ ner_model = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="simple")
9
+ except Exception as e:
10
+ ner_model = None
11
+ print(f"Error loading model: {e}")
12
 
13
  def extract_named_entities(text):
14
+ if not text.strip():
15
+ return [{"Entity": "N/A", "Text": "No input provided", "Score": 0.0}]
16
+
17
+ try:
18
+ if ner_model is None:
19
+ raise ValueError("Model failed to load. Check logs for details.")
20
+ entities = ner_model(text)
21
+ return [{"Entity": ent["entity_group"], "Text": ent["word"], "Score": round(ent["score"], 3)} for ent in entities]
22
+ except Exception as e:
23
+ return [{"Entity": "Error", "Text": str(e), "Score": 0.0}]
24
 
25
  # Define the Gradio interface
26
  iface = gr.Interface(
 
29
  outputs=gr.Dataframe(headers=["Entity", "Text", "Score"], label="Named Entities"),
30
  title="Named Entity Recognition",
31
  description="Input some text and get the named entities (like names, locations, organizations).",
32
+ examples=[
33
+ ["My name is Emilio."],
34
+ ["Barack Obama was the President of the United States."],
35
+ ["OpenAI created ChatGPT."],
36
+ ]
37
  )
38
 
 
39
  if __name__ == "__main__":
40
  iface.launch()