Spaces:
Running
Running
import gradio as gr | |
import pandas as pd | |
from sentence_transformers import SentenceTransformer, util | |
# Load data | |
df = pd.read_excel("IslamWeb_output.xlsx") | |
df2 = pd.read_excel("JordanFatwas_all.xlsx") | |
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2") | |
embeddings = model.encode(df["question"].tolist(), convert_to_tensor=True) | |
embeddings2 = model.encode(df2["question"].tolist(), convert_to_tensor=True) | |
def search_fatwa(query): | |
query_embedding = model.encode(query, convert_to_tensor=True) | |
scores = util.pytorch_cos_sim(query_embedding, embeddings)[0] | |
top_idx = int(scores.argmax()) | |
scores2 = util.pytorch_cos_sim(query_embedding, embeddings2)[0] | |
top_idx2 = int(scores2.argmax()) | |
return { | |
"question1": df.iloc[top_idx]["question"], | |
"link1": df.iloc[top_idx]["link"], | |
"question2": df2.iloc[top_idx2]["question"], | |
"link2": df2.iloc[top_idx2]["link"], | |
} | |
print("AAAAA") | |
iface = gr.Interface( | |
fn=search_fatwa, | |
inputs="text", | |
outputs="json", | |
allow_flagging="never", | |
title="Fatwa Search", | |
description="Ask a question and receive a relevant fatwa with a verified link" | |
) | |
iface.launch() | |