Spaces:
Sleeping

midrees2806 commited on
Commit
efdfbf5
·
verified ·
1 Parent(s): 2bb1f45

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +75 -68
rag.py CHANGED
@@ -1,32 +1,32 @@
1
  import json
 
 
 
 
 
 
 
 
2
  from sentence_transformers import SentenceTransformer, util
3
  from groq import Groq
4
- from datetime import datetime
5
- import requests
6
  from datasets import load_dataset, Dataset
7
- from io import BytesIO
 
8
  from PIL import Image, ImageDraw, ImageFont
9
- import numpy as np
10
- from dotenv import load_dotenv
11
- import os
12
- import pandas as pd
13
 
14
- # Load environment variables
15
  load_dotenv()
16
-
17
- # Initialize Groq client
18
  groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
19
-
20
- # Load models and dataset
21
  similarity_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
22
 
23
- # Config
24
  HF_DATASET_REPO = "midrees2806/unmatched_queries"
25
  HF_TOKEN = os.getenv("HF_TOKEN")
26
 
27
- # Load multiple JSON datasets
28
  dataset = []
29
  try:
 
30
  json_files = glob.glob('datasets/*.json')
31
  for file_path in json_files:
32
  with open(file_path, 'r', encoding='utf-8') as f:
@@ -35,103 +35,110 @@ try:
35
  for item in data:
36
  if isinstance(item, dict) and 'Question' in item and 'Answer' in item:
37
  dataset.append(item)
38
- else:
39
- print(f"Invalid entry in {file_path}: {item}")
40
  else:
41
- print(f"File {file_path} does not contain a list.")
42
  except Exception as e:
43
  print(f"Error loading datasets: {e}")
44
 
45
- # Precompute embeddings
46
  dataset_questions = [item.get("Question", "").lower().strip() for item in dataset]
47
  dataset_answers = [item.get("Answer", "") for item in dataset]
48
- dataset_embeddings = similarity_model.encode(dataset_questions, convert_to_tensor=True)
49
 
50
- # Save unmatched queries to Hugging Face
 
 
 
 
 
 
 
51
  def manage_unmatched_queries(query: str):
 
52
  try:
53
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
54
  try:
55
  ds = load_dataset(HF_DATASET_REPO, token=HF_TOKEN)
56
  df = ds["train"].to_pandas()
57
- except:
58
  df = pd.DataFrame(columns=["Query", "Timestamp", "Processed"])
 
59
  if query not in df["Query"].values:
60
  new_entry = {"Query": query, "Timestamp": timestamp, "Processed": False}
61
  df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
62
  updated_ds = Dataset.from_pandas(df)
63
  updated_ds.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN)
64
  except Exception as e:
65
- print(f"Failed to save query: {e}")
66
 
67
- def query_groq_llm(prompt, model_name="llama3-70b-8192"):
 
68
  try:
69
  chat_completion = groq_client.chat.completions.create(
70
- messages=[{
71
- "role": "user",
72
- "content": prompt
73
- }],
74
- model=model_name,
75
- temperature=0.7,
76
- max_tokens=500
77
  )
78
  return chat_completion.choices[0].message.content.strip()
79
  except Exception as e:
80
- print(f"Error querying Groq API: {e}")
81
- return ""
82
 
 
83
  def get_best_answer(user_input):
84
-
85
  if not user_input.strip():
86
  return "Please enter a valid question."
 
87
  user_input_lower = user_input.lower().strip()
88
-
89
  if len(user_input_lower.split()) < 3:
90
- return "Please ask your question properly with at least 3 words."
91
 
92
- # 👉 Check if question is about fee
93
- if any(keyword in user_input_lower for keyword in ["fee structure", "fees structure"]):
94
  return (
95
- "💰 For complete and up-to-date fee details for this program, we recommend visiting the official University of Education fee structure page.\n"
96
- "You’ll find comprehensive information regarding tuition, admission charges, and other applicable fees there.\n"
97
  "🔗 https://ue.edu.pk/allfeestructure.php"
98
  )
99
 
100
- # 🔁 Continue with normal similarity-based logic
 
 
 
101
  user_embedding = similarity_model.encode(user_input_lower, convert_to_tensor=True)
102
  similarities = util.pytorch_cos_sim(user_embedding, dataset_embeddings)[0]
103
  best_match_idx = similarities.argmax().item()
104
  best_score = similarities[best_match_idx].item()
105
 
106
- if best_score < 0.65:
107
- manage_unmatched_queries(user_input)
108
-
109
  if best_score >= 0.65:
 
110
  original_answer = dataset_answers[best_match_idx]
111
- prompt = f"""As an official assistant for University of Education Lahore, provide a clear response:
112
- Question: {user_input}
113
- Original Answer: {original_answer}
114
- Improved Answer:"""
115
- else:
116
- prompt = f"""As an official assistant for University of Education Lahore, provide a helpful response:
117
- Include relevant details about university policies.
118
- If unsure, direct to official channels.
119
- Question: {user_input}
120
- Official Answer:"""
121
-
122
- llm_response = query_groq_llm(prompt)
123
-
124
- if llm_response:
125
- for marker in ["Improved Answer:", "Official Answer:"]:
126
- if marker in llm_response:
127
- response = llm_response.split(marker)[-1].strip()
128
- break
129
- else:
130
- response = llm_response
131
  else:
132
- response = dataset_answers[best_match_idx] if best_score >= 0.65 else """For official information:
133
- 📞 +92-42-99262231-33
134
- ✉️ info@ue.edu.pk
135
- 🌐 ue.edu.pk"""
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
- return response
 
1
  import json
2
+ import glob
3
+ import os
4
+ import pandas as pd
5
+ import numpy as np
6
+ from datetime import datetime
7
+ from dotenv import load_dotenv
8
+
9
+ # Core AI Libraries
10
  from sentence_transformers import SentenceTransformer, util
