CoolFace
Apppublic

TehminaFatima/RAG_example

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
streamlit_app.py76 linesDownload Raw Back to src
1import os2import streamlit as st3import PyPDF24import faiss5import numpy as np6import textwrap7from tempfile import NamedTemporaryFile8from sentence_transformers import SentenceTransformer9from groq import Groq10 11# Initialize Groq client12client = Groq(api_key="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")13 14# Load sentence transformer model15embedder = SentenceTransformer('all-MiniLM-L6-v2')16 17def extract_text_from_pdf(pdf_path):18    with open(pdf_path, 'rb') as file:19        reader = PyPDF2.PdfReader(file)20        text = ''21        for page in reader.pages:22            text += page.extract_text()23    return text24 25def chunk_text(text, max_tokens=500):26    return textwrap.wrap(text, width=max_tokens)27 28def get_embeddings(chunks):29    return embedder.encode(chunks, convert_to_tensor=True)30 31def create_faiss_index(embeddings):32    embeddings = embeddings.cpu().detach().numpy()33    index = faiss.IndexFlatL2(embeddings.shape[1])34    index.add(embeddings)35    return index36 37def search_faiss_index(index, query, chunks, top_k=3):38    query_embedding = embedder.encode([query])39    D, I = index.search(np.array(query_embedding), top_k)40    return [chunks[i] for i in I[0]]41 42def query_groq(prompt):43    chat_completion = client.chat.completions.create(44        messages=[{"role": "user", "content": prompt}],45        model="llama-3.3-70b-versatile"46    )47    return chat_completion.choices[0].message.content48 49# Streamlit UI50st.set_page_config(page_title="RAG with Groq & FAISS")51st.title("๐Ÿ“„๐Ÿ” RAG App with Groq + FAISS")52 53uploaded_file = st.file_uploader("Upload a PDF", type="pdf")54query = st.text_input("Enter your query")55 56if uploaded_file and query:57    with NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:58        temp_pdf.write(uploaded_file.read())59        pdf_path = temp_pdf.name60 61    # Process PDF and retrieve relevant context62    text = extract_text_from_pdf(pdf_path)63    chunks = chunk_text(text)64    embeddings = get_embeddings(chunks)65    index = create_faiss_index(embeddings)66    relevant_chunks = search_faiss_index(index, query, chunks)67    68    context = "\n".join(relevant_chunks)69    final_prompt = f"Based on the following context:\n{context}\n\nAnswer this query:\n{query}"70    response = query_groq(final_prompt)71 72    # Display response73    st.subheader("๐Ÿ“ข Response")74    st.write(response)75 76