Spaces:
Sleeping
Sleeping
Update rag.py
Browse files
rag.py
CHANGED
|
@@ -3,6 +3,7 @@ from sentence_transformers import SentenceTransformer, util
|
|
| 3 |
from groq import Groq
|
| 4 |
import datetime
|
| 5 |
import requests
|
|
|
|
| 6 |
from io import BytesIO
|
| 7 |
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
import numpy as np
|
|
@@ -27,6 +28,23 @@ dataset_questions = [item.get("Question", "").lower().strip() for item in datase
|
|
| 27 |
dataset_answers = [item.get("Answer", "") for item in dataset]
|
| 28 |
dataset_embeddings = similarity_model.encode(dataset_questions, convert_to_tensor=True)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
| 31 |
try:
|
| 32 |
chat_completion = groq_client.chat.completions.create(
|
|
@@ -60,6 +78,9 @@ def get_best_answer(user_input):
|
|
| 60 |
best_match_idx = similarities.argmax().item()
|
| 61 |
best_score = similarities[best_match_idx].item()
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
if best_score >= 0.65:
|
| 64 |
original_answer = dataset_answers[best_match_idx]
|
| 65 |
prompt = f"""As an official assistant for University of Education Lahore, provide a clear response:
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
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
|
|
|
|
| 28 |
dataset_answers = [item.get("Answer", "") for item in dataset]
|
| 29 |
dataset_embeddings = similarity_model.encode(dataset_questions, convert_to_tensor=True)
|
| 30 |
|
| 31 |
+
# Save unmatched queries to Hugging Face
|
| 32 |
+
def manage_unmatched_queries(query: str):
|
| 33 |
+
try:
|
| 34 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 35 |
+
try:
|
| 36 |
+
ds = load_dataset(HF_DATASET_REPO, token=HF_TOKEN)
|
| 37 |
+
df = ds["train"].to_pandas()
|
| 38 |
+
except:
|
| 39 |
+
df = pd.DataFrame(columns=["Query", "Timestamp", "Processed"])
|
| 40 |
+
if query not in df["Query"].values:
|
| 41 |
+
new_entry = {"Query": query, "Timestamp": timestamp, "Processed": False}
|
| 42 |
+
df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
|
| 43 |
+
updated_ds = Dataset.from_pandas(df)
|
| 44 |
+
updated_ds.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Failed to save query: {e}")
|
| 47 |
+
|
| 48 |
def query_groq_llm(prompt, model_name="llama3-70b-8192"):
|
| 49 |
try:
|
| 50 |
chat_completion = groq_client.chat.completions.create(
|
|
|
|
| 78 |
best_match_idx = similarities.argmax().item()
|
| 79 |
best_score = similarities[best_match_idx].item()
|
| 80 |
|
| 81 |
+
if best_score < 0.65:
|
| 82 |
+
manage_unmatched_queries(user_input)
|
| 83 |
+
|
| 84 |
if best_score >= 0.65:
|
| 85 |
original_answer = dataset_answers[best_match_idx]
|
| 86 |
prompt = f"""As an official assistant for University of Education Lahore, provide a clear response:
|