Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,34 +2,45 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
|
5 |
-
# Load
|
6 |
df = pd.read_excel("IslamWeb_output.xlsx")
|
7 |
df2 = pd.read_excel("JordanFatwas_all.xlsx")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")
|
9 |
-
embeddings = model.encode(df["question"].tolist(), convert_to_tensor=True)
|
10 |
-
embeddings2 = model.encode(df2["question"].tolist(), convert_to_tensor=True)
|
11 |
|
|
|
12 |
def search_fatwa(query):
|
13 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
|
|
14 |
scores = util.pytorch_cos_sim(query_embedding, embeddings)[0]
|
15 |
top_idx = int(scores.argmax())
|
16 |
-
|
17 |
scores2 = util.pytorch_cos_sim(query_embedding, embeddings2)[0]
|
18 |
top_idx2 = int(scores2.argmax())
|
|
|
19 |
return {
|
20 |
"question1": df.iloc[top_idx]["question"],
|
21 |
"link1": df.iloc[top_idx]["link"],
|
22 |
"question2": df2.iloc[top_idx2]["question"],
|
23 |
"link2": df2.iloc[top_idx2]["link"],
|
24 |
}
|
25 |
-
|
|
|
26 |
iface = gr.Interface(
|
27 |
fn=search_fatwa,
|
28 |
inputs="text",
|
29 |
outputs="json",
|
30 |
allow_flagging="never",
|
31 |
-
title="Fatwa Search",
|
32 |
-
description="
|
33 |
)
|
34 |
|
35 |
iface.launch()
|
|
|
2 |
import pandas as pd
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
|
5 |
+
# Load files
|
6 |
df = pd.read_excel("IslamWeb_output.xlsx")
|
7 |
df2 = pd.read_excel("JordanFatwas_all.xlsx")
|
8 |
+
|
9 |
+
# Validate
|
10 |
+
for d, name in [(df, "IslamWeb"), (df2, "JordanFatwas")]:
|
11 |
+
if not {"question", "link"}.issubset(d.columns):
|
12 |
+
raise ValueError(f"❌ Missing required columns in {name}")
|
13 |
+
|
14 |
+
# Load model + encode
|
15 |
model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")
|
16 |
+
embeddings = model.encode(df["question"].fillna('').tolist(), convert_to_tensor=True)
|
17 |
+
embeddings2 = model.encode(df2["question"].fillna('').tolist(), convert_to_tensor=True)
|
18 |
|
19 |
+
# Define function
|
20 |
def search_fatwa(query):
|
21 |
query_embedding = model.encode(query, convert_to_tensor=True)
|
22 |
+
|
23 |
scores = util.pytorch_cos_sim(query_embedding, embeddings)[0]
|
24 |
top_idx = int(scores.argmax())
|
25 |
+
|
26 |
scores2 = util.pytorch_cos_sim(query_embedding, embeddings2)[0]
|
27 |
top_idx2 = int(scores2.argmax())
|
28 |
+
|
29 |
return {
|
30 |
"question1": df.iloc[top_idx]["question"],
|
31 |
"link1": df.iloc[top_idx]["link"],
|
32 |
"question2": df2.iloc[top_idx2]["question"],
|
33 |
"link2": df2.iloc[top_idx2]["link"],
|
34 |
}
|
35 |
+
|
36 |
+
# Interface
|
37 |
iface = gr.Interface(
|
38 |
fn=search_fatwa,
|
39 |
inputs="text",
|
40 |
outputs="json",
|
41 |
allow_flagging="never",
|
42 |
+
title="Fatwa Search (Dual Source)",
|
43 |
+
description="Get the most relevant fatwas from both datasets"
|
44 |
)
|
45 |
|
46 |
iface.launch()
|