Spaces:
Sleeping
Sleeping
Download rag.py from dauduchieu/fqa: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/spaces/dauduchieu/fqa/resolve/main/rag.py
- Command line
-
hf download hf://spaces/dauduchieu/fqa/rag.py
-
curl -L -o rag.py https://huggingface.co/spaces/dauduchieu/fqa/resolve/main/rag.py
1.12 kB
| from sentence_transformers import SentenceTransformer, util | |
| # Load model embedding | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| def find_similar_examples(query, df, num_results=5): | |
| if df.empty: | |
| print("Empty data!") | |
| return [] | |
| if "ques" not in df or "ans" not in df: | |
| print("Column 'ques' or 'ans' not exist in DataFrame.") | |
| return [] | |
| questions = df["ques"].dropna().tolist() | |
| if not questions: | |
| print("There are no valid examples!") | |
| return [] | |
| question_embeddings = model.encode(questions, convert_to_tensor=True) | |
| query_embedding = model.encode(query, convert_to_tensor=True) | |
| scores = util.pytorch_cos_sim(query_embedding, question_embeddings)[0] | |
| # Limit num of result | |
| num_results = min(num_results, len(questions)) | |
| top_matches = scores.topk(num_results) | |
| similar_examples = [] | |
| for idx in top_matches.indices.tolist(): # Convert tensor to list[int] | |
| question = questions[idx] | |
| answer = df.iloc[idx]["ans"] | |
| similar_examples.append({"ques": question, "ans": answer}) | |
| return similar_examples | |