import pandas as pd import spacy import gradio as gr import re import json dataset = pd.read_excel('Dataset-Verse-by-Verse.xlsx') dataset.rename(columns={'ArabicText': 'text'}, inplace=True) nlp = spacy.load('aravec_model') all_docs = [nlp(doc) for doc in dataset['text']] 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 = [] for i, doc in enumerate(dataset['text']): similarity_score = ref_sentence.similarity(nlp(doc)) similar_sentence = doc surah_name = dataset['SurahNameArabic'][i] ayah_no = int(dataset['AyahNo'][i]) surah_no = int(dataset['SurahNo'][i]) similar_sentences.append({ "similar_sentence": similar_sentence, "similarity_score": similarity_score, "surahName": surah_name, "AyahNo": ayah_no, "SurahNumber" : surah_no }) 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)