Spaces:
Sleeping
Sleeping
File size: 2,114 Bytes
1725afa 972a93c 714b045 972a93c 714b045 1725afa 972a93c 714b045 1725afa 714b045 972a93c 1725afa 972a93c 1725afa 972a93c 714b045 972a93c 1725afa 972a93c 714b045 972a93c 1725afa 972a93c 714b045 1725afa 972a93c 714b045 1725afa 972a93c 1725afa 972a93c 1725afa 972a93c 714b045 972a93c 714b045 |
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 |
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 for Groq API
# App config
st.set_page_config(page_title="SMEHelpBot π€", layout="wide")
st.title("π€ SMEHelpBot β Your AI Assistant for Small Businesses")
# API key setup
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY") or os.getenv("GROQ_API_KEY") or "your_groq_api_key_here"
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 based on the document:")
if uploaded_file:
with open("temp.pdf", "wb") as f:
f.write(uploaded_file.read())
loader = PyPDFLoader("temp.pdf")
documents = loader.load()
# Split into chunks
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_documents(documents)
# Create embeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectorstore = FAISS.from_documents(chunks, embeddings)
retriever = vectorstore.as_retriever()
# Groq LLaMA3 setup
llm = ChatGroq(temperature=0.3, model_name="llama3-8b-8192")
# Retrieval QA
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=retriever,
return_source_documents=True
)
if user_question:
with st.spinner("Generating answer..."):
result = qa({"query": user_question})
st.success(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 enter a question to get started.")
|