dgotlieb commited on
Commit
0600bd7
·
verified ·
1 Parent(s): 27ab7f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -1,34 +1,38 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load a more general and powerful keyword extraction model
5
- # Note: This model uses the "ner" task (Named Entity Recognition)
6
- pipe = pipeline("ner", model="ybelkada/keyword-extraction-llm-transformers", aggregation_strategy="simple")
7
 
8
- def extract_keywords(text):
9
  """
10
- This function takes text and returns a comma-separated string of keywords.
11
  """
12
  try:
13
- keywords = pipe(text)
14
- if not keywords:
15
- return "No keywords found."
16
 
17
- # This model's output is slightly different, so we extract the 'word'
18
- # and replace spaces in multi-word keywords for readability.
19
- processed_keywords = [kw['word'].replace(' ', ' ') for kw in keywords]
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=extract_keywords,
28
- inputs=gr.Textbox(lines=5, label="Enter Your Text Here", placeholder="e.g., The camera quality is amazing for the price, but the battery life is disappointing..."),
29
- outputs=gr.Textbox(label="Extracted Keywords"),
30
- title="Course Demo: AI Keyword Extractor",
31
- description="A simple tool to find the most important keywords in a piece of text. Built for the 'Understanding AI' course."
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