Spaces:
Sleeping
Sleeping
Update rag.py
Browse files
rag.py
CHANGED
|
@@ -87,30 +87,17 @@ def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
|
| 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
|
| 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):
|
|
@@ -118,15 +105,21 @@ def get_best_answer(user_input):
|
|
| 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
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
# π Continue with normal similarity-based logic
|
| 132 |
user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
|
|
@@ -166,8 +159,40 @@ def get_best_answer(user_input):
|
|
| 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
|
| 173 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
print(f"Error querying Groq API: {e}")
|
| 88 |
return ""
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def get_best_answer(user_input):
|
| 91 |
+
"""Core function to find or generate the best response for a user query"""
|
| 92 |
# 1. Check for empty input
|
| 93 |
if not user_input.strip():
|
| 94 |
+
return {"response": "Please enter a valid question.", "should_scroll": False}
|
| 95 |
|
| 96 |
user_input_lower = user_input.lower().strip()
|
| 97 |
|
| 98 |
# 2. Check for minimum word count (3 words)
|
| 99 |
if len(user_input_lower.split()) < 3 and not any(greet in user_input_lower for greet in GREETINGS):
|
| 100 |
+
return {"response": "Please ask your question properly with at least 3 words.", "should_scroll": False}
|
| 101 |
|
| 102 |
# 3. Handle greetings (regardless of word count)
|
| 103 |
if any(greet in user_input_lower for greet in GREETINGS):
|
|
|
|
| 105 |
f"You are an official assistant for University of Education Lahore. "
|
| 106 |
f"Respond to this greeting in a friendly and professional manner: {user_input}"
|
| 107 |
)
|
| 108 |
+
return {
|
| 109 |
+
"response": greeting_response if greeting_response else "Hello! How can I assist you today?",
|
| 110 |
+
"should_scroll": True
|
| 111 |
+
}
|
| 112 |
|
| 113 |
# 4. Check if question is about fee
|
| 114 |
if any(keyword in user_input_lower for keyword in ["fee structure", "fees structure", "semester fees", "semester fee"]):
|
| 115 |
+
return {
|
| 116 |
+
"response": (
|
| 117 |
+
"π° For complete and up-to-date fee details for this program, we recommend visiting the official University of Education fee structure page.\n"
|
| 118 |
+
"You'll find comprehensive information regarding tuition, admission charges, and other applicable fees there.\n"
|
| 119 |
+
"π https://ue.edu.pk/allfeestructure.php"
|
| 120 |
+
),
|
| 121 |
+
"should_scroll": True
|
| 122 |
+
}
|
| 123 |
|
| 124 |
# π Continue with normal similarity-based logic
|
| 125 |
user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
|
|
|
|
| 159 |
βοΈ info@ue.edu.pk
|
| 160 |
π ue.edu.pk"""
|
| 161 |
|
|
|
|
| 162 |
return {
|
| 163 |
"response": response,
|
| 164 |
+
"should_scroll": True
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
def handle_submit(input_field_value):
|
| 168 |
+
"""Main function to handle user submissions"""
|
| 169 |
+
user_input = input_field_value.strip()
|
| 170 |
+
|
| 171 |
+
if not user_input:
|
| 172 |
+
return {"response": "Please enter a question", "should_scroll": False}
|
| 173 |
+
|
| 174 |
+
response = get_best_answer(user_input)
|
| 175 |
+
|
| 176 |
+
# Ensure consistent response format
|
| 177 |
+
if isinstance(response, str):
|
| 178 |
+
return {"response": response, "should_scroll": True}
|
| 179 |
+
elif isinstance(response, dict):
|
| 180 |
+
return response
|
| 181 |
+
else:
|
| 182 |
+
return {"response": "An error occurred while processing your request.", "should_scroll": False}
|
| 183 |
+
|
| 184 |
+
# Example usage
|
| 185 |
+
if __name__ == "__main__":
|
| 186 |
+
# Test the system
|
| 187 |
+
test_questions = [
|
| 188 |
+
"Hello",
|
| 189 |
+
"What's the fee structure?",
|
| 190 |
+
"What are the admission requirements for BSIT?",
|
| 191 |
+
"Short"
|
| 192 |
+
]
|
| 193 |
+
|
| 194 |
+
for question in test_questions:
|
| 195 |
+
print(f"\nQuestion: {question}")
|
| 196 |
+
response = handle_submit(question)
|
| 197 |
+
print(f"Response: {response['response']}")
|
| 198 |
+
print(f"Should scroll: {response['should_scroll']}")
|