Spaces:
Runtime error
Runtime error
muhalmutaz
commited on
Commit
·
88d19e7
1
Parent(s):
8dc2fd6
app
Browse files- app.py +49 -0
- quran-simple-clean.txt +0 -0
- tafsir-simple-clean.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
| 2 |
+
import torch
|
| 3 |
+
import pickle
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# bi_encoder = SentenceTransformer("microsoft/Multilingual-MiniLM-L12-H384")
|
| 8 |
+
cross_encoder = CrossEncoder("cross-encoder/mmarco-mMiniLMv2-L12-H384-v1")
|
| 9 |
+
# Corpus from quran
|
| 10 |
+
my_file = open("quran-simple-clean.txt", "r",encoding="utf-8")
|
| 11 |
+
data = my_file.read()
|
| 12 |
+
corpus = data.split("\n")
|
| 13 |
+
embedder = SentenceTransformer('symanto/sn-xlm-roberta-base-snli-mnli-anli-xnli')
|
| 14 |
+
corpus_embeddings = embedder.encode(corpus, convert_to_tensor=True)
|
| 15 |
+
|
| 16 |
+
def search(query,top_k=100):
|
| 17 |
+
print("New query:")
|
| 18 |
+
print(query)
|
| 19 |
+
ans=[]
|
| 20 |
+
##### Sematic Search #####
|
| 21 |
+
# Encode the query using the bi-encoder and find potentially relevant passages
|
| 22 |
+
question_embedding = embedder.encode(query, convert_to_tensor=True)
|
| 23 |
+
hits = util.semantic_search(question_embedding, corpus_embeddings, top_k=top_k)
|
| 24 |
+
hits = hits[0] # Get the hits for the first query
|
| 25 |
+
|
| 26 |
+
##### Re-Ranking #####
|
| 27 |
+
# Now, score all retrieved passages with the cross_encoder
|
| 28 |
+
cross_inp = [[query, corpus[hit['corpus_id']]] for hit in hits]
|
| 29 |
+
cross_scores = cross_encoder.predict(cross_inp)
|
| 30 |
+
|
| 31 |
+
# Sort results by the cross-encoder scores
|
| 32 |
+
for idx in range(len(cross_scores)):
|
| 33 |
+
hits[idx]['cross-score'] = cross_scores[idx]
|
| 34 |
+
|
| 35 |
+
hits = sorted(hits, key=lambda x: x['cross-score'], reverse=True)
|
| 36 |
+
|
| 37 |
+
for idx, hit in enumerate(hits[0:5]):
|
| 38 |
+
ans.append(corpus[hit['corpus_id']])
|
| 39 |
+
return "\n\n".join(ans)
|
| 40 |
+
|
| 41 |
+
exp=[""]
|
| 42 |
+
|
| 43 |
+
desc="البحث بالمعنى."
|
| 44 |
+
|
| 45 |
+
inp=gr.inputs.Textbox(lines=1, placeholder=None, default="", label="أدخل كلمات البحث هنا")
|
| 46 |
+
out=gr.outputs.Textbox(type="auto",label="نتائج البحث")
|
| 47 |
+
|
| 48 |
+
iface = gr.Interface(fn=search, inputs=inp, outputs=out,examples=exp,article=desc,title="البحث المعنوي في القرآن الكريم")
|
| 49 |
+
iface.launch(share=True)
|
quran-simple-clean.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tafsir-simple-clean.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|