| import os |
| import tempfile |
| import streamlit as st |
|
|
| from langchain_community.document_loaders import PyPDFLoader |
| from langchain_community.vectorstores import FAISS |
| from langchain_community.embeddings import HuggingFaceEmbeddings |
| from langchain.chains import RetrievalQA |
| from langchain.prompts import PromptTemplate |
| from langchain.schema import Document |
| |
| from langchain_groq import ChatGroq |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| llm = ChatGroq( |
| api_key=GROQ_API_KEY, |
| model_name="llama3-8b-8192", |
| temperature=0.1 |
| ) |
|
|
| |
| embedding = HuggingFaceEmbeddings( |
| model_name="sentence-transformers/all-MiniLM-L6-v2", |
| cache_folder="./hf_cache", |
| huggingfacehub_api_token=HUGGINGFACE_API_KEY |
| ) |
| |
| |
| |
|
|
| |
| st.title("ππ₯ Chat with PDF or Text using Groq + RAG") |
|
|
| |
| uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"]) |
|
|
| |
| pasted_text = st.text_area("Or paste some text below:") |
|
|
| |
| user_query = st.text_input("Ask a question about the content") |
|
|
| |
| submit_button = st.button("Submit") |
|
|
| if submit_button: |
| documents = [] |
|
|
| |
| if uploaded_file: |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file: |
| tmp_file.write(uploaded_file.read()) |
| tmp_path = tmp_file.name |
|
|
| loader = PyPDFLoader(tmp_path) |
| documents = loader.load_and_split() |
|
|
| |
| elif pasted_text.strip(): |
| documents = [Document(page_content=pasted_text)] |
|
|
| else: |
| st.warning("Please upload a PDF or paste some text.") |
| st.stop() |
|
|
| |
| vectorstore = FAISS.from_documents(documents, embedding) |
| retriever = vectorstore.as_retriever() |
|
|
| |
| prompt_template = PromptTemplate( |
| input_variables=["context", "question"], |
| template=""" |
| You are an AI assistant. Use the following context to answer the question. |
| Be concise, accurate, and helpful. |
| |
| Context: {context} |
| Question: {question} |
| Answer:""" |
| ) |
|
|
| |
| qa_chain = RetrievalQA.from_chain_type( |
| llm=llm, |
| chain_type="stuff", |
| retriever=retriever, |
| return_source_documents=True, |
| chain_type_kwargs={"prompt": prompt_template} |
| ) |
|
|
| |
| result = qa_chain({"query": user_query}) |
|
|
| |
| st.markdown("### π¬ Answer") |
| st.write(result["result"]) |
|
|
| |
| if uploaded_file: |
| with st.expander("π Sources"): |
| for i, doc in enumerate(result["source_documents"]): |
| st.write(f"**Page {i+1}** β {doc.metadata.get('source', 'Unknown')}") |
|
|