File size: 3,015 Bytes
20fe924
41a527e
 
20fe924
 
 
41a527e
20fe924
41a527e
 
20fe924
41a527e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20fe924
 
 
 
 
 
41a527e
20fe924
 
 
 
 
 
41a527e
 
20fe924
 
41a527e
20fe924
 
 
 
 
 
 
 
 
 
 
 
 
 
41a527e
 
 
 
20fe924
41a527e
 
 
20fe924
41a527e
 
 
 
 
 
 
20fe924
41a527e
 
20fe924
 
 
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
77
78
79
80
81
82
83
84
85
86
87
import streamlit as st
import os
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.document_loaders import PyPDFLoader
from langchain.chains import RetrievalQA
from groq import Groq

# Set the API Key directly (Not recommended for production)
GROQ_API_KEY = "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"

# Initialize Groq client
def initialize_groq_client():
    """Initialize the Groq client with the API key."""
    os.environ["GROQ_API_KEY"] = GROQ_API_KEY
    return Groq(api_key=GROQ_API_KEY)

# Generate response using Groq API
def generate_response(client, query, model_name="llama3-8b-8192"):
    """Generate a response using Groq's chat completion."""
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": query,
            }
        ],
        model=model_name,
    )
    return chat_completion.choices[0].message.content

# Load and process PDF
def process_pdf(pdf_path):
    """Load and split the PDF into documents."""
    loader = PyPDFLoader(pdf_path)
    documents = loader.load_and_split()
    return documents

# Create FAISS vector database
def create_vector_db(documents):
    """Create a FAISS vector database from documents."""
    embeddings = OpenAIEmbeddings()  # Use OpenAI embeddings for vectorization
    vector_db = FAISS.from_documents(documents, embeddings)
    return vector_db

# Build RAG pipeline
def build_rag_pipeline(vector_db, groq_client):
    """Build the Retrieval-Augmented Generation (RAG) pipeline."""
    retriever = vector_db.as_retriever(search_type="similarity", search_kwargs={"k": 5})
    return retriever, groq_client

# Streamlit App
def main():
    st.title("KP Universities Act 2016 - Query App")
    st.write("Ask any question about the KP Universities Act 2016.")

    # Step 1: Upload PDF
    uploaded_pdf = st.file_uploader("Upload the KP Universities Act 2016 PDF", type="pdf")
    if uploaded_pdf:
        with open("uploaded_act.pdf", "wb") as f:
            f.write(uploaded_pdf.read())
        documents = process_pdf("uploaded_act.pdf")
        st.success("PDF Loaded and Processed Successfully!")

        # Initialize Groq Client
        try:
            groq_client = initialize_groq_client()
            st.success("Groq Client Initialized Successfully!")

            # Build Vector DB and QA Chain
            vector_db = create_vector_db(documents)
            retriever, client = build_rag_pipeline(vector_db, groq_client)

            # Step 3: Ask Questions
            query = st.text_input("Ask a question:")
            if query:
                with st.spinner("Fetching Answer..."):
                    response = generate_response(client, query)
                    st.write("### Answer:")
                    st.write(response)

        except Exception as e:
            st.error(f"Error loading client or processing query: {e}")

if __name__ == "__main__":
    main()