Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load a
|
| 5 |
-
#
|
| 6 |
-
pipe = pipeline("ner", model="
|
| 7 |
|
| 8 |
-
def
|
| 9 |
"""
|
| 10 |
-
This function
|
| 11 |
"""
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
if not
|
| 15 |
-
return "No
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
return ", ".join(processed_keywords)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
-
return f"An error occurred: {e}"
|
| 24 |
|
| 25 |
# Create the user interface for the app
|
| 26 |
iface = gr.Interface(
|
| 27 |
-
fn=
|
| 28 |
-
inputs=gr.Textbox(lines=5, label="Enter Your Text Here", placeholder="e.g.,
|
| 29 |
-
outputs=gr.Textbox(label="
|
| 30 |
-
title="Course Demo: AI
|
| 31 |
-
description="A simple tool to find
|
| 32 |
)
|
| 33 |
|
| 34 |
# Launch the app
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load a very popular and stable Named Entity Recognition (NER) model.
|
| 5 |
+
# This is much more reliable and a core NLP concept.
|
| 6 |
+
pipe = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
|
| 7 |
|
| 8 |
+
def find_key_entities(text):
|
| 9 |
"""
|
| 10 |
+
This function uses a NER model to find key entities (people, places, topics).
|
| 11 |
"""
|
| 12 |
try:
|
| 13 |
+
entities = pipe(text)
|
| 14 |
+
if not entities:
|
| 15 |
+
return "No key entities found."
|
| 16 |
|
| 17 |
+
# Extract the 'word' for each entity found by the model.
|
| 18 |
+
# This will find names, locations, organizations, etc.
|
| 19 |
+
results = [entity['word'].replace(' ', '') for entity in entities]
|
|
|
|
| 20 |
|
| 21 |
+
# Remove any duplicates
|
| 22 |
+
unique_results = list(dict.fromkeys(results))
|
| 23 |
+
|
| 24 |
+
return ", ".join(unique_results)
|
| 25 |
+
|
| 26 |
except Exception as e:
|
| 27 |
+
return f"An error occurred: {str(e)}"
|
| 28 |
|
| 29 |
# Create the user interface for the app
|
| 30 |
iface = gr.Interface(
|
| 31 |
+
fn=find_key_entities,
|
| 32 |
+
inputs=gr.Textbox(lines=5, label="Enter Your Text Here", placeholder="e.g., Apple announced the new iPhone in California with Tim Cook."),
|
| 33 |
+
outputs=gr.Textbox(label="Identified Key Entities"),
|
| 34 |
+
title="Course Demo: AI Entity Extractor",
|
| 35 |
+
description="A simple tool to find key entities (people, places, topics) in text. This shows how AI can understand parts of a sentence. Built for the 'Understanding AI' course."
|
| 36 |
)
|
| 37 |
|
| 38 |
# Launch the app
|