dgotlieb commited on
Commit
2e28f66
·
verified ·
1 Parent(s): 279afb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -1,38 +1,32 @@
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
 
1
  import gradio as gr
2
+ import yake
3
 
4
+ # Initialize the YAKE keyword extractor. This is a lightweight statistical model.
5
+ # We'll ask it for the top 5 keywords.
6
+ kw_extractor = yake.KeywordExtractor(top=5, n=3)
7
 
8
+ def find_keywords(text):
9
  """
10
+ Uses the YAKE algorithm to extract keywords from text.
11
  """
12
  try:
13
+ keywords = kw_extractor.extract_keywords(text)
14
+ if not keywords:
15
+ return "No keywords found."
16
 
17
+ # YAKE returns a list of tuples (keyword, score). We just want the keyword.
18
+ result = [kw[0] for kw in keywords]
19
+ return ", ".join(result)
 
 
 
 
 
 
20
  except Exception as e:
21
  return f"An error occurred: {str(e)}"
22
 
23
  # Create the user interface for the app
24
  iface = gr.Interface(
25
+ fn=find_keywords,
26
+ inputs=gr.Textbox(lines=5, label="Enter Your Text Here", placeholder="e.g., The app is good but the location is not as good as I expected."),
27
+ outputs=gr.Textbox(label="Identified Keywords"),
28
+ title="Course Demo: Keyword Extractor",
29
+ description="A simple tool to find the most important keywords in a piece of text using a classic statistical algorithm. Built for the 'Understanding AI' course."
30
  )
31
 
32
  # Launch the app