Spaces:
Sleeping
Sleeping
ChatGPT updates
Browse files
app.py
CHANGED
@@ -1,14 +1,26 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
import gradio as gr
|
3 |
|
4 |
-
# Load the
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def extract_named_entities(text):
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|