Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from transformers import AutoTokenizer, AutoModel
|
| 4 |
+
import torch
|
| 5 |
+
import faiss
|
| 6 |
+
import numpy as np
|
| 7 |
+
import os
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
# -------------------- Config --------------------
|
| 11 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Add this as a secret in Hugging Face
|
| 12 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
+
EMBED_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
|
| 14 |
+
|
| 15 |
+
# -------------------- PDF Processing --------------------
|
| 16 |
+
def read_pdf(file):
|
| 17 |
+
pdf = PdfReader(file)
|
| 18 |
+
text = ""
|
| 19 |
+
for page in pdf.pages:
|
| 20 |
+
text += page.extract_text() + "\n"
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
# -------------------- Chunking --------------------
|
| 24 |
+
def chunk_text(text, chunk_size=500, overlap=50):
|
| 25 |
+
words = text.split()
|
| 26 |
+
chunks = []
|
| 27 |
+
start = 0
|
| 28 |
+
while start < len(words):
|
| 29 |
+
end = start + chunk_size
|
| 30 |
+
chunk = " ".join(words[start:end])
|
| 31 |
+
chunks.append(chunk)
|
| 32 |
+
start += chunk_size - overlap
|
| 33 |
+
return chunks
|
| 34 |
+
|
| 35 |
+
# -------------------- Embedding --------------------
|
| 36 |
+
@st.cache_resource
|
| 37 |
+
def load_embedding_model():
|
| 38 |
+
tokenizer = AutoTokenizer.from_pretrained(EMBED_MODEL_NAME)
|
| 39 |
+
model = AutoModel.from_pretrained(EMBED_MODEL_NAME)
|
| 40 |
+
return tokenizer, model
|
| 41 |
+
|
| 42 |
+
def get_embeddings(text_chunks, tokenizer, model):
|
| 43 |
+
embeddings = []
|
| 44 |
+
for chunk in text_chunks:
|
| 45 |
+
inputs = tokenizer(chunk, return_tensors="pt", truncation=True, padding=True)
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
outputs = model(**inputs)
|
| 48 |
+
emb = outputs.last_hidden_state[:, 0, :].numpy()[0]
|
| 49 |
+
embeddings.append(emb)
|
| 50 |
+
return np.array(embeddings)
|
| 51 |
+
|
| 52 |
+
# -------------------- FAISS --------------------
|
| 53 |
+
def build_faiss_index(embeddings):
|
| 54 |
+
dimension = embeddings.shape[1]
|
| 55 |
+
index = faiss.IndexFlatL2(dimension)
|
| 56 |
+
index.add(embeddings)
|
| 57 |
+
return index
|
| 58 |
+
|
| 59 |
+
def search_index(index, query, tokenizer, model, chunks, top_k=3):
|
| 60 |
+
inputs = tokenizer(query, return_tensors="pt", truncation=True, padding=True)
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
outputs = model(**inputs)
|
| 63 |
+
query_emb = outputs.last_hidden_state[:, 0, :].numpy()
|
| 64 |
+
distances, indices = index.search(query_emb, top_k)
|
| 65 |
+
return [chunks[i] for i in indices[0]]
|
| 66 |
+
|
| 67 |
+
# -------------------- GROQ Query --------------------
|
| 68 |
+
def query_groq(context, question):
|
| 69 |
+
prompt = f"""You are a helpful engineering assistant. Use the following context to answer the question.
|
| 70 |
+
|
| 71 |
+
Context:
|
| 72 |
+
{context}
|
| 73 |
+
|
| 74 |
+
Question:
|
| 75 |
+
{question}
|
| 76 |
+
"""
|
| 77 |
+
headers = {
|
| 78 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 79 |
+
"Content-Type": "application/json"
|
| 80 |
+
}
|
| 81 |
+
payload = {
|
| 82 |
+
"model": "llama3-8b-8192",
|
| 83 |
+
"messages": [
|
| 84 |
+
{"role": "system", "content": "You are a helpful engineering tutor."},
|
| 85 |
+
{"role": "user", "content": prompt}
|
| 86 |
+
],
|
| 87 |
+
"temperature": 0.3,
|
| 88 |
+
"max_tokens": 512
|
| 89 |
+
}
|
| 90 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 91 |
+
response.raise_for_status()
|
| 92 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 93 |
+
|
| 94 |
+
# -------------------- Streamlit UI --------------------
|
| 95 |
+
st.title("📚 engGlass RAG Assistant")
|
| 96 |
+
st.write("Upload a PDF, ask engineering questions, and get smart answers!")
|
| 97 |
+
|
| 98 |
+
uploaded_file = st.file_uploader("Upload PDF", type="pdf")
|
| 99 |
+
question = st.text_input("Ask a question based on the uploaded document:")
|
| 100 |
+
|
| 101 |
+
if uploaded_file and question:
|
| 102 |
+
with st.spinner("Reading and processing PDF..."):
|
| 103 |
+
text = read_pdf(uploaded_file)
|
| 104 |
+
chunks = chunk_text(text)
|
| 105 |
+
tokenizer, model = load_embedding_model()
|
| 106 |
+
embeddings = get_embeddings(chunks, tokenizer, model)
|
| 107 |
+
index = build_faiss_index(embeddings)
|
| 108 |
+
top_chunks = search_index(index, question, tokenizer, model, chunks)
|
| 109 |
+
context = "\n".join(top_chunks)
|
| 110 |
+
|
| 111 |
+
with st.spinner("Generating answer from Groq..."):
|
| 112 |
+
try:
|
| 113 |
+
answer = query_groq(context, question)
|
| 114 |
+
st.markdown("### 💡 Answer")
|
| 115 |
+
st.write(answer)
|
| 116 |
+
except Exception as e:
|
| 117 |
+
st.error(f"Error: {str(e)}")
|