Spaces:
Sleeping
Sleeping
Update rag.py
Browse files
rag.py
CHANGED
|
@@ -27,6 +27,12 @@ GREETINGS = [
|
|
| 27 |
"hey there", "greetings"
|
| 28 |
]
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Load local dataset
|
| 31 |
try:
|
| 32 |
with open('dataset.json', 'r') as f:
|
|
@@ -100,7 +106,9 @@ def get_best_answer(user_input):
|
|
| 100 |
"🔗 https://ue.edu.pk/allfeestructure.php"
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
|
| 105 |
best_match_idx = similarities.argmax().item()
|
| 106 |
best_score = similarities[best_match_idx].item()
|
|
@@ -111,27 +119,21 @@ def get_best_answer(user_input):
|
|
| 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.
|
| 117 |
DO NOT add any new or extra information. ONLY rephrase and improve the clarity and formatting of the original answer.
|
| 118 |
-
|
| 119 |
### Question:
|
| 120 |
{user_input}
|
| 121 |
-
|
| 122 |
### Original Answer:
|
| 123 |
{original_answer}
|
| 124 |
-
|
| 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 |
-
|
| 132 |
### Question:
|
| 133 |
{user_input}
|
| 134 |
-
|
| 135 |
### Official Answer:
|
| 136 |
"""
|
| 137 |
|
|
|
|
| 27 |
"hey there", "greetings"
|
| 28 |
]
|
| 29 |
|
| 30 |
+
# Normalize user input for internal processing
|
| 31 |
+
def normalize_input(text):
|
| 32 |
+
text = text.lower().strip()
|
| 33 |
+
text = text.replace("which", "what")
|
| 34 |
+
return text
|
| 35 |
+
|
| 36 |
# Load local dataset
|
| 37 |
try:
|
| 38 |
with open('dataset.json', 'r') as f:
|
|
|
|
| 106 |
"🔗 https://ue.edu.pk/allfeestructure.php"
|
| 107 |
)
|
| 108 |
|
| 109 |
+
# Normalize only for similarity
|
| 110 |
+
normalized_input = normalize_input(user_input_lower)
|
| 111 |
+
user_embedding = similarity_model.encode(normalized_input, convert_to_tensor=True)
|
| 112 |
similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
|
| 113 |
best_match_idx = similarities.argmax().item()
|
| 114 |
best_score = similarities[best_match_idx].item()
|
|
|
|
| 119 |
if best_score >= 0.65:
|
| 120 |
original_answer = dataset_answers[best_match_idx]
|
| 121 |
prompt = f"""Name is UOE AI Assistant! You are an official assistant for the University of Education Lahore.
|
|
|
|
| 122 |
Rephrase the following official answer clearly and professionally.
|
| 123 |
Use structured formatting (like headings, bullet points, or numbered lists) where appropriate.
|
| 124 |
DO NOT add any new or extra information. ONLY rephrase and improve the clarity and formatting of the original answer.
|
|
|
|
| 125 |
### Question:
|
| 126 |
{user_input}
|
|
|
|
| 127 |
### Original Answer:
|
| 128 |
{original_answer}
|
|
|
|
| 129 |
### Rephrased Answer:
|
| 130 |
"""
|
| 131 |
else:
|
| 132 |
prompt = f"""Name is UOE AI Assistant! As an official assistant for University of Education Lahore, provide a helpful response:
|
| 133 |
Include relevant details about university policies.
|
| 134 |
If unsure, direct to official channels.
|
|
|
|
| 135 |
### Question:
|
| 136 |
{user_input}
|
|
|
|
| 137 |
### Official Answer:
|
| 138 |
"""
|
| 139 |
|