Quasa / app.py
masadonline's picture
Update app.py
7bf6ead verified
raw
history blame
2.37 kB
import os
import streamlit as st
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain_groq import ChatGroq # βœ… Correct import
# Set up Streamlit UI
st.set_page_config(page_title="SMEHelpBot πŸ€–", layout="wide")
st.title("πŸ€– SMEHelpBot – Your AI Assistant for Small Businesses")
# Set Groq API key (use .streamlit/secrets.toml or environment variable)
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY") or os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
st.error("❌ Please set your GROQ_API_KEY in environment or .streamlit/secrets.toml")
st.stop()
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
# Upload PDF
uploaded_file = st.file_uploader("πŸ“„ Upload a PDF (e.g., SME policies, documents):", type=["pdf"])
user_question = st.text_input("πŸ’¬ Ask a question about the uploaded document:")
if uploaded_file:
with open("temp.pdf", "wb") as f:
f.write(uploaded_file.read())
# Load PDF and split into chunks
loader = PyPDFLoader("temp.pdf")
documents = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_documents(documents)
# Create vector store
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectorstore = FAISS.from_documents(chunks, embeddings)
retriever = vectorstore.as_retriever()
# Load Groq LLaMA3
llm = ChatGroq(temperature=0.3, model_name="llama3-8b-8192")
# Set up RetrievalQA chain
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=retriever,
return_source_documents=True
)
# Handle user query
if user_question:
with st.spinner("πŸ€” Thinking..."):
result = qa_chain({"query": user_question})
st.success("βœ… Answer:")
st.write(result["result"])
with st.expander("πŸ“„ Source Snippets"):
for i, doc in enumerate(result["source_documents"]):
st.markdown(f"**Source {i+1}:**\n{doc.page_content[:300]}...")
else:
st.info("πŸ“‚ Upload a PDF and ask a question to get started.")