Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234456788910111212131414151616171819192021222324252526272822 unmodified lines5152535454555656575859606162616263646564666768697068717270737273747575767677787979808181828383848586878687import streamlit as stimport osimport tempfileimport faissimport fitz # PyMuPDF for PDFsimport docximport openpyxlimport faiss
from langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain.embeddings import HuggingFaceEmbeddingsfrom langchain.vectorstores import FAISSfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.docstore.document import Documentfrom langchain_community.llms import Groqfrom langchain.llms import Groqfrom langchain.chains import RetrievalQAfrom langchain.schema import Document as LCDocument
# Initialize LLM# Load LLM (API key from Hugging Face secrets)llm = Groq( model="llama3-8b-8192", api_key=os.getenv("GROQ_API_KEY") # Put this in Hugging Face secrets api_key=os.getenv("GROQ_API_KEY"))
# Embeddings modelembedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# File processors# File readersdef read_pdf(file_path): text = "" doc = fitz.open(file_path)22 unmodified lines
if suffix.lower() == "pdf": return read_pdf(tmp_path) elif suffix.lower() in ["docx"]: elif suffix.lower() == "docx": return read_docx(tmp_path) elif suffix.lower() in ["xlsx"]: elif suffix.lower() == "xlsx": return read_excel(tmp_path) else: return "Unsupported file type."
# Streamlit UIst.title("π RAG Document QA with Faiss + LLaMA3")# Streamlit Appst.set_page_config(page_title="DocuQuery AI", layout="centered")st.title("π DocuQuery AI")st.markdown("Upload a document (PDF, Word, or Excel) and ask questions about its content using LLaMA3.")
uploaded_file = st.file_uploader("Upload a PDF, Word or Excel file", type=["pdf", "docx", "xlsx"])uploaded_file = st.file_uploader("Upload your document", type=["pdf", "docx", "xlsx"])
if uploaded_file: st.success("β
File uploaded successfully.") with st.spinner("Reading and processing file..."): raw_text = process_file(uploaded_file) raw_text = process_file(uploaded_file)
# Split text into chunks splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) texts = splitter.split_text(raw_text) docs = [Document(page_content=t) for t in texts] docs = [Document(page_content=chunk) for chunk in splitter.split_text(raw_text)]
# Embed and create vector store with st.spinner("Indexing document..."): with st.spinner("Indexing document with FAISS..."): db = FAISS.from_documents(docs, embedding_model) retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 4}) qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever) qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
st.success("β
Document indexed! Ask your questions below:") st.success("π Document indexed. Ask your question below!")
user_query = st.text_input("β Ask a question about your document") user_query = st.text_input("β Ask something about the document:") if user_query: with st.spinner("Generating answer..."): answer = qa.run(user_query) st.markdown(f"**π¬ Answer:** {answer}") response = qa_chain.run(user_query) st.markdown(f"**π¬ Answer:** {response}")app.py+23-23