File size: 2,215 Bytes
3895252
 
 
 
 
 
 
 
 
74d77a0
3895252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import streamlit as st
from PyPDF2 import PdfReader
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
from groq import Groq

# Initialize Groq Client
client = Groq(api_key=os.getenv("groq_api_key"))

# Load embedding model
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")

# Initialize FAISS vector store
dimension = 384  # Embedding dimension of the model
index = faiss.IndexFlatL2(dimension)

# Function to extract text from PDF
def extract_text_from_pdf(pdf_file):
    reader = PdfReader(pdf_file)
    text = ""
    for page in reader.pages:
        text += page.extract_text()
    return text

# Function to split text into chunks
def chunk_text(text, chunk_size=500):
    words = text.split()
    return [" ".join(words[i:i+chunk_size]) for i in range(0, len(words), chunk_size)]

# Function to add embeddings to vector database
def add_to_vector_db(chunks):
    embeddings = embedding_model.encode(chunks)
    index.add(np.array(embeddings, dtype="float32"))
    return embeddings

# Streamlit frontend
st.title("RAG-based PDF Query Application")

# PDF upload
uploaded_file = st.file_uploader("Upload your PDF file", type=["pdf"])
if uploaded_file:
    st.write("Processing your PDF...")
    text = extract_text_from_pdf(uploaded_file)
    chunks = chunk_text(text)
    add_to_vector_db(chunks)
    st.success("PDF processed and embeddings stored in the vector database!")

# Query input
query = st.text_input("Enter your query:")
if query:
    # Generate embedding for query
    query_embedding = embedding_model.encode([query])
    
    # Retrieve relevant chunks from FAISS
    distances, indices = index.search(np.array(query_embedding, dtype="float32"), k=5)
    context = "\n".join([chunks[i] for i in indices[0]])

    # Interact with Groq API
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": f"Context: {context}\n\nQuery: {query}"
            }
        ],
        model="llama3-8b-8192",
        stream=False,
    )
    response = chat_completion.choices[0].message.content

    # Display response
    st.write("Response:")
    st.write(response)