11
  from groq import Groq
 
 
12
  from datasets import load_dataset, Dataset
13
+
14
+ # Image/UI Utils (Optional based on your imports)
15
  from PIL import Image, ImageDraw, ImageFont
16
+ from io import BytesIO
 
 
 
17
 
18
+ # 1. INITIALIZATION & CONFIG
19
  load_dotenv()
 
 
20
  groq_client = Groq(api_key=os.getenv("GROQ_API_KEY"))
 
 
21
  similarity_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
22
 
 
23
  HF_DATASET_REPO = "midrees2806/unmatched_queries"
24
  HF_TOKEN = os.getenv("HF_TOKEN")
25
 
26
+ # 2. DATA LOADING
27
  dataset = []
28
  try:
29
+ # Get all json files from the datasets folder
30
  json_files = glob.glob('datasets/*.json')
31
  for file_path in json_files:
32
  with open(file_path, 'r', encoding='utf-8') as f:
 
35
  for item in data:
36
  if isinstance(item, dict) and 'Question' in item and 'Answer' in item:
37
  dataset.append(item)
 
 
38
  else:
39
+ print(f"Skipping {file_path}: Expected a list of dictionaries.")
40
  except Exception as e:
41
  print(f"Error loading datasets: {e}")
42
 
43
+ # Precompute embeddings for faster search
44
  dataset_questions = [item.get("Question", "").lower().strip() for item in dataset]
45
  dataset_answers = [item.get("Answer", "") for item in dataset]
 
46
 
47
+ # Convert dataset to tensors once at startup
48
+ if dataset_questions:
49
+ dataset_embeddings = similarity_model.encode(dataset_questions, convert_to_tensor=True)
50
+ else:
51
+ dataset_embeddings = None
52
+ print("Warning: Dataset is empty!")
53
+
54
+ # 3. UTILITY FUNCTIONS
55
  def manage_unmatched_queries(query: str):
56
+ """Logs queries that didn't meet the similarity threshold to Hugging Face Hub."""
57
  try:
58
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
59
  try:
60
  ds = load_dataset(HF_DATASET_REPO, token=HF_TOKEN)
61
  df = ds["train"].to_pandas()
62
+ except Exception:
63
  df = pd.DataFrame(columns=["Query", "Timestamp", "Processed"])
64
+
65
  if query not in df["Query"].values:
66
  new_entry = {"Query": query, "Timestamp": timestamp, "Processed": False}
67
  df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
68
  updated_ds = Dataset.from_pandas(df)
69
  updated_ds.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN)
70
  except Exception as e:
71
+ print(f"Failed to save unmatched query: {e}")
72
 
73
+ def query_groq_llm(prompt, system_message="You are an official assistant for the University of Education Lahore."):
74
+ """Sends a prompt to Groq Llama 3 and returns the response."""
75
  try:
76
  chat_completion = groq_client.chat.completions.create(
77
+ messages=[
78
+ {"role": "system", "content": system_message},
79
+ {"role": "user", "content": prompt}
80
+ ],
81
+ model="llama3-70b-8192",
82
+ temperature=0.6, # Lower temperature for more factual responses
83
+ max_tokens=800
84
  )
85
  return chat_completion.choices[0].message.content.strip()
86
  except Exception as e:
87
+ print(f"Error querying Groq: {e}")
88
+ return None
89
 
90
+ # 4. MAIN RAG LOGIC
91
  def get_best_answer(user_input):
92
+ # Basic Validation
93
  if not user_input.strip():
94
  return "Please enter a valid question."
95
+
96
  user_input_lower = user_input.lower().strip()
 
97
  if len(user_input_lower.split()) < 3:
98
+ return "Please ask your question with more detail (at least 3 words)."
99
 
100
+ # Special Case: Fee Structure (Direct Link)
101
+ if any(kw in user_input_lower for kw in ["fee structure", "fees structure", "fee list"]):
102
  return (
103
+ "💰 For complete and up-to-date fee details, please visit the official page:\n"
 
104
  "🔗 https://ue.edu.pk/allfeestructure.php"
105
  )
106
 
107
+ # Calculate Similarity
108
+ if dataset_embeddings is None:
109
+ return "System is currently updating. Please try again in a moment."
110
+
111
  user_embedding = similarity_model.encode(user_input_lower, 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()
115
 
116
+ # DECISION BRIDGE
 
 
117
  if best_score >= 0.65:
118
+ # PATH 1: Verified Data Found
119
  original_answer = dataset_answers[best_match_idx]
120
+ prompt = (
121
+ f"A student asked: '{user_input}'.\n"
122
+ f"Based on our official records: '{original_answer}'.\n"
123
+ "Please rewrite this into a friendly, professional response."
124
+ )
125
+ system_role = "You are an official University assistant. Use ONLY the provided records."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  else:
127
+ # PATH 2: No Match - Fallback to General Knowledge & Log
128
+ manage_unmatched_queries(user_input)
129
+ prompt = (
130
+ f"A student asked: '{user_input}'.\n"
131
+ "I don't have a specific record for this. Provide a general helpful response "
132
+ "based on university standards. If you are unsure about specific dates or costs, "
133
+ "direct them to contact info@ue.edu.pk."
134
+ )
135
+ system_role = "You are a helpful University assistant. Direct unknown specific queries to official channels."
136
+
137
+ # Final Generation
138
+ llm_response = query_groq_llm(prompt, system_message=system_role)
139
+
140
+ # Final Fallback in case API fails
141
+ if not llm_response:
142
+ return original_answer if best_score >= 0.65 else "Please contact the university at info@ue.edu.pk for assistance."
143
 
144
+ return llm_response