midrees2806 commited on
Commit
2cb70a0
Β·
verified Β·
1 Parent(s): c23cce8

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +46 -6
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
- # πŸ‘‰ Check if question is about fee
 
 
 
 
 
 
 
 
 
 
 
 
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’ll find comprehensive information regarding tuition, admission charges, and other applicable fees there.\n"
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
- # 3. Save unmatched queries (threshold = 0.65)
102
  if best_score < 0.65:
103
  manage_unmatched_queries(user_input)
104
 
@@ -130,4 +166,8 @@ def get_best_answer(user_input):
130
  βœ‰οΈ [email protected]
131
  🌐 ue.edu.pk"""
132
 
133
- return response
 
 
 
 
 
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
  βœ‰οΈ [email protected]
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
+ }