CoolFace
Apppublic

UnsaMalik/Generative_Engine_Optimization_App

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py111 linesDownload Raw Back to root
1import os2import tempfile3import streamlit as st4 5from langchain_community.document_loaders import PyPDFLoader6from langchain_community.vectorstores import FAISS7from langchain_community.embeddings import HuggingFaceEmbeddings8from langchain.chains import RetrievalQA9from langchain.prompts import PromptTemplate10from langchain.schema import Document11# from langchain_groq import GroqLLM12from langchain_groq import ChatGroq13 14# --- Environment Variables ---15GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your-groq-api-key")16HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY", "your-huggingface-api-key")17 18# --- Initialize Groq LLM ---19# llm = GroqLLM(20#     api_key=GROQ_API_KEY,21#     model="llama3-8b-8192",22#     temperature=0.123# )24llm = ChatGroq(25    api_key=GROQ_API_KEY,26    model_name="llama3-8b-8192",  # Note: it's `model_name` not `model`27    temperature=0.128)29 30# --- HuggingFace Embeddings ---31embedding = HuggingFaceEmbeddings(32    model_name="sentence-transformers/all-MiniLM-L6-v2",33    cache_folder="./hf_cache",34    # huggingfacehub_api_token=HUGGINGFACE_API_KEY35)36# embedding = HuggingFaceEmbeddings(37#     model_name="sentence-transformers/all-MiniLM-L6-v2"38# )39 40# --- Streamlit UI ---41st.title("๐Ÿ“„๐Ÿ“ฅ Chat with PDF or Text using Groq + RAG")42 43# Option to upload PDF44uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])45 46# Option to paste raw text47pasted_text = st.text_area("Or paste some text below:")48 49# User's question50user_query = st.text_input("Ask a question about the content")51 52# Submit button53submit_button = st.button("Submit")54 55if submit_button:56    documents = []57 58    # Handle uploaded PDF59    if uploaded_file:60        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:61            tmp_file.write(uploaded_file.read())62            tmp_path = tmp_file.name63 64        loader = PyPDFLoader(tmp_path)65        documents = loader.load_and_split()66 67    # Handle pasted text if no PDF68    elif pasted_text.strip():69        documents = [Document(page_content=pasted_text)]70 71    else:72        st.warning("Please upload a PDF or paste some text.")73        st.stop()74 75    # Create vector store76    vectorstore = FAISS.from_documents(documents, embedding)77    retriever = vectorstore.as_retriever()78 79    # Optional custom prompt80    prompt_template = PromptTemplate(81        input_variables=["context", "question"],82        template="""83        You are an AI assistant. Use the following context to answer the question.84        Be concise, accurate, and helpful.85        Context: {context}86        Question: {question}87        Answer:"""88    )89 90    # QA Chain91    qa_chain = RetrievalQA.from_chain_type(92        llm=llm,93        chain_type="stuff",94        retriever=retriever,95        return_source_documents=True,96        chain_type_kwargs={"prompt": prompt_template}97    )98 99    # Run QA100    result = qa_chain({"query": user_query})101 102    # Show result103    st.markdown("### ๐Ÿ’ฌ Answer")104    st.write(result["result"])105 106    # Show sources (only if from PDF)107    if uploaded_file:108        with st.expander("๐Ÿ“„ Sources"):109            for i, doc in enumerate(result["source_documents"]):110                st.write(f"**Page {i+1}** โ€” {doc.metadata.get('source', 'Unknown')}")111