JERNGOC commited on
Commit
bac1a26
Β·
verified Β·
1 Parent(s): b628e1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -28,9 +28,9 @@ def jieba_tokenizer(text):
28
  vectorizer = CountVectorizer(tokenizer=jieba_tokenizer)
29
  kw_model = KeyBERT()
30
 
31
- # Extract keywords
32
- def extract_keywords(doc):
33
- keywords = kw_model.extract_keywords(doc, vectorizer=vectorizer)
34
  return keywords
35
 
36
  # Plot keywords
@@ -48,7 +48,7 @@ def plot_keywords(keywords, title):
48
  return '/tmp/keywords_plot.png'
49
 
50
  # Function to scrape content and extract keywords
51
- def scrape_and_extract(url):
52
  response = requests.get(url)
53
  response.encoding = 'utf-8'
54
  soup = BeautifulSoup(response.text, 'html.parser')
@@ -56,7 +56,7 @@ def scrape_and_extract(url):
56
  content_div = soup.find('div', {'class': 'caas-body'})
57
  paragraphs = content_div.find_all('p')
58
  content = '\n'.join([p.text.strip() for p in paragraphs])
59
- keywords = extract_keywords(content)
60
  plot_path = plot_keywords(keywords, "Keyword Extraction Results")
61
  return title, content, keywords, plot_path
62
 
@@ -67,10 +67,11 @@ st.title("πŸ” Professional Keyword Extraction Tool")
67
  st.write("Extracts keywords from a given URL and displays a bar chart of the keywords with their respective scores.")
68
 
69
  url = st.text_input("🌐 Enter the article URL here:")
 
70
 
71
  if st.button("Extract Keywords"):
72
  if url:
73
- title, content, keywords, plot_path = scrape_and_extract(url)
74
 
75
  st.subheader("πŸ“„ Article Title")
76
  st.write(title)
@@ -86,5 +87,3 @@ if st.button("Extract Keywords"):
86
  st.image(plot_path)
87
  else:
88
  st.warning("Please enter a URL to extract keywords.")
89
-
90
-
 
28
  vectorizer = CountVectorizer(tokenizer=jieba_tokenizer)
29
  kw_model = KeyBERT()
30
 
31
+ # Extract keywords using MMR
32
+ def extract_keywords(doc, diversity):
33
+ keywords = kw_model.extract_keywords(doc, vectorizer=vectorizer, use_mmr=True, diversity=diversity)
34
  return keywords
35
 
36
  # Plot keywords
 
48
  return '/tmp/keywords_plot.png'
49
 
50
  # Function to scrape content and extract keywords
51
+ def scrape_and_extract(url, diversity):
52
  response = requests.get(url)
53
  response.encoding = 'utf-8'
54
  soup = BeautifulSoup(response.text, 'html.parser')
 
56
  content_div = soup.find('div', {'class': 'caas-body'})
57
  paragraphs = content_div.find_all('p')
58
  content = '\n'.join([p.text.strip() for p in paragraphs])
59
+ keywords = extract_keywords(content, diversity)
60
  plot_path = plot_keywords(keywords, "Keyword Extraction Results")
61
  return title, content, keywords, plot_path
62
 
 
67
  st.write("Extracts keywords from a given URL and displays a bar chart of the keywords with their respective scores.")
68
 
69
  url = st.text_input("🌐 Enter the article URL here:")
70
+ diversity = st.slider("Adjust Diversity (0.0: Most Relevant, 1.0: Most Diverse)", 0.0, 1.0, 0.5, step=0.01)
71
 
72
  if st.button("Extract Keywords"):
73
  if url:
74
+ title, content, keywords, plot_path = scrape_and_extract(url, diversity)
75
 
76
  st.subheader("πŸ“„ Article Title")
77
  st.write(title)
 
87
  st.image(plot_path)
88
  else:
89
  st.warning("Please enter a URL to extract keywords.")