OmarElSayed
Update app.py
4d67792
raw
history blame
No virus
2.2 kB
import pandas as pd
import spacy
import gradio as gr
import re
import json
import random
dataset = pd.read_csv('hatith_all_2.csv')
nlp = spacy.load('aravec_model')
all_docs = [nlp(doc) for doc in dataset['hadith']]
def clean_text(text):
# remove tashkeel
text = re.sub('[~ًٌٍَُِّْ]', '', text)
text = re.sub('[ًٌٍَُِّْـ]', '', text)
# ozbot el alef
text = re.sub('إ', 'ا', text)
text = re.sub('أ', 'ا', text)
text = re.sub('آ', 'ا', text)
# remove longation
text = re.sub(r'(.)\1+', r'\1\1', text)
# remove extra spaces
text = re.sub(' +', ' ', text)
text = text.strip()
text = re.sub('[\s]+', ' ', text)
# remove punctuations
text = re.sub(r'[^\w\s]', '', text)
return text
def get_similar_sentences(text):
text = clean_text(text)
ref_sentence = nlp(text)
similar_sentences = []
#sampled_docs = random.sample(all_docs , 15000)
for i, doc in enumerate(all_docs):
similarity_score = ref_sentence.similarity(doc)
similar_sentences.append({
"similar_sentence": dataset['reference'][i]+str(doc),
"similarity_score": similarity_score,
})
similar_sentences.sort(key=lambda x: x['similarity_score'], reverse=True)
top_10 = similar_sentences[:10]
return top_10
text_input = gr.inputs.Textbox(lines = 1 , label = "Enter a Quran Verse" )
output_text = gr.JSON()
examples = ['الحمدلله رب العالمين',
'مثلهم كمثل الذي استوقد نارًا فلما أضاءت ما حوله ذهب الله بنورهم وتركهم في ظلماتٍ لا يبصرون',
'إن الذين كفروا سواء عليهم أأنذرتهم أم لم تنذرهم لا يؤمنون',
'ونادى أصحاب الجنة أصحاب النار أن قد وجدنا ما وعدنا ربنا حقا فهل وجدتم ما وعد ربكم حقا ۖ قالوا نعم ۚ فأذن مؤذن بينهم أن لعنة الله على الظالمين'
]
intf = gr.Interface(fn = get_similar_sentences , inputs = text_input , outputs =output_text, examples=examples )
intf.launch(debug = True)