Spaces:
Running
Running
Update rag.py
Browse files
rag.py
CHANGED
@@ -31,15 +31,15 @@ GREETINGS = [
|
|
31 |
try:
|
32 |
with open('dataset.json', 'r') as f:
|
33 |
dataset = json.load(f)
|
34 |
-
if not all(isinstance(item, dict) and '
|
35 |
raise ValueError("Invalid dataset structure")
|
36 |
except Exception as e:
|
37 |
print(f"Error loading dataset: {e}")
|
38 |
dataset = []
|
39 |
|
40 |
# Precompute embeddings
|
41 |
-
dataset_questions = [item.get("
|
42 |
-
dataset_answers = [item.get("
|
43 |
dataset_embeddings = similarity_model.encode(dataset_questions, convert_to_tensor=True)
|
44 |
|
45 |
# Save unmatched queries to Hugging Face
|
@@ -110,7 +110,7 @@ def get_best_answer(user_input):
|
|
110 |
|
111 |
if best_score >= 0.65:
|
112 |
original_answer = dataset_answers[best_match_idx]
|
113 |
-
prompt = f"""You are an official assistant for the University of Education Lahore.
|
114 |
|
115 |
Rephrase the following official answer clearly and professionally.
|
116 |
Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
|
@@ -125,7 +125,7 @@ DO NOT add any new or extra information. ONLY rephrase and improve the clarity a
|
|
125 |
### Rephrased Answer:
|
126 |
"""
|
127 |
else:
|
128 |
-
prompt = f"""As an official assistant for University of Education Lahore, provide a helpful response:
|
129 |
Include relevant details about university policies.
|
130 |
If unsure, direct to official channels.
|
131 |
|
|
|
31 |
try:
|
32 |
with open('dataset.json', 'r') as f:
|
33 |
dataset = json.load(f)
|
34 |
+
if not all(isinstance(item, dict) and 'Question' in item and 'Answer' in item for item in dataset):
|
35 |
raise ValueError("Invalid dataset structure")
|
36 |
except Exception as e:
|
37 |
print(f"Error loading dataset: {e}")
|
38 |
dataset = []
|
39 |
|
40 |
# Precompute embeddings
|
41 |
+
dataset_questions = [item.get("Question", "").lower().strip() for item in dataset]
|
42 |
+
dataset_answers = [item.get("Answer", "") for item in dataset]
|
43 |
dataset_embeddings = similarity_model.encode(dataset_questions, convert_to_tensor=True)
|
44 |
|
45 |
# Save unmatched queries to Hugging Face
|
|
|
110 |
|
111 |
if best_score >= 0.65:
|
112 |
original_answer = dataset_answers[best_match_idx]
|
113 |
+
prompt = f"""Name is UOE AI Assistant! You are an official assistant for the University of Education Lahore.
|
114 |
|
115 |
Rephrase the following official answer clearly and professionally.
|
116 |
Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
|
|
|
125 |
### Rephrased Answer:
|
126 |
"""
|
127 |
else:
|
128 |
+
prompt = f"""Name is UOE AI Assistant! As an official assistant for University of Education Lahore, provide a helpful response:
|
129 |
Include relevant details about university policies.
|
130 |
If unsure, direct to official channels.
|
131 |
|