Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234456789101011121314141617181920151617221819202132 unmodified lines545556575859606162636465666768697071647273747576777078798081828376848586798781888990918586879293949596979899100101102103104105import streamlit as stimport osimport tempfileimport fitz # PyMuPDF for PDFsimport fitzimport docximport openpyxlimport faiss
from groq import Groqfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.llms import Groqfrom langchain.vectorstores import FAISSfrom langchain.text_splitter import RecursiveCharacterTextSplitterfrom langchain.docstore.document import Documentfrom langchain.chains import RetrievalQA
# Load LLM (API key from Hugging Face secrets)llm = Groq( model="llama3-8b-8192", api_key=os.getenv("GROQ_API_KEY"))# Initialize Groq clientgroq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
# Embeddings model# Embedding modelembedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# File readers32 unmodified lines else: return "Unsupported file type."
# Prompt builderdef build_prompt(context, question): return f"""You are a helpful assistant. Answer the question based only on the context provided below.
Context:{context}
Question:{question}
Answer:"""
# 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.")st.markdown("Upload a document and ask questions about it using LLaMA-3 from Groq.")
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..."): with st.spinner("Processing file..."): raw_text = process_file(uploaded_file)
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) docs = [Document(page_content=chunk) for chunk in splitter.split_text(raw_text)]
with st.spinner("Indexing document with FAISS..."): with st.spinner("Embedding & indexing..."): db = FAISS.from_documents(docs, embedding_model) retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 4}) qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
st.success("π Document indexed. Ask your question below!") st.success("π Document indexed. Ask a question!")
user_query = st.text_input("β Ask something about the document:") if user_query: with st.spinner("Generating answer..."): response = qa_chain.run(user_query) st.markdown(f"**π¬ Answer:** {response}") with st.spinner("Generating response..."): retrieved_docs = retriever.get_relevant_documents(user_query) context = "\n".join([doc.page_content for doc in retrieved_docs])
prompt = build_prompt(context, user_query)
response = groq_client.chat.completions.create( model="llama3-8b-8192", messages=[ {"role": "user", "content": prompt} ] )
st.markdown(f"**π¬ Answer:** {response.choices[0].message.content}")app.py+35-17