emiliol commited on
Commit
8c6ebcd
1 Parent(s): 7d5dede

ChatGPT updates from logs

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -1,26 +1,27 @@
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,11 +30,6 @@ 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__":
 
1
+ from transformers import pipeline
2
  import gradio as gr
3
 
4
+ # Load the NER pipeline
5
  try:
6
+ ner_model = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
7
+ print("Model loaded successfully.")
 
8
  except Exception as e:
9
  ner_model = None
10
  print(f"Error loading model: {e}")
11
 
12
  def extract_named_entities(text):
13
+ if ner_model is None:
14
+ return [["Error", "Model not loaded", 0.0]]
15
+
16
  if not text.strip():
17
+ return [["Error", "No input provided", 0.0]]
18
 
19
  try:
 
 
20
  entities = ner_model(text)
21
+ # Convert list of dictionaries to list of lists for Gradio compatibility
22
+ return [[ent["entity_group"], ent["word"], round(ent["score"], 3)] for ent in entities]
23
  except Exception as e:
24
+ return [["Error", str(e), 0.0]]
25
 
26
  # Define the Gradio interface
27
  iface = gr.Interface(
 
30
  outputs=gr.Dataframe(headers=["Entity", "Text", "Score"], label="Named Entities"),
31
  title="Named Entity Recognition",
32
  description="Input some text and get the named entities (like names, locations, organizations).",
 
 
 
 
 
33
  )
34
 
35
  if __name__ == "__main__":