ibn-sidah-team commited on
Commit
1f0ded3
1 Parent(s): b4a973d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ python -m venv .env
2
+
3
+
4
+ # Activate the virtual environment
5
+ source .env/bin/activate
6
+ # Deactivate the virtual environment
7
+ source .env/bin/deactivate
8
+
9
+
10
+ pip install -r requirements.txt
11
+ pip install faiss-cpu
12
+ # In the requirements.txt but not installed correctly so we have to use pip command
13
+ #!pip install gensim==3.8.1
14
+ python -m spacy download en_core_web_sm
15
+ # Install gradio for user interface
16
+ pip install gradio
17
+
18
+ import sensegram
19
+ from wsd import WSD
20
+ from gensim.models import KeyedVectors
21
+
22
+ # Model files
23
+ sense_vectors_fpath = "./best_sense_gram_model/best_model.sense_vectors"
24
+ word_vectors_fpath = "./best_sense_gram_model/best_model.word_vectors"
25
+
26
+ # Model parameters
27
+ max_context_words = 3
28
+ context_window_size = 5
29
+ ignore_case = True
30
+ lang = "ar" # to filter out stopwords
31
+
32
+ # Model loading ... takes some time
33
+ sv = sensegram.SenseGram.load_word2vec_format(sense_vectors_fpath, binary=False)
34
+ wv = KeyedVectors.load_word2vec_format(word_vectors_fpath, binary=False, unicode_errors="ignore")
35
+
36
+ # Method takes word and context and retirn the results of the model.
37
+ def wsd_method(word, context):
38
+ output = ""
39
+ output += "Probabilities of the senses:\n{}\n\n".format(sv.get_senses(word, ignore_case=ignore_case))
40
+ for sense_id, prob in sv.get_senses(word, ignore_case=ignore_case):
41
+ output += sense_id
42
+ output += ("\n"+"="*20+"\n")
43
+ for rsense_id, sim in sv.wv.most_similar(sense_id):
44
+ output += "{} {:f}\n".format(rsense_id, sim)
45
+ output +="\n"
46
+ # Disambiguate a word in a context
47
+ wsd_model = WSD(sv, wv, window=context_window_size, lang=lang,
48
+ max_context_words=max_context_words, ignore_case=ignore_case)
49
+ output += str(wsd_model.disambiguate(context, word))
50
+ return output
51
+
52
+ import gradio as gr
53
+ # Lanuching live demo
54
+ demo = gr.Interface(
55
+ fn=wsd_method,
56
+ inputs=[gr.Textbox(lines=1, placeholder="الكلمة"),gr.Textbox(lines=2, placeholder="السياق")],
57
+ outputs="text",
58
+ title="فـك الالتباس الدلالي",
59
+ description="فضلًا أدخل الكلمة ثم السياق ثم اضغط على زر إرسال، ولاستعراض المخرجات كاملة يرجى استخدام زر التمرير لأسفل.",
60
+ )
61
+ demo.launch()
62
+