Spaces:
Sleeping
Sleeping
Update rag.py
Browse files
rag.py
CHANGED
|
@@ -24,6 +24,13 @@ similarity_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
|
| 24 |
HF_DATASET_REPO = "midrees2806/unmatched_queries" # Your dataset repo
|
| 25 |
HF_TOKEN = os.getenv("HF_TOKEN") # From Space secrets
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# --- Dataset Loading ---
|
| 28 |
try:
|
| 29 |
with open('dataset.json', 'r') as f:
|
|
@@ -64,7 +71,6 @@ def manage_unmatched_queries(query: str):
|
|
| 64 |
print(f"Failed to save query: {e}")
|
| 65 |
|
| 66 |
# --- Enhanced LLM Query ---
|
| 67 |
-
|
| 68 |
def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
| 69 |
try:
|
| 70 |
chat_completion = groq_client.chat.completions.create(
|
|
@@ -81,14 +87,44 @@ def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
|
| 81 |
print(f"Error querying Groq API: {e}")
|
| 82 |
return ""
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
def get_best_answer(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
user_input_lower = user_input.lower().strip()
|
| 86 |
-
|
| 87 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
if any(keyword in user_input_lower for keyword in ["fee structure", "fees structure", "semester fees", "semester fee"]):
|
| 89 |
return (
|
| 90 |
"π° For complete and up-to-date fee details for this program, we recommend visiting the official University of Education fee structure page.\n"
|
| 91 |
-
"You
|
| 92 |
"π https://ue.edu.pk/allfeestructure.php"
|
| 93 |
)
|
| 94 |
|
|
@@ -98,7 +134,7 @@ def get_best_answer(user_input):
|
|
| 98 |
best_match_idx = similarities.argmax().item()
|
| 99 |
best_score = similarities[best_match_idx].item()
|
| 100 |
|
| 101 |
-
#
|
| 102 |
if best_score < 0.65:
|
| 103 |
manage_unmatched_queries(user_input)
|
| 104 |
|
|
@@ -130,4 +166,8 @@ def get_best_answer(user_input):
|
|
| 130 |
βοΈ info@ue.edu.pk
|
| 131 |
π ue.edu.pk"""
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
HF_DATASET_REPO = "midrees2806/unmatched_queries" # Your dataset repo
|
| 25 |
HF_TOKEN = os.getenv("HF_TOKEN") # From Space secrets
|
| 26 |
|
| 27 |
+
# Greeting words list
|
| 28 |
+
GREETINGS = [
|
| 29 |
+
"hi", "hello", "hey", "good morning", "good afternoon", "good evening",
|
| 30 |
+
"assalam o alaikum", "salam", "namaste", "hola", "bonjour", "hi there",
|
| 31 |
+
"hey there", "greetings", "howdy"
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
# --- Dataset Loading ---
|
| 35 |
try:
|
| 36 |
with open('dataset.json', 'r') as f:
|
|
|
|
| 71 |
print(f"Failed to save query: {e}")
|
| 72 |
|
| 73 |
# --- Enhanced LLM Query ---
|
|
|
|
| 74 |
def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
| 75 |
try:
|
| 76 |
chat_completion = groq_client.chat.completions.create(
|
|
|
|
| 87 |
print(f"Error querying Groq API: {e}")
|
| 88 |
return ""
|
| 89 |
|
| 90 |
+
def handle_submit():
|
| 91 |
+
user_input = input_field.value.strip()
|
| 92 |
+
|
| 93 |
+
if not user_input:
|
| 94 |
+
show_message("Please enter a question")
|
| 95 |
+
return
|
| 96 |
+
|
| 97 |
+
response = get_best_answer(user_input)
|
| 98 |
+
|
| 99 |
+
if response.get('should_scroll', False):
|
| 100 |
+
scroll_to_answer()
|
| 101 |
+
|
| 102 |
+
display_response(response.get('response', ''))
|
| 103 |
+
|
| 104 |
def get_best_answer(user_input):
|
| 105 |
+
# 1. Check for empty input
|
| 106 |
+
if not user_input.strip():
|
| 107 |
+
return None # This will be handled in the frontend to prevent submission
|
| 108 |
+
|
| 109 |
user_input_lower = user_input.lower().strip()
|
| 110 |
+
|
| 111 |
+
# 2. Check for minimum word count (3 words)
|
| 112 |
+
if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
|
| 113 |
+
return "Please ask your question properly with at least 3 words."
|
| 114 |
+
|
| 115 |
+
# 3. Handle greetings (regardless of word count)
|
| 116 |
+
if any(greet in user_input_lower for greet in GREETINGS):
|
| 117 |
+
greeting_response = query_groq_llm(
|
| 118 |
+
f"You are an official assistant for University of Education Lahore. "
|
| 119 |
+
f"Respond to this greeting in a friendly and professional manner: {user_input}"
|
| 120 |
+
)
|
| 121 |
+
return greeting_response if greeting_response else "Hello! How can I assist you today?"
|
| 122 |
+
|
| 123 |
+
# 4. Check if question is about fee
|
| 124 |
if any(keyword in user_input_lower for keyword in ["fee structure", "fees structure", "semester fees", "semester fee"]):
|
| 125 |
return (
|
| 126 |
"π° For complete and up-to-date fee details for this program, we recommend visiting the official University of Education fee structure page.\n"
|
| 127 |
+
"You'll find comprehensive information regarding tuition, admission charges, and other applicable fees there.\n"
|
| 128 |
"π https://ue.edu.pk/allfeestructure.php"
|
| 129 |
)
|
| 130 |
|
|
|
|
| 134 |
best_match_idx = similarities.argmax().item()
|
| 135 |
best_score = similarities[best_match_idx].item()
|
| 136 |
|
| 137 |
+
# Save unmatched queries (threshold = 0.65)
|
| 138 |
if best_score < 0.65:
|
| 139 |
manage_unmatched_queries(user_input)
|
| 140 |
|
|
|
|
| 166 |
βοΈ info@ue.edu.pk
|
| 167 |
π ue.edu.pk"""
|
| 168 |
|
| 169 |
+
# Return the response along with a flag to indicate auto-scrolling should happen
|
| 170 |
+
return {
|
| 171 |
+
"response": response,
|
| 172 |
+
"should_scroll": True # Frontend should use this to trigger auto-scrolling
|
| 173 |
+
}
|