Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =========================================
|
| 2 |
+
# 1οΈβ£ Install imports
|
| 3 |
+
# =========================================
|
| 4 |
+
import os
|
| 5 |
+
import pickle
|
| 6 |
+
import faiss
|
| 7 |
+
import numpy as np
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from sentence_transformers import SentenceTransformer
|
| 10 |
+
from groq import Groq
|
| 11 |
+
|
| 12 |
+
# =========================================
|
| 13 |
+
# 2οΈβ£ Load Groq API from HF Secrets
|
| 14 |
+
# =========================================
|
| 15 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 16 |
+
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 17 |
+
|
| 18 |
+
# =========================================
|
| 19 |
+
# 3οΈβ£ Load FAISS + Chunks
|
| 20 |
+
# =========================================
|
| 21 |
+
index = faiss.read_index("faiss_index.bin")
|
| 22 |
+
|
| 23 |
+
with open("chunks.pkl", "rb") as f:
|
| 24 |
+
all_chunks = pickle.load(f)
|
| 25 |
+
|
| 26 |
+
# Load embedding model (only for query embedding)
|
| 27 |
+
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 28 |
+
|
| 29 |
+
# =========================================
|
| 30 |
+
# 4οΈβ£ Groq Query Function
|
| 31 |
+
# =========================================
|
| 32 |
+
def groq_query(prompt):
|
| 33 |
+
completion = client.chat.completions.create(
|
| 34 |
+
messages=[
|
| 35 |
+
{"role": "system", "content": "You are a strict university information assistant. Answer ONLY from provided context. Do not guess."},
|
| 36 |
+
{"role": "user", "content": prompt}
|
| 37 |
+
],
|
| 38 |
+
model=GROQ_MODEL,
|
| 39 |
+
temperature=0
|
| 40 |
+
)
|
| 41 |
+
return completion.choices[0].message.content
|
| 42 |
+
|
| 43 |
+
# =========================================
|
| 44 |
+
# 5οΈβ£ RAG Function
|
| 45 |
+
# =========================================
|
| 46 |
+
def rag_answer(query, k=3):
|
| 47 |
+
|
| 48 |
+
if not query.strip():
|
| 49 |
+
return "Please enter a valid question."
|
| 50 |
+
|
| 51 |
+
query_embedding = embedding_model.encode([query])
|
| 52 |
+
query_embedding = query_embedding / np.linalg.norm(query_embedding, axis=1, keepdims=True)
|
| 53 |
+
query_embedding = query_embedding.astype("float32")
|
| 54 |
+
|
| 55 |
+
distances, indices = index.search(query_embedding, k)
|
| 56 |
+
|
| 57 |
+
retrieved_texts = [all_chunks[i] for i in indices[0]]
|
| 58 |
+
|
| 59 |
+
context = "\n\n".join(retrieved_texts)
|
| 60 |
+
|
| 61 |
+
prompt = f"""
|
| 62 |
+
You must answer ONLY using the information provided below.
|
| 63 |
+
If answer is not in the context, reply exactly:
|
| 64 |
+
"The information is not available in the provided documents."
|
| 65 |
+
|
| 66 |
+
Context:
|
| 67 |
+
{context}
|
| 68 |
+
|
| 69 |
+
Question:
|
| 70 |
+
{query}
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
return groq_query(prompt)
|
| 74 |
+
|
| 75 |
+
# =========================================
|
| 76 |
+
# 6οΈβ£ Gradio UI
|
| 77 |
+
# =========================================
|
| 78 |
+
with gr.Blocks() as demo:
|
| 79 |
+
gr.Markdown("## π Baltistan Academic Assistant")
|
| 80 |
+
|
| 81 |
+
chatbot = gr.Chatbot()
|
| 82 |
+
msg = gr.Textbox(placeholder="Ask your question...")
|
| 83 |
+
|
| 84 |
+
def respond(message, history):
|
| 85 |
+
answer = rag_answer(message)
|
| 86 |
+
history = history + [(message, answer)]
|
| 87 |
+
return history, ""
|
| 88 |
+
|
| 89 |
+
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 90 |
+
|
| 91 |
+
demo.launch()
|