OmarElSayed commited on
Commit
9bd3c01
1 Parent(s): 0882c11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -2,12 +2,12 @@ import pandas as pd
2
  import spacy
3
  import gradio as gr
4
  import re
 
 
5
 
6
-
7
- dataset = pd.read_excel('Dataset-Verse-by-Verse.xlsx')
8
- dataset.rename(columns={'ArabicText': 'text'}, inplace=True)
9
  nlp = spacy.load('aravec_model')
10
- all_docs = [nlp(doc) for doc in dataset['text']]
11
 
12
  def clean_text(text):
13
  # remove tashkeel
@@ -31,21 +31,25 @@ def get_similar_sentences(text):
31
  text = clean_text(text)
32
  ref_sentence = nlp(text)
33
  similar_sentences = []
34
- for i,doc in enumerate(all_docs):
35
- similar_sentences.append((doc, ref_sentence.similarity(doc) , i))
36
- similar_sentences.sort(key=lambda x: x[1], reverse=True)
 
 
 
 
 
37
  top_10 = similar_sentences[:10]
38
- # add the surahnamearabic to text
39
- return dict(zip([' [ ' + dataset['SurahNameArabic'][i] + ' ] ' + doc.text for doc, _, i in top_10], [similarity for _, similarity, _ in top_10]))
40
 
41
  text_input = gr.inputs.Textbox(lines = 1 , label = "Enter a Quran Verse" )
42
 
43
- label = gr.outputs.Label()
44
  examples = ['الحمدلله رب العالمين',
45
  'مثلهم كمثل الذي استوقد نارًا فلما أضاءت ما حوله ذهب الله بنورهم وتركهم في ظلماتٍ لا يبصرون',
46
  'إن الذين كفروا سواء عليهم أأنذرتهم أم لم تنذرهم لا يؤمنون',
47
  'ونادى أصحاب الجنة أصحاب النار أن قد وجدنا ما وعدنا ربنا حقا فهل وجدتم ما وعد ربكم حقا ۖ قالوا نعم ۚ فأذن مؤذن بينهم أن لعنة الله على الظالمين'
48
  ]
49
 
50
- intf = gr.Interface(fn = get_similar_sentences , inputs = text_input , outputs = label , examples=examples )
51
- intf.launch()
 
2
  import spacy
3
  import gradio as gr
4
  import re
5
+ import json
6
+ import random
7
 
8
+ dataset = pd.read_csv('hatith_all.csv')
 
 
9
  nlp = spacy.load('aravec_model')
10
+ all_docs = [nlp(doc) for doc in dataset['hadith']]
11
 
12
  def clean_text(text):
13
  # remove tashkeel
 
31
  text = clean_text(text)
32
  ref_sentence = nlp(text)
33
  similar_sentences = []
34
+ sampled_docs = random.sample(all_docs , 15000)
35
+ for i, doc in enumerate(sampled_docs):
36
+ similarity_score = ref_sentence.similarity(doc)
37
+ similar_sentences.append({
38
+ "similar_sentence": str(doc),
39
+ "similarity_score": similarity_score,
40
+ })
41
+ similar_sentences.sort(key=lambda x: x['similarity_score'], reverse=True)
42
  top_10 = similar_sentences[:10]
43
+ return json.dumps(top_10)
 
44
 
45
  text_input = gr.inputs.Textbox(lines = 1 , label = "Enter a Quran Verse" )
46
 
47
+ output_text = gr.outputs.Textbox(label="Output Text")
48
  examples = ['الحمدلله رب العالمين',
49
  'مثلهم كمثل الذي استوقد نارًا فلما أضاءت ما حوله ذهب الله بنورهم وتركهم في ظلماتٍ لا يبصرون',
50
  'إن الذين كفروا سواء عليهم أأنذرتهم أم لم تنذرهم لا يؤمنون',
51
  'ونادى أصحاب الجنة أصحاب النار أن قد وجدنا ما وعدنا ربنا حقا فهل وجدتم ما وعد ربكم حقا ۖ قالوا نعم ۚ فأذن مؤذن بينهم أن لعنة الله على الظالمين'
52
  ]
53
 
54
+ intf = gr.Interface(fn = get_similar_sentences , inputs = text_input , outputs = "json", examples=examples )
55
+ intf.launch(debug = True)