Spaces:
Running
Running
| import gradio as gr | |
| import pandas as pd | |
| from sentence_transformers import SentenceTransformer, util | |
| # Load data | |
| df = pd.read_csv("fatwas.csv") | |
| model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2") | |
| embeddings = model.encode(df["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()) | |
| return { | |
| "question": df.iloc[top_idx]["question"], | |
| "answer": df.iloc[top_idx]["answer"], | |
| "link": df.iloc[top_idx]["link"] | |
| } | |
| 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() | |