Spaces:
Sleeping
Sleeping
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() | |