mohbay commited on
Commit
fcadab7
·
verified ·
1 Parent(s): 79ea8cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from sentence_transformers import SentenceTransformer, util
4
+
5
+ # Load data
6
+ df = pd.read_csv("fatwas.csv")
7
+ model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")
8
+ embeddings = model.encode(df["question"].tolist(), convert_to_tensor=True)
9
+
10
+ def search_fatwa(query):
11
+ query_embedding = model.encode(query, convert_to_tensor=True)
12
+ scores = util.pytorch_cos_sim(query_embedding, embeddings)[0]
13
+ top_idx = int(scores.argmax())
14
+ return {
15
+ "question": df.iloc[top_idx]["question"],
16
+ "answer": df.iloc[top_idx]["answer"],
17
+ "link": df.iloc[top_idx]["link"]
18
+ }
19
+
20
+ iface = gr.Interface(
21
+ fn=search_fatwa,
22
+ inputs="text",
23
+ outputs="json",
24
+ allow_flagging="never",
25
+ title="Fatwa Search",
26
+ description="Ask a question and receive a relevant fatwa with a verified link"
27
+ )
28
+
29
+ iface.launch()