Spaces:
Sleeping
Sleeping
File size: 2,368 Bytes
1725afa 972a93c 7bf6ead 972a93c 7bf6ead 1725afa 972a93c 7bf6ead 714b045 1725afa 714b045 7bf6ead 972a93c 1725afa 7bf6ead 972a93c 1725afa 972a93c 1725afa 972a93c 7bf6ead 972a93c 1725afa 972a93c 7bf6ead 1725afa 972a93c 7bf6ead 972a93c 1725afa 972a93c 7bf6ead 1725afa 7bf6ead 972a93c 714b045 972a93c 7bf6ead |
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 |
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.")
|