Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import faiss
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from groq import Groq
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from pypdf import PdfReader
|
| 8 |
+
|
| 9 |
+
# Use Hugging Face secret for API key
|
| 10 |
+
client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
| 11 |
+
|
| 12 |
+
# Embedding model
|
| 13 |
+
embedder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 14 |
+
|
| 15 |
+
# Load dataset (example: AG News small subset)
|
| 16 |
+
dataset = load_dataset("ag_news", split="train[:100]")
|
| 17 |
+
texts = [item["text"] for item in dataset]
|
| 18 |
+
|
| 19 |
+
# Chunking
|
| 20 |
+
def chunk_text(text, chunk_size=200):
|
| 21 |
+
words = text.split()
|
| 22 |
+
return [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]
|
| 23 |
+
|
| 24 |
+
chunks = []
|
| 25 |
+
for doc in texts:
|
| 26 |
+
chunks.extend(chunk_text(doc))
|
| 27 |
+
|
| 28 |
+
# Embeddings + FAISS
|
| 29 |
+
embeddings = embedder.encode(chunks)
|
| 30 |
+
dimension = embeddings.shape[1]
|
| 31 |
+
index = faiss.IndexFlatL2(dimension)
|
| 32 |
+
index.add(embeddings)
|
| 33 |
+
|
| 34 |
+
# Retriever
|
| 35 |
+
def retrieve(query, k=3):
|
| 36 |
+
q_emb = embedder.encode([query])
|
| 37 |
+
D, I = index.search(q_emb, k)
|
| 38 |
+
return [chunks[i] for i in I[0]]
|
| 39 |
+
|
| 40 |
+
# RAG pipeline
|
| 41 |
+
def rag_pipeline(query):
|
| 42 |
+
retrieved = retrieve(query)
|
| 43 |
+
context = "\n".join(retrieved)
|
| 44 |
+
prompt = f"Answer the question using context:\n{context}\n\nQuestion: {query}\nAnswer:"
|
| 45 |
+
response = client.chat.completions.create(
|
| 46 |
+
model="mixtral-8x7b-32768",
|
| 47 |
+
messages=[{"role": "user", "content": prompt}]
|
| 48 |
+
)
|
| 49 |
+
return response.choices[0].message.content
|
| 50 |
+
|
| 51 |
+
# Gradio UI
|
| 52 |
+
def chatbot(query):
|
| 53 |
+
return rag_pipeline(query)
|
| 54 |
+
|
| 55 |
+
demo = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="RAGify Bilal")
|
| 56 |
+
demo.launch()
|