medmediani commited on
Commit
d5e322f
·
1 Parent(s): 8032379
.ipynb_checkpoints/app-checkpoint.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from kwextractor import KeyWordExtractor
3
+
4
+ def greet(name):
5
+ return "Hello " + name + "!!"
6
+
7
+ kw_ex=KeyWordExtractor()
8
+
9
+
10
+ def generate_kws(context,num_kw):
11
+
12
+ context=context.strip()
13
+ if context:
14
+ try:
15
+ num_kw=int(num_kw)
16
+ except ValueError:
17
+ num_kw=None
18
+ return kw_ex.extract(context, num_kw) or ""
19
+
20
+ return result
21
+ else:
22
+ raise gr.Error("Please enter text in inputbox!!!!")
23
+
24
+
25
+
26
+ inputs=gr.Textbox(value=article_value, lines=5, label="Input Context",elem_id="inp_div")
27
+ nkws = gr.Textbox(label="Number of keywords to extract",elem_id="inp_div")
28
+ outputs=gr.Textbox(label="Generated Keywords",lines=6,elem_id="inp_div")
29
+
30
+ demo = gr.Interface(
31
+ generate_kws,
32
+ [inputs,nkws],
33
+ outputs,
34
+ title="Keyword Extraction Model",
35
+ css=".gradio-container {background-color: lightgray} #inp_div {background-color: #7FB3D5;}",
36
+ article="""<p style='text-align: center;'>Feel free to give us your <a href="https://www.pragnakalp.com/contact/" target="_blank">feedback</a> on this Keyword Extraction demo.</p>
37
+ """
38
+
39
+ )
40
+ demo.launch()
.ipynb_checkpoints/kwextractor-checkpoint.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import csv, os, sys
3
+ import argparse
4
+
5
+
6
+ from keybert import KeyBERT
7
+ from sentence_transformers import SentenceTransformer
8
+
9
+
10
+ class KeyWordExtractor():
11
+
12
+ def __init__(self):
13
+
14
+ KWE_PRETRAINED = 'medmediani/Arabic-KW-Mdel/model'
15
+ self.SEQ_LENGTH = 512
16
+ self.MAX_KW_NGS=3
17
+ self.NKW=3
18
+
19
+ #self.device = torch.device('cpu')
20
+ self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
21
+
22
+ sentence_model = SentenceTransformer(KWE_PRETRAINED)
23
+ self.kw_model = KeyBERT(model=sentence_model)
24
+
25
+
26
+ self.kw_model.to(self.device)
27
+
28
+ def extract(self, ctxt, nkws=None, max_kw_ngs=None):
29
+ nkws= nkws if nkws is not None else self.NKW
30
+ max_kw_ngs=max_kw_ngs if max_kw_ngs is not None else self.MAX_KW_NGS
31
+
32
+ kw=self.kw_model.extract_keywords(ctxt, keyphrase_ngram_range=(1, max_kw_ngs),
33
+ top_n=nkws,
34
+ #use_maxsum=True,nr_candidates=20, top_n=5,
35
+ #use_mmr=True, diversity=0.1,
36
+ stop_words=None)
37
+ return ", ".join(w for w,_ in kw)
38
+
app.py CHANGED
@@ -1,7 +1,40 @@
1
  import gradio as gr
 
2
 
3
  def greet(name):
4
  return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from kwextractor import KeyWordExtractor
3
 
4
  def greet(name):
5
  return "Hello " + name + "!!"
6
 
7
+ kw_ex=KeyWordExtractor()
8
+
9
+
10
+ def generate_kws(context,num_kw):
11
+
12
+ context=context.strip()
13
+ if context:
14
+ try:
15
+ num_kw=int(num_kw)
16
+ except ValueError:
17
+ num_kw=None
18
+ return kw_ex.extract(context, num_kw) or ""
19
+
20
+ return result
21
+ else:
22
+ raise gr.Error("Please enter text in inputbox!!!!")
23
+
24
+
25
+
26
+ inputs=gr.Textbox(value=article_value, lines=5, label="Input Context",elem_id="inp_div")
27
+ nkws = gr.Textbox(label="Number of keywords to extract",elem_id="inp_div")
28
+ outputs=gr.Textbox(label="Generated Keywords",lines=6,elem_id="inp_div")
29
+
30
+ demo = gr.Interface(
31
+ generate_kws,
32
+ [inputs,nkws],
33
+ outputs,
34
+ title="Keyword Extraction Model",
35
+ css=".gradio-container {background-color: lightgray} #inp_div {background-color: #7FB3D5;}",
36
+ article="""<p style='text-align: center;'>Feel free to give us your <a href="https://www.pragnakalp.com/contact/" target="_blank">feedback</a> on this Keyword Extraction demo.</p>
37
+ """
38
+
39
+ )
40
+ demo.launch()
kwextractor.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import csv, os, sys
3
+ import argparse
4
+
5
+
6
+ from keybert import KeyBERT
7
+ from sentence_transformers import SentenceTransformer
8
+
9
+
10
+ class KeyWordExtractor():
11
+
12
+ def __init__(self):
13
+
14
+ KWE_PRETRAINED = 'medmediani/Arabic-KW-Mdel/model'
15
+ self.SEQ_LENGTH = 512
16
+ self.MAX_KW_NGS=3
17
+ self.NKW=3
18
+
19
+ #self.device = torch.device('cpu')
20
+ self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
21
+
22
+ sentence_model = SentenceTransformer(KWE_PRETRAINED)
23
+ self.kw_model = KeyBERT(model=sentence_model)
24
+
25
+
26
+ self.kw_model.to(self.device)
27
+
28
+ def extract(self, ctxt, nkws=None, max_kw_ngs=None):
29
+ nkws= nkws if nkws is not None else self.NKW
30
+ max_kw_ngs=max_kw_ngs if max_kw_ngs is not None else self.MAX_KW_NGS
31
+
32
+ kw=self.kw_model.extract_keywords(ctxt, keyphrase_ngram_range=(1, max_kw_ngs),
33
+ top_n=nkws,
34
+ #use_maxsum=True,nr_candidates=20, top_n=5,
35
+ #use_mmr=True, diversity=0.1,
36
+ stop_words=None)
37
+ return ", ".join(w for w,_ in kw)
38
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==3.28.1
2
+ keybert==0.7.0
3
+ sentence_transformers==2.2.2
4
+ torch==1.12.1+cu